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

Android O-关闭通知通道,然后打开,重要性级别始终重置为中等

  •  7
  • laixiaoyuan  · 技术社区  · 8 年前

    在Android O中,我们可以使用不同优先级(重要性)的不同通知通道。在我的代码中,我将通道重要性级别设置为紧急,即“发出声音并在屏幕上弹出”。

    我知道我们总是可以手动更改重要性级别,但有没有办法让它在关闭频道之前记住设置,以便在再次打开频道时自动恢复到以前的设置?

    1 回复  |  直到 8 年前
        1
  •  3
  •   flame3    7 年前

    似乎没有通知功能来记住您的重要性级别。在代码中保存通道的默认重要性并不难。

    这是我的测试:

    以下是示例代码,用于检查在关闭通知通道并将其设置为原始重要性后,该级别是否已降低。

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel notificationChannel = notificationManager.getNotificationChannel("your_channel_id");
    
        int importance = notificationChannel.getImportance();
        if (importance < NotificationManager.IMPORTANCE_HIGH && importance > 0 ) {
            notificationChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
        }