Kenenisa Bekele
Kenenisa Bekele

Reputation: 845

Enable notification channels programmatically

I am able to check if the channel is enabled/disabled with

NotificationChannel channel = manager.getNotificationChannel(channelId);
boolean isEnabled = channel.getImportance() != IMPORTANCE_NONE;

But if the channel is not enabled then I would like to enable it by setting it to IMPORTANCE_HIGH

if (!isEnabled) {
channel.setImportance(NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
}

The problem is that the channel is not updated, it only works if I tried to disable it with IMPORTANCE_NONE in case it is enabled, but not the way around.

I have tried to delete the channel and create a new one with a different id, that works, but not creating a new one with the same id.

How to get around this?

Upvotes: 3

Views: 2935

Answers (2)

Egis
Egis

Reputation: 5141

When a notification channel is disabled, all you can do is ask the user to enable it. You can navigate the user directly to the channel settings screen.

fun openNotificationChannelSettings(context: Context, channelId: String) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
        intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
        intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId)
        context.startActivity(intent)
    }
}

Upvotes: 0

Jarvis
Jarvis

Reputation: 1802

You can't change channel importance programmatically without deleting the channel.

Because user may have changed the importance manually.

To achieve this programmatically to get channel and create new channel with new id. delete the old channel. Your changes will not reflect if create channel with previous id

for reference check WhatsApp application try to change the ringtone from application and see in channel at bottom left x channel is deleted message

Upvotes: 3

Related Questions