系统会通过两个方法将通知上报给SystemUI的NotificationListener类:
@Override
public void onListenerConnected() {
if (DEBUG) Log.d(TAG, "onListenerConnected");
onPluginConnected();
final StatusBarNotification[] notifications = getActiveNotifications();
if (notifications == null) {
Log.w(TAG, "onListenerConnected unable to get active notifications.");
return;
}
final RankingMap currentRanking = getCurrentRanking();
mPresenter.getHandler().post(() -> {
for (StatusBarNotification sbn : notifications) {
mEntryManager.addNotification(sbn, currentRanking);
}
});
}
@Override
public void onNotificationPosted(final StatusBarNotification sbn,
final RankingMap rankingMap) {
if (DEBUG) Log.d(TAG, "onNotificationPosted: " + sbn);
if (sbn != null && !onPluginNotificationPosted(sbn, rankingMap)) {
//......省略部分代码......
}
}
通知显示流程图如下:
androidP通知显示流程图.png
如果我们要对一些通知进行屏蔽,可在NotificationData类的filterAndSort()方法中进行处理,即修改shouldFilterOut()方法。如代码所示屏蔽android包的USB channel通知。
public void filterAndSort() {
mSortedAndFiltered.clear();
synchronized (mEntries) {
final int N = mEntries.size();
for (int i = 0; i < N; i++) {
Entry entry = mEntries.valueAt(i);
if (shouldFilterOut(entry)) {
continue;
}
mSortedAndFiltered.add(entry);
}
}
Collections.sort(mSortedAndFiltered, mRankingComparator);
}
**
* @return true if this notification should NOT be shown right now
*/
public boolean shouldFilterOut(Entry entry) {
final StatusBarNotification sbn = entry.notification;
//屏蔽android包channelId为USB的通知.
try {
if(sbn.getPackageName().equals("android") && sbn.getNotification().getChannelId().equals("USB")){
Logger.d(TAG,"block notification channel-USB of package-android.");
return true;
}
}catch (Exception e){
e.printStackTrace();
//do nothing.
}
if (!(mEnvironment.isDeviceProvisioned() ||
showNotificationEvenIfUnprovisioned(sbn))) {
return true;
}
//......省略部分代码......
}