有的手机在添加channel后仍然无法弹出通知。追踪logcat发现有这么一句:
E/NotificationService: Suppressing notification from package by user request.
用户请求抑制此通知?追踪notify源码找到NotificationManagerService的enqueueNotificationInternal(......)方法:
void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid,final int callingPid, final String tag, final int id, final Notification notification,int incomingUserId) {
...
if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r)) {return;}
...
mHandler.post(new EnqueueNotificationRunnable(userId, r));
}
private boolean checkDisqualifyingFeatures(int userId, int callingUid, int id, String tag,NotificationRecord r) {
...// blocked apps
if (isBlocked(r, mUsageStats)) {return false;}
return true;
}
protected boolean isBlocked(NotificationRecord r, NotificationUsageStats usageStats) {
final String pkg = r.sbn.getPackageName();
final int callingUid = r.sbn.getUid();
final boolean isPackageSuspended = isPackageSuspendedForUser(pkg, callingUid);
if (isPackageSuspended) {
Slog.e(TAG, "Suppressing notification from package due to package suspended by administrator.");
usageStats.registerSuspendedByAdmin(r);
return isPackageSuspended;
}
final boolean isBlocked = mRankingHelper.getImportance(pkg, callingUid) == NotificationManager.IMPORTANCE_NONE
|| r.getChannel().getImportance() == NotificationManager.IMPORTANCE_NONE;
if (isBlocked) {
Slog.e(TAG, "Suppressing notification from package by user request.");
usageStats.registerBlocked(r);
}
return isBlocked;
}
最终的isBlocked判断条件满足,导致notify操作被中断return。