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

问题描述 投票:6回答:1

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

但是当我进入通知设置,关闭频道通知,然后将其打开时,重要性级别将始终重置为中等(“无声音”),这是默认级别。

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

android android-notifications android-8.0-oreo
1个回答
1
投票

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

这是我的测试:

我在开头创建了一个重要级别为IMPORTANCE_HIGH的通知渠道。然后从“设置”或长按通知弹出窗口关闭通知。重要性级别变为IMPORTANCE_NONE。当我再次打开通知时,它变为IMPORTANCE_LOW。

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

    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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.