Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a new notification when push notification is received (not replace the previous)

I am using push notifications in my app. I need to display a notification when a push notification delivered. If I send another notification (without clearing the previous notification), it will replace the old notification.

This is the code I use:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

int icon = R.drawable.ic_launcher;
CharSequence tickerText = "New notification Pending";
long time = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, time);
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;

// Context context = getApplicationContext();
CharSequence contentTitle = "Notifications";
CharSequence contentText = newMessage;
Intent notificationIntent = new Intent(this, LoginActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
        contentIntent);
mNotificationManager.notify(1, notification);

But I don't want to replace the notification, I want to add it as a new notification.

like image 358
gouthaman93 Avatar asked Jun 17 '13 12:06

gouthaman93


People also ask

How do I stop my notifications from changing?

Step 1: Tap to open the Settings app. Step 2: Tap Sound & Notifications. Step 3: Tap App Notifications. Step 4: Tap to open an app and then tap the toggle next to Block to disable or enable its notifications.

Are push notifications the same as notifications?

The main difference between push notification and notification is that the latter are created internally from an application on the device that wants to show user some information, a reminder, some news or updates, and so on.


Video Answer


2 Answers

You can also use System.currentTimeMillis() to assign unique id to your notification.

int id = (int) System.currentTimeMillis();
mNotificationManager.notify(id, notification);
like image 84
Amrit Pal Singh Avatar answered Oct 17 '22 04:10

Amrit Pal Singh


You need to supply a different ID as the notification ID each time. The best approach would be to send an ID field to GCM which can be then accessed via Intent.getExtras().getInt() in your GCMIntentService's onMessage() method.

If this is not possible, I'd suggest using something like (int)Math.random()*10 to generate a random integer number as your notification ID. This will (partially) ensure that your notifications will not replace each other.

like image 11
ramdesh Avatar answered Oct 17 '22 04:10

ramdesh