Android GCM multiple push notifications with one i

2019-05-30 02:08发布

问题:

My app wants to bundle multiple push notifications in one icon in the status bar.

When clicking on the icon, the multiple notifications should be received by the app to show them in listview mode.

There are already some entries in stackoverflow which come close to what I want to obtain and it did give me a better insight in handling pending intents and notification flags but they didn´t solve completely my problem.

First step: Creating the notification:

Following some entries in stackoverflow, I made the following assumptions:

  • One notification ID (notifyID) to obtain only one icon in status bar
  • A unique requestId parameter in the pending intent to differentiate between the various notifications requests within the same notification ID
  • FLAG_ACTIVITY_NEW_TASK used in notification intent and FLAG_UPDATE_CURRENT used in pending intent

     Notification notification;
     int icon = R.drawable.ic_launcher;
     int smallIcon = R.drawable.ic_launcher;
     int notifyID = 1;
     long when = System.currentTimeMillis();
     int requestID = (int) System.currentTimeMillis();
    
     NotificationManager notificationManager = (NotificationManager)
     context.getSystemService(Context.NOTIFICATION_SERVICE);   
    
     Intent notificationIntent = new Intent(context, NewActivity.class);
    
     notificationIntent.putExtra("new_message", message);
    
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
     PendingIntent contentIntent = PendingIntent.getActivity(context, requestID,    
       notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
     int numMessages = 1;
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
     mBuilder.setNumber(numMessages)
        .setSmallIcon(smallIcon)
        .setAutoCancel(true)
        .setContentTitle("You have " +numMessages+ " new messages.")
        .setContentText(message)
        .setWhen(when)
        .setContentIntent(contentIntent)
            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
    
     notificationManager.notify(notifyID, mBuilder.build());
    

The way I see it, each time a notification is sent by GCM, my app generates a notification to send to the notification manager taking into account the previous assumptions.

First problem: if I want to notify the user with the number of pending notifcations left, how can I get track of the previous number of sent notifications? Do I have to store it in storage media?

Second step: Tapping the notification icon in status bar containing multiple notifications:

   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    showNotifications();
   }

   public void onResume() {
    showNotifications();
    super.onResume();
   }


   public void showNotifications() {

   if (getIntent().getExtras() != null) {
    if (getIntent().getExtras().getString("new_message") != null) {
    String newMessage = getIntent().getExtras().getString("new_message");
    if (!newMessage.equals("")) {
           handleMessage(newMessage);
       getIntent().removeExtra("new_message);
    }
    }
   }
   }

   public void onNewIntent(Intent intent) {
   Bundle extras = intent.getExtras();
   if (extras != null) {
         if (intent.getExtras().getString("new_message") != null) {
         String newMessage = intent.getExtras().getString("new_message");
         if (!newMessage.equals("")) {
           intent.removeExtra("new_message");       }
         }
         }
   }
   }

Second problem: I only receive the last notification sent. It seems to be that differentiating the pending intents with requestId didn´t do the trick. Also I thought that the different pending intents related to one notification icon would be handled by onNewIntent... A possible solution that I was thinking about is to save incoming notifications from GCM in storage and get them on tapping the status bar icon, but for sure this cannot be the way Google meant it to be…

¿Any help?

回答1:

for second step try this: PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);