0

不久前,我更新了我的应用程序以添加对 Android 8+ 的支持,在更新通知以使用所需的NotificationChannel后,我注意到我失去了一项功能。
该应用程序使用计时器通知,当时我能够更新通知优先级,使用PRIORITY_HIGH显示通知,因此通知被“推送”给用户,然后将通知优先级编辑为PRIORITY_LOW,以隐藏通知。

在阅读了文档后,据我所知,创建 NotificationChannel 后我无法控制优先级,将此控制权留给用户,但是,要求用户编辑通知设置对于我的使用而言并不是最佳选择。

相关代码:

//Creating the notification channel (while setting up the app)
NotificationChannel channel = new NotificationChannel(channelId, channelTitle, NotificationManager.IMPORTANCE_HIGH);

//Editing the NotificationChannel to lower the Notification priority (not working)
NotificationChannel channel = notificationManager.getNotificationChannel(channelId);
channel.setImportance(NotificationManager.IMPORTANCE_LOW);     
notificationManager.createNotificationChannel(channel);

有没有办法在仍然使用 NotificationChannels 的同时完成这种行为?

4

1 回答 1

0

事实证明我对此(很多)想得太多,只需 在NotificationBuilder上添加选项setOnlyAlertOnce,同时仍使用NotificationManager.IMPORTANCE_HIGH设置通道,将导致预期的行为,如下所示:

NotificationCompat.Builder notificationBuilder = 
    new NotificationCompat.Builder(context, channelId).setOnlyAlertOnce(true);
于 2019-01-03T12:18:52.193 回答