Android displays Notification without repeated notifications, androidnotification
In Android development, Notification is often used to display notifications. However, in the Code previously written, only one Notification can be sent to an APP. When a new Notification is sent, the previous Notification will be overwritten, I have never known why. Well, I don't want to talk much about it. paste my previous code first.
private void showNotification(String title, Context context) { NotificationManager notificationManager = (NotificationManager) context .getSystemService(android.content.Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "XXX", System.currentTimeMillis()); notification.flags = Notification.FLAG_AUTO_CANCEL; notification.defaults |= Notification.DEFAULT_VIBRATE; notification.defaults |= Notification.DEFAULT_SOUND; notification.defaults |= Notification.DEFAULT_LIGHTS; notification.vibrate = new long[]{0, 100, 200, 300}; Intent intent = null; if (pushType == 1) { intent = new Intent(context, Advertisement.class); } else if (pushType == 2) { intent = new Intent(context, HomePage.class); } else if (pushType == 3) { intent = new Intent(context, OrderList.class); } PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); notification.setLatestEventInfo(context, "XXX", title, contentIntent); notificationManager.notify(111, notification); }
In the past few days, I have read the Android Notification source code and found a description of the notify function.
/** * 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); }
Here we can see that the first parameter id in Notification is the id of each Notification. It is inferred from common sense that, as long as the id is the same, it will naturally overwrite it, use timestamps instead of the preceding statement. The new Code is as follows:
Private void showNotification (String title, Context context) {int requestCode = (int) System. currentTimeMillis (); icationicationmanager notifmanager = (icationicationmanager) context. getSystemService (android. content. context. NOTIFICATION_SERVICE); Notification notification = new Notification (R. drawable. ic_launcher, "90 door-to-door car wash", System. currentTimeMillis (); notification. flags = Notification. FLAG_AUTO_CANCEL; notification. defaults | = Notification. DEFAULT_VIBRATE; notification. defaults | = Notification. DEFAULT_SOUND; notification. defaults | = Notification. DEFAULT_LIGHTS; notification. vibrate = new long [] {0,100,200,300}; Intent intent = null; if (pushType = 1) {intent = new Intent (context, Advertisement. class);} else if (pushType = 2) {intent = new Intent (context, HomePage. class);} else if (pushType = 3) {intent = new Intent (context, OrderList. class);} PendingIntent contentIntent = PendingIntent. getActivity (context, 0, intent, 0); notification. setLatestEventInfo (context, "90 door-to-door car wash", title, contentIntent); icationicationmanager. Y (requestCode, notification );}
Here, I changed the id parameter in notify () into a timestamp, compile, and run, as if it was successful. In this way, different notifications can be displayed ~
= Although it succeeded, you can see in the source code that there is also a reload
/** * 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. * * @param tag A string identifier for this notification. May be {@code null}. * @param id An identifier for this notification. The pair (tag, id) must be unique * within your application. * @param notification A {@link Notification} object describing what to * show the user. Must not be null. */ public void notify(String tag, int id, Notification notification) { int[] idOut = new int[1]; INotificationManager service = getService(); String pkg = mContext.getPackageName(); if (notification.sound != null) { notification.sound = notification.sound.getCanonicalUri(); if (StrictMode.vmFileUriExposureEnabled()) { notification.sound.checkFileUriExposed("Notification.sound"); } } if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")"); Notification stripped = notification.clone(); Builder.stripForDelivery(stripped); try { service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id, stripped, idOut, UserHandle.myUserId()); if (id != idOut[0]) { Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]); } } catch (RemoteException e) { } }
This is the real function called by the previous notifiy () method, but the tag is set to null in the previous call, in the comments of this Code, the tag and id can be used to distinguish different notifications. We can use these two together to achieve the same effect as qq.