2016-08-19 241 views
5

我正在Android上實施推送通知。問題出現在我想更新通知時。我想堆疊通知,這樣如果通知可見,我只需更新它並執行此操作。更新推送通知Android

mNotifyBuilder.setContentText(currentText) 
    .setNumber(++numMessages); 

但每當我收到通知++ numMessages被設置回0之前總結。如果屏幕上出現通知,我需要從中得出結論。下面是代碼:

//Class is extending GcmListenerService 
public class GCMPushReceiverService extends GcmListenerService { 

    //variables for message 1,on receiving job applications 
    String name; 
    String lastname; 
    String jobtypename; 
    String kasualjobdatetimepostedat; 
    String kasualjobcount; 
    int numMessages; 

    //This method will be called on every new message received 
    @Override 
    public void onMessageReceived(String from, Bundle data) { 
     //Getting the message from the bundle 
     String message = data.getString("message"); 
     String messageid = data.getString("messageid"); 

     if(messageid.compareTo("1")==0){ 
      name=data.getString("firstname"); 
      lastname=data.getString("lastname"); 
      jobtypename=data.getString("jobtype"); 
      kasualjobdatetimepostedat=data.getString("kasualjobdatetimepostedat"); 
     } 
     else 
     { 
      kasualjobcount=data.getString("kasualjobcount"); 
     } 
      //Displaying a notification with the message 
     sendNotification(message, messageid); 
    } 

    //This method is generating a notification and displaying the notification 
    private void sendNotification(String message,String messageid) { 
     Intent intent = new Intent(this, Main_Activity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     int requestCode = 0; 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT); 
     Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     Log.d("notificationid", String.valueOf(messageid)); 
     if(messageid.compareTo("2")==0) {//messages to do with jobs around 2 
      String messageionkasualjobscount="There are "+kasualjobcount+" new jobs around you"; 
      NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this); 
      noBuilder.setSmallIcon(R.mipmap.ic_launcher) 
        .setContentTitle("KasualJobs2") 
        .setContentText(messageionkasualjobscount) 
        .setAutoCancel(true) 
        .setContentIntent(pendingIntent).setSound(Settings.System.DEFAULT_NOTIFICATION_URI); 

      NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      notificationManager.notify(2, noBuilder.build()); //messageid = ID of notification 
     }else{//messages to with users applying for job 1 

      String messageionkasualjobapplication=name+ " "+ lastname+" has applied for the "+jobtypename+" you posted on "+kasualjobdatetimepostedat; 
      NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this); 
      noBuilder.setSmallIcon(R.mipmap.ic_launcher) 
        .setContentTitle("KasualJobs1") 
        .setContentText(messageionkasualjobapplication) 
        .setAutoCancel(true).setNumber(++numMessages) 
        .setContentIntent(pendingIntent).setSound(Settings.System.DEFAULT_NOTIFICATION_URI); 

      NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      notificationManager.notify(1, noBuilder.build()); //messageid = ID of notification 

     } 
    } 

} 

回答

2

一般情況下,你不能指望你的字段值在多個調用堅持到onMessageReceived,因爲服務可能已被殺害OS以釋放內存。

我建議將您的郵件存儲在數據庫中。無論何時您收到新消息,請將其插入數據庫,並在用戶讀取時刪除它。然後,您可以輕鬆查詢有多少條未讀消息。

0

我建議你使用FCM而不是GCM(因爲GCM將不再使用)。 並更新通知使用sqlite數據庫存儲日期爲&時間的通知。讀取時清除通知&也將其從數據庫中刪除。