2013-07-25 42 views
0

在此應用程序中,我正在處理該操作,我希望在應用程序不在前臺時收到通知。基本上,當您在應用程序中時,您的在線用戶可以向您發送消息。根據您在應用程序中的位置,您會看到一個對話框,指出您收到了一條消息或獲取了消息活動的全文。不過,我想處理您在未運行應用程序時收到的消息。我已經構建了一個通知活動,但我不知道如何讓應用程序在後臺運行並推送通知。當應用程序未運行時創建通知

這裏是將接收到警報的活動的一個例子,一個消息被髮送到你

public class TabExercise extends TabActivity implements ChatCallbackAdapter{ 
    /** Called when the activity is first created. */ 
    public StartSocket connect; 
    public static Context mContext; 
    private ConnectSocket connectsocket; 
    final Context context = this; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_tab_exercise); 

     TabHost tabHost = getTabHost(); 
     connectsocket= new ConnectSocket(this); 
     connectsocket.start(); 
     // connect=new StartSocket(); 
     mContext=TabExercise.this; 
     // Tab for Contacts 
     TabSpec contacts = tabHost.newTabSpec("Contacts"); 
     // setting Title and Icon for the Tab 
     contacts.setIndicator("Contacts", getResources().getDrawable(R.drawable.ic_launcher)); 
     Intent contactsIntent = new Intent(this, Contacts.class); 
     contacts.setContent(contactsIntent); 

     // Tab for Songs 
     TabSpec songspec = tabHost.newTabSpec("Notifications");   
     songspec.setIndicator("Notifications", getResources().getDrawable(R.drawable.ic_launcher)); 
     Intent songsIntent = new Intent(this, Notifications.class); 
     songspec.setContent(songsIntent); 

     // Tab for Videos 
     TabSpec videospec = tabHost.newTabSpec("Messages"); 
     videospec.setIndicator("Messages", getResources().getDrawable(R.drawable.ic_launcher)); 
     Intent videosIntent = new Intent(this, MyMessages.class); 
     videospec.setContent(videosIntent); 

     // Adding all TabSpec to TabHost 
     tabHost.addTab(contacts); // Adding photos tab 
     tabHost.addTab(songspec); // Adding songs tab 
     tabHost.addTab(videospec); // Adding videos tab 

     TabWidget widget = tabHost.getTabWidget(); 
     for(int i = 0; i < widget.getChildCount(); i++) { 
      View v = widget.getChildAt(i); 

      // Look for the title view to ensure this is an indicator and not a divider. 
      TextView tv = (TextView)v.findViewById(android.R.id.title); 
      if(tv == null) { 
       continue; 

      } 
      v.setBackgroundResource(R.layout.tab_indicator_holo); 
     } 
     Intent in= getIntent(); 
     String tabSel=in.getStringExtra("tab_index"); 
     //Bundle extras = in.getExtras(); 
     if (tabSel != null) { 

      if(tabSel.equals("1")){ 
       tabHost.setCurrentTab(Integer.valueOf(tabSel)); 
      } 
      if(tabSel.equals("2")){ 
       tabHost.setCurrentTab(Integer.valueOf(tabSel)); 
      } 
     } 
    } 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.home, menu); 
    // SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    // SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); 
     // Assumes current activity is the searchable activity 
    // searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
     //searchView.setInputType(InputType.TYPE_CLASS_TEXT); 
    // searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default 
     return true; 
    } 

    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.logout: 
       // app icon in action bar clicked; go home 
       SaveSharedPreference.setUserName(TabExercise.this,""); 
       SaveSharedPreference.setName(TabExercise.this,""); 
       SaveSharedPreference.setUserId(TabExercise.this,""); 
       Intent intent = new Intent(this, MainActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(intent); 
       return true; 

      case R.id.find_a_user: 
       // app icon in action bar clicked; go home 

       Intent in = new Intent(this, Contacts.class); 

       startActivity(in); 
       return true; 

      case R.id.editprofile: 

       Intent inte=new Intent(this, EditProfile.class); 
       startActivity(inte); 
       return true; 
      case R.id.chat: 
       Intent inti = new Intent(this, StartChat.class); 
       startActivity(inti); 
       return true; 

      case R.id.delete_all: 
       //connect.start(); 
       Intent inten=new Intent(TabExercise.this, CreateNotification.class); 
       startActivity(inten); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    @Override 
    public void callback(JSONArray data) throws JSONException { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void on(String event, JSONObject data) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void onMessage(String message) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void onMessage(JSONObject json) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void onConnect() { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void onDisconnect() { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void onConnectFailure() { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void onMessageReceived(Message m) { 
     // TODO Auto-generated method stub 
     Log.d("Status","This is the status "+m.status); 
     if(m.status.equals("ready")){ 
      connectsocket.login(SaveSharedPreference.getName(TabExercise.this), SaveSharedPreference.getUserId(TabExercise.this)); 
      connectsocket.subscribe(); 
     } 
     if(m.status.equals("call")){ 
      Intent intent=new Intent(TabExercise.this, StartCall.class); 
      startActivity(intent); 
     } 
     if(m.status.equals("message")){ 
      System.out.println("Received a message "+m.msg+" and a name "+m.name); 
      final String name=m.name; 
      final String pid=m.pid; 
      final String msg=m.msg; 

      //Intent intenter=new Intent(TabExercise.this, CreateNotification.class); 
      //startActivity(intenter); 

      runOnUiThread(new Runnable(){ 

      public void run(){ 
      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        context); 
      alertDialogBuilder.setTitle(name+" just sent you a message"); 
      alertDialogBuilder.setMessage("Click yes to go to the message"); 
      alertDialogBuilder 

       .setCancelable(false) 
       .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 
         Intent inte=new Intent(TabExercise.this, Chat.class); 
         Bundle extras=new Bundle(); 
         extras.putString("name", name); 
         extras.putString("pid", pid); 
         extras.putString("msg", msg); 
         inte.putExtras(extras); 
         startActivity(inte); 

         dialog.cancel(); 
        } 
        }) 
       .setNegativeButton("No",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 
         // if this button is clicked, just close 
         // the dialog box and do nothing 
         dialog.cancel(); 
        } 
       }); 

       // create alert dialog 
       AlertDialog alertDialog = alertDialogBuilder.create(); 

       // show it 
       alertDialog.show(); 
      } 
      }); 
     } 
    } 

這裏是創建通知的通知活動。

public class CreateNotification extends Activity { 
    View viewer; 
    @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_create_notification); 
     Intent intent = new Intent(this, ReceiveNotification.class); 
     PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 

     // Build notification 
     // Actions are just fake 
     Notification noti = new Notification.Builder(this) 
      .setContentTitle("Received a message") 
      .setContentText("Subject").setSmallIcon(R.drawable.ic_launcher) 
      .setContentIntent(pIntent) 
      .addAction(R.drawable.ic_launcher, "Rply", pIntent) 
      .build(); 
     NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     // Hide the notification after its selected 
     noti.flags |= Notification.FLAG_AUTO_CANCEL; 

     notificationManager.notify(0, noti); 
     } 

    } 

回答

0

使用報警管理器定期觸發報警。收到警報時,運行後臺服務來查詢您的服務器是否收到任何消息。

或者,如果您希望服務器將消息直接推送到您的設備,則可以使用Google Cloud Messaging。

+0

ahhh有趣的,讓我看看。 –

相關問題