Android: Manage multiple push notification in device of an app
D

5

15

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.

Downstage answered 4/4, 2014 at 9:8 Comment(0)
B
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

Bobodioulasso answered 4/4, 2014 at 9:19 Comment(1)
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.Teteak
I
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);
Innocency answered 24/12, 2015 at 9:21 Comment(0)
A
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

Acidimetry answered 4/4, 2014 at 9:24 Comment(0)
C
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. :)

Cobweb answered 18/9, 2014 at 5:8 Comment(0)
S
0
notificationManager.notify(Different_id_everytime, notification);
Somewise answered 28/4, 2017 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.