Add a new notification when push notification receives (not replace old one)

柔情痞子 提交于 2019-12-03 09:28:50

问题


I am using push notification on my app. I used to display a notification when a push notification comes, if I send another notification (without clearing the previous notification)it replaces the new 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 notification, I wanted to add it as a new notification.


回答1:


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.




回答2:


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

int id = (int) System.currentTimeMillis();
mNotificationManager.notify(id, notification);



回答3:


simple you have to

change Notification id

  mNotificationManager.notify(1, notification);

instead of 1

for more refer this Link




回答4:


Use a new notification ID every time instead of hardcoding to 1:

int i = x; //Generate a new integer everytime
mNotificationManager.notify(i, notification);



回答5:


I am not sure what your use case is, but the Android design guidelines recommend not doing it at all.

Don't:

Do:




回答6:


We need Unique notification id which will generate the new notifications.

Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

  @param id An identifier for this notification unique within your application.
  @param notification A {@link Notification} object describing what to show the user. 
  Must not be null.

public void notify(int id, Notification notification)
{
    notify(null, id, notification);
}

Example :

int  id =(int) System.currentTimeMillis();
            mNotificationManager.notify(id, notify);


来源:https://stackoverflow.com/questions/17148076/add-a-new-notification-when-push-notification-receives-not-replace-old-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!