Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android O - turn off notification channel and then turn on, the importance level always reset to medium

In Android O, we can have different notification channels with different priority level (Importance). In my code, I set the channel importance level to Urgent, that is "Make sound and pop on screen".

But when I go to the notification setting, turn off the channel notification, and then turn it on, the importance level will always reset to medium ("No sound"), which is the default level.

I know we can always change the importance level manually, but is there a way to have it remember the settings before the channel is turned off so that when it is turned on again, it will automatically recover to the settings it has before?

like image 485
laixiaoyuan Avatar asked Sep 12 '17 21:09

laixiaoyuan


1 Answers

It seems that there is not function for Notifications to remember your importance level. It is not hard to just save the default importance for your channel in your code.

This is my test:

I created a notifications channel with importance level as IMPORTANCE_HIGH at the beginning. Then turn off the notification either from the Settings or the long press on the notification pop up. The importance level becomes IMPORTANCE_NONE. When I turn on the notification again, it becomes IMPORTANCE_LOW.

The following is example code to check if the level has been decreased after you turn off and on your notification channel and set it to your original importance.

    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);
    }
like image 141
flame3 Avatar answered Oct 06 '22 06:10

flame3