A.N
A.N

Reputation: 97

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?

Upvotes: 2

Views: 686

Answers (2)

Sagar Bisht
Sagar Bisht

Reputation: 1

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
           
            NotificationChannel channel = notificationManager.getNotificationChannel(channelId);
            if (channel != null) {
                channel.setImportance(NotificationManager.IMPORTANCE_HIGH);
                notificationManager.createNotificationChannel(channel);
            }

Upvotes: 0

Amani
Amani

Reputation: 3979

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

Upvotes: 3

Related Questions