14

I am developing an application in which i implemented Push notification functionality. My code of onMessage - GCMIntentService.java is:

@Override
protected void onMessage(Context context, Intent data) {
    String message = data.getExtras().getString("message");

    displayMessage(context, message);
    generateNotification(context, message);
}

And code of generateNotification -

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, GCMMessageView.class);

    notificationIntent.putExtra("message", message);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);

    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);
}

This code is working fine. I am getting push message and notification.

=>But when i send more then one message the notification gets over write. And i am able to see only last notification in status bar.

The need of application is that - if i send more then one notification then all should be display in status bar.

So please guide me what should i do for that in my code.

Manoj Fegde
  • 4,786
  • 15
  • 50
  • 95

5 Answers5

10

Try to put different notification id (i.e. nid) in intent and notify instead of 0 for each new notification this will prevent over writing your notification

  PendingIntent intent = PendingIntent.getActivity(context, nid,notificationIntent, 0);

and

notificationManager.notify(nid, notification);

Hope this will help you

Systematix Infotech
  • 2,345
  • 1
  • 14
  • 31
  • You saved my day. When several push notifications were delivered, only last pending intent was executed for every notification. After I changed 0 to notificationId, it has fixed. – CoolMind Oct 31 '19 at 12:20
4

Get random numbers:

Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;

Instead of:

notificationManager.notify(0, notification);

use:

notificationManager.notify(m, notification);
zero323
  • 322,348
  • 103
  • 959
  • 935
Rooban
  • 248
  • 1
  • 15
2

You need to update your Notification ID when you fire a Notification. in

notificationManager.notify(ID, notification);

How?

To set up a notification so it can be different, issue it with a notification ID by calling NotificationManager.notify(ID, notification). To change this notification once you've issued it, create a NotificationCompat.Builder object, build a Notification object from it, and issue the Notification with the Different ID you are not used previously.

If the previous notification is still visible, the system updates it from the contents of the Notification object. If the previous notification has been dismissed, a new notification is created instead.

For more information go to official docs

M D
  • 47,665
  • 9
  • 93
  • 114
2

hello you need to pass unique notification id in notify method. Below is the code for pass unique notification id:

//"CommonUtilities.getValudeFromOreference" is the method created by me to get value from savedPreferences.
String notificationId = CommonUtilities.getValueFromPreference(context, Global.NOTIFICATION_ID, "0");
int notificationIdinInt = Integer.parseInt(notificationId);

notificationManager.notify(notificationIdinInt, notification);

// will increment notification id for uniqueness
notificationIdinInt = notificationIdinInt + 1;
CommonUtilities.saveValueToPreference(context, Global.NOTIFICATION_ID, notificationIdinInt + "");
//Above "CommonUtilities.saveValueToPreference" is the method created by me to save new value in savePreferences.

Let me know if you need more detail information or any query. :)

Gaurav Darji
  • 488
  • 5
  • 12
0
notificationManager.notify(Different_id_everytime, notification);
Rob
  • 26,989
  • 16
  • 82
  • 98
Makvin
  • 3,475
  • 27
  • 26