android-o上对通知做了更细粒度的管理。根据app的业务场景将通知分类。用户可以在设置中选择接收自己感兴趣的通知。同时也可以屏蔽不喜欢的通知。先上图看看新特性。
入口:手机桌面长按(android 原生8.0)app图标,右上角出现提示“应用信息”,点击进入应用详情。如下图一:我们可以看见”应用通知“
点击应用通知进入应用通知管理详情页,如下图二中类别所示:本次测试创建了三种NotificationChannel:下载,聊天,推送。每种NotificationChannel设置了不同的属性,如:重要性,是否振动等等
点击图二中的下载NotificationChannel,进入NotificationChannel详情页,如图三,可以看到下载NotificationChannel的详细信息
总之,android-o上对通知的管理更加细粒度,通过”管道“对通知分类,用户可以根据自己对通知的喜好选择接收特定管道的通知。
二,从源码分析
一,关键类
1.NotificationManagerService
NotificationManagerService继承SystemService,是系统级别的service.
NotificationManagerService在init初始化的时候会加载/data/system/notification_policy.xml中的通知信息到内存数据结构,该文件中保存所有package注册的所有NotificationChannel。如下内容为xml文件部分内容。可以看到不同package下创建的channel以及channel相关属性。
</package>
<package name="com.android.providers.downloads" show_badge="true" uid="1010013">
<channel id="active" name="下载中" importance="2" sound="content://settings/system/notification_sound" usage="5" content_type="4" flags="0" show_badge="true" />
<channel id="complete" name="下载完成" importance="2" sound="content://settings/system/notification_sound" usage="5" content_type="4" flags="0" show_badge="true" />
<channel id="waiting" name="等待中" importance="2" sound="content://settings/system/notification_sound" usage="5" content_type="4" flags="0" show_badge="true" />
<channel id="insufficient_space" name="空间不足" importance="2" sound="content://settings/system/notification_sound" usage="5" content_type="4" flags="0" show_badge="true" />
</package>
<package name="com.android.providers.downloads" show_badge="true" uid="10013">
<channel id="active" name="下载中" importance="2" sound="