代码之家  ›  专栏  ›  技术社区  ›  hgoebl

查明通知通道是否被用户禁用

  •  0
  • hgoebl  · 技术社区  · 7 年前

    在我的android应用程序中,有一个长期运行的服务(导出数据),它显示进度并以通知用户共享导出的文件结束。很明显,这不是推送通知,而是 Notification 本地创建者 new NotificationCompat.Builder(…).set…().build() .

    有相当多的用户会禁用通知频道(或全局禁用我的应用程序的通知)。我不知道为什么,因为它真的不是很冗长,顺便说一句。但是那些用户觉得这个应用程序不起作用。如果通知被静音,我想警告他们。

    我能提前知道 NotificationChannel 是否被用户禁用?

    我只发现 NotificationManager.areNotificationsEnabled() 但我不确定它是否还有其他用途。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Nitesh    7 年前

    我相信您可以使用以下两种方法的组合来检测特定通道或整个包的通知是否被阻止。

    从官方文件

    isBlocked()

    公共布尔值被阻止()
    返回是否阻止发布到属于此组的频道的通知。此值独立于NotificationManager.AreNotificationsenabled()和NotificationChannel.GetImportance()。

    areNotificationsEnabled()

    已启用公共布尔arnotifications()
    返回是否阻止来自调用包的通知。

        2
  •  1
  •   marc_s MisterSmith    7 年前

    要检索通知通道,我们可以调用方法 getNotificationChannel() NotificationManager .

    我们需要通过相关频道的频道ID。

    另外,要检索所有 NotificationChannels 我们可以调用方法 getNotificationChannels() .

    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        List<NotificationChannel> notificationChannels = notificationManager.getNotificationChannels();
    }
    

    现在使用通道id从列表中检索该通道的属性。 例如:如果我创建了ID为“备份警报”的通道

    然后我要检查那个频道的属性。

    如果需要提示用户调整该频道的设置:可以使用如下意图从应用程序本身触发一个意图:

    Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
    intent.putExtra(Settings.EXTRA_CHANNEL_ID, notificationChannel.getId());
    /*intent.putExtra(Settings.EXTRA_CHANNEL_ID, "Backup alerts");*/
    /* Above line is example   */
    intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
    startActivity(intent);
    

    我们传递频道ID和应用程序包名称。这将特别打开特定的通道id。

    希望这对你有帮助;)