【问题标题】:NotificationChannel - How to show a notification for a while then hide it?NotificationChannel - 如何显示通知一段时间然后隐藏它?
【发布时间】:2019-01-02 17:50:42
【问题描述】:

不久前,我更新了我的应用程序以添加对 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?

【问题讨论】:

  • 也许您可以有两个具有不同优先级/重要性的通知渠道?
  • @JoeyDalu 是的,我想过,但是为此设置一个频道似乎有点奇怪,但是,如果这是唯一的选择,我可能不得不......哈哈

标签: android notifications notification-channel


【解决方案1】:

原来我想太多了(很多),只是添加了选项 NotificationBuilder 上的 setOnlyAlertOnce,虽然仍然使用 NotificationManager.IMPORTANCE_HIGH 来设置通道,但会产生预期的行为,如下所示:

NotificationCompat.Builder notificationBuilder = 
    new NotificationCompat.Builder(context, channelId).setOnlyAlertOnce(true);

【讨论】:

    猜你喜欢
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多