2016-08-19 15 views
5

Androidでプッシュ通知を実装しています。この問題は、通知を更新したいときに発生します。私は、通知を表示する場合、私は単にそれを更新し、これを行うように通知を積み重ねたいと思います。プッシュ通知の更新Android

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

しかし、通知を受け取るたびに、++ numMessagesは合計される前に0に設定されます。画面に通知があれば、それが残っていた場所から要約する必要があります。あなたはサービスがメモリを解放するOSによって殺されている可能性がありますので、あなたのフィールド値が、onMessageReceivedに複数の呼び出しにわたって持続する期待すべきではありません、一般的には

//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

:ここではコードです。

メッセージをデータベースに保存することをお勧めします。新しいメッセージを受信するたびに、メッセージをデータベースに挿入し、ユーザーがメッセージを読むと削除します。次に、未読メッセージの数を簡単に照会することができます。

0

GCMの代わりにFCMを使用することをおすすめします(GCMは使用されなくなるため)。 通知を更新するには、日付が&の通知を格納するsqliteデータベースを使用します。読み取り時に通知をクリアする&もデータベースから削除します。

関連する問題