Limit exceed cannot create more channels error NotificationManager

111 Views Asked by At

I got the

java.lang.IllegalStateException: Limit exceed; cannot create more channels

When I check my code I see that I made a big mistake by using System.currentTimeMillis() for channel ID. So is there a way to delete the old created channels or another solution to solve the problem.

String channelId = AndroidVersionUtils.isOreoOrMore() ? getNewNotificationIdString() : context.getString(R.string.app_name);

    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel mChannel;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mChannel = new NotificationChannel(channelId, title, NotificationManager.IMPORTANCE_HIGH);
        mChannel.setLightColor(Color.GRAY);
        mChannel.enableLights(true);
        mChannel.setDescription(content);
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();
        mChannel.setSound(uri, audioAttributes);

        if (mNotificationManager != null) {
            mNotificationManager.createNotificationChannel(mChannel);
        }
    }



 private static String getNewNotificationIdString() {
        return ""+System.currentTimeMillis();
    }
1

There are 1 best solutions below

0
On

You can get the list of channels and delete them by id like in the code below.

notificationManager.notificationChannelsCompat.forEach {
    notificationManager.deleteNotificationChannel(it.id)
}