【发布时间】:2018-06-06 15:23:32
【问题描述】:
【问题讨论】:
标签: android android-notifications
【问题讨论】:
标签: android android-notifications
documentation 不是很清楚如何显示这个选项,只是有可能:
在 Android 8.0(API 级别 26)及更高版本上,用户还可以通过逐个频道覆盖“请勿打扰”来允许通过应用特定类别(也称为频道)的通知。 [...] 在运行 Android 7.1(API 级别 25)及更低版本的设备上,用户可以逐个应用而不是逐个通道地允许通知。
但根据我的测试,在 Android 8.0+ 上,只有将 importance 设置为 Urgent 的通知渠道才有此选项,对应于NotificationManager.IMPORTANCE_HIGH。有关创建频道的更多信息,请参阅Create a channel and set the importance。
在 Android 5.0 到 7.1 上,据说你必须使用setPriority()
在 Android 8.0(API 级别 26)及更高版本上,通知的重要性取决于发布通知的渠道的重要性。用户可以在系统设置中更改通知渠道的重要性(图 12)。在 Android 7.1(API 级别 25)及更低版本上,每个通知的重要性由通知的优先级决定。
所以我尝试使用NotificationCompat.PRIORITY_MAX,但在我还添加了system-wide category 之前,我没有看到“覆盖请勿打扰”选项,
类似:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Notification title")
.setContentText("Text content")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_ALARM);
现在,对于 Android 8.0+,要查看用户对您的频道应用了哪些设置,Read notification channel settings 建议使用来自getNotificationChannel() 的canBypassDnd():
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = manager.getNotificationChannel(CHANNEL_ID);
channel.canBypassDnd();
}
不幸的是,在 7.1 下似乎没有任何公共方法可以获取该信息; NotificationManagerCompat 唯一可用的是areNotificationsEnabled()。
【讨论】:
areNotificationsEnabled()