Android显示不重复通知的Notification
在Android开发中经常会用到Notification来展示通知,但是之前写出来的代码中一个APP只能有一个通知,有新的通知的时候自己会覆盖之前的通知,一直不知道为什么,好,话不多说,先贴我之前的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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); } |
这几天,看了Android关于Notification的源码,发现了notify函数的一段说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * 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); } |
在这里可以看到,Notification中的第一个参数id就是每一个通知的id,按照常理来推断,只要id一样,自然就会覆盖,既然这样,那就用时间戳来代替上面的写法好啦,新代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | private void showNotification(String title, Context context) { int requestCode = ( int ) System.currentTimeMillis(); NotificationManager notificationManager = (NotificationManager) context .getSystemService(android.content.Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "90上门洗车" , 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上门洗车" , title, contentIntent); notificationManager.notify(requestCode, notification); } |
在这里,我把notify()里面的id参数变成了时间戳,编译,运行,好像成功了呢,这样,就可以显示不同的通知啦~
= = 虽然成功了,但是在源码里面可以看到,还有一种重载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /** * 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) { } } |
这个,才是之前的notifiy()方法调用的真实的函数,不过就是tag在之前的调用中被设置成了null,在这段代码的注释中说道tag和id都是可以拿来区分不同的通知的,我们可以把这两个混起来使用,实现类似于qq一样的效果
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步