前言
定制系统一般都会要求状态栏左上角只显示固定的通知消息,避免预装其它app乱发通知消息
解决办法
找到 SystemUI 中控制消息现实的地方,将其拦截
frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\NotificationListener.java
private boolean isNeedAddNotification(String pkgName){ java.util.List<String> pkgList = new java.util.ArrayList<String>(); pkgList.clear(); pkgList.add("android"); return pkgList.contains(pkgName); } @Override public void onNotificationPosted(final StatusBarNotification sbn, final RankingMap rankingMap) { if (DEBUG) Log.d(TAG, "onNotificationPosted: " + sbn); //cczheng add for notificationarea only show pstn and android icon if (sbn != null){ String pkgName = sbn.getOpPkg(); if (!isNeedAddNotification(pkgName)) { Log.d("NotificationListener","onNotificationPosted forbidden "+pkgName); return; } }//end if (sbn != null && !onPluginNotificationPosted(sbn, rankingMap)) { Dependency.get(Dependency.MAIN_HANDLER).post(() -> { processForRemoteInput(sbn.getNotification(), mContext); String key = sbn.getKey(); boolean isUpdate = mEntryManager.getNotificationData().get(key) != null; // In case we don't allow child notifications, we ignore children of // notifications that have a summary, since` we're not going to show them // anyway. This is true also when the summary is canceled, // because children are automatically canceled by NoMan in that case. if (!ENABLE_CHILD_NOTIFICATIONS && mGroupManager.isChildInGroupWithSummary(sbn)) { if (DEBUG) { Log.d(TAG, "Ignoring group child due to existing summary: " + sbn); } // Remove existing notification to avoid stale data. if (isUpdate) { mEntryManager.removeNotification(key, rankingMap, UNDEFINED_DISMISS_REASON); } else { mEntryManager.getNotificationData() .updateRanking(rankingMap); } return; } if (isUpdate) { mEntryManager.updateNotification(sbn, rankingMap); } else { Log.d("NotificationListener", "addNotification: " + sbn); mEntryManager.addNotification(sbn, rankingMap); } }); } } @Override public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap, int reason) { if (DEBUG) Log.d(TAG, "onNotificationRemoved: " + sbn + " reason: " + reason); //cczheng add for notificationarea only show pstn and android icon if (sbn != null){ String pkgName = sbn.getOpPkg(); if (!isNeedAddNotification(pkgName)) { Log.d("NotificationListener","onNotificationPosted forbidden "+pkgName); return; } }//end if (sbn != null && !onPluginNotificationRemoved(sbn, rankingMap)) { final String key = sbn.getKey(); Dependency.get(Dependency.MAIN_HANDLER).post(() -> { mEntryManager.removeNotification(key, rankingMap, reason); }); } } @Override public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) { onNotificationRemoved(sbn, rankingMap, UNDEFINED_DISMISS_REASON); }
这里只保留 android 进程发送过来的通知,其余一律禁止显示,这里为了演示做的简陋,采用了list存储,
可根据需求增加 Provider 来增删改,来看下log
D/NotificationListener: onNotificationPosted: StatusBarNotification(pkg=android user=UserHandle{-1} id=26 tag=null key=-1|android|26|null|1000: Notification(channel=DEVELOPER pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x2 color=0xff607d8b vis=PUBLIC)) D/NotificationListener: addNotification: StatusBarNotification(pkg=android user=UserHandle{-1} id=26 tag=null key=-1|android|26|null|1000: Notification(channel=DEVELOPER pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x2 color=0xff607d8b vis=PUBLIC)) D/NotificationListener: onNotificationPosted: StatusBarNotification(pkg=com.android.dyboot user=UserHandle{0} id=4403 tag=null key=0|com.android.dyboot|4403|null|10069: Notification(channel=a3845e9cfc7d494593e892990b7c7bec pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x22 color=0x00000000 vis=PRIVATE)) D/NotificationListener: onNotificationPosted forbidden com.android.dyboot