Android: new information replace old information in notification

北慕城南 提交于 2019-12-10 10:32:46

问题


From the documenation of the NotificationManager in Android:

public void notify (int id, Notification notification) 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.

it will be replaced by the updated information.

I don't want the old information to be replaced, I want both notifications. NB: each notification has its own id:

notificationManager.notify(0, notification);
notificationManager.notify(1, notification);

How to do this?


回答1:


Stack your notifications

If your app creates a notification while another of the same type is still pending, avoid creating an altogether new notification object. Instead, stack the notification.

A stacked notification builds a summary description and allows the user to understand how many notifications of a particular kind are pending.

http://developer.android.com/design/patterns/notifications.html




回答2:


public void notify (String tag, int id, Notification notification)

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

Parameters tag A string identifier for this notification. May be null. id An identifier for this notification. The pair (tag, id) must be unique within your application. notification A Notification object describing what to show the user. Must not be null.




回答3:


Try this one:

private void notifyMe(String message) {
        NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder ncomp = new NotificationCompat.Builder(this);
        ncomp.setContentTitle(getResources().getString(R.string.notification_title));
        ncomp.setContentText(message);
        ncomp.setTicker(getResources().getString(R.string.notification_ticker));
        ncomp.setSmallIcon(R.drawable.ic_launcher);
        ncomp.setAutoCancel(true);      
        //nManager.notify((int) System.currentTimeMillis(), ncomp.build());
        nManager.notify(int(System.currentTimemillisec
), ncomp.build());
    }


来源:https://stackoverflow.com/questions/11374986/android-new-information-replace-old-information-in-notification

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