Search code examples
javaandroid

Android notification importance cannot be changed


Code Summary:

NotificationCompat.Builder notification;
 NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
...
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_TITLE, importance);
manager.createNotificationChannel(channel);
notification.setChannelId(CHANNEL_ID);
manager.notify(notificationId, 0, notification.build());

manager.getImportance() returns always 3(IMPORTANCE_DEFAULT). How can I change importance?


Solution

  • Once you submit the channel to the NotificationManager, you cannot change the importance level. However, the user can change their preferences for your app's channels at any time.

    https://developer.android.com/training/notify-user/channels

    The solution is to guide user to notification settings and allow him to modify your channel. To open a channel in android settings there is an intent:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
          final Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
          intent.putExtra(Settings.EXTRA_APP_PACKAGE, getActivity().getPackageName());
          intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelID);
          if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
              startActivity(intent);
          } else {
              Toast.makeText(getActivity(), R.string.not_found, Toast.LENGTH_LONG).show();
          }
    }
    

    Another solution is to create a new channel and use that channel but user always sees all channels that you have created in Android Settings