Android app - detect if app push notification is off

15,969

Solution 1

You can check if the user is allowing notifications for your application with this command:

NotificationManagerCompat.from(context).areNotificationsEnabled()

This one-liner works from API level 19+. However, starting with android O, notification channels are introduced. This allows the user to disable only particular notification channels from the application settings screen and also disable all notifications from the app. With the command above, you can only check if the notifications are allowed or not for the whole app, not specific channels. Meaning, you cannot see any notifications even tho the command above gives a value of true

The code below returns true only if all notifications are allowed for the application and also all of the existing notification channels are enabled. Works from API level 19+ including the changes needed starting from Android O:

Java

public boolean areNotificationsEnabled() {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (!manager.areNotificationsEnabled()) {
      return false;
    }
    List<NotificationChannel> channels = manager.getNotificationChannels();
    for (NotificationChannel channel : channels) {
      if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
        return false;
      }
    }
    return true;
  } else {
    return NotificationManagerCompat.from(context).areNotificationsEnabled();
  }
}

Kotlin

fun areNotificationsEnabled(notificationManager: NotificationManagerCompat) = when {
    notificationManager.areNotificationsEnabled().not() -> false
    Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
        notificationManager.notificationChannels.firstOrNull { channel ->
            channel.importance == NotificationManager.IMPORTANCE_NONE
        } == null
    }
    else -> true
}

Solution 2

According to the documentation, the 1st is possible. I haven't looked into the second part of your question (taking the user to the notification settings).

To check current status of notifications, you first have to know if the device you're on is below Oreo or not. Below Oreo, it's as simple as calling areNotificationsEnabled() on the Support Library's NoticificationManagerCompat object (available as of version 24.1.0). On Oreo or above, you need to check per notification channel by calling getImportance() on a NotificationChannel object. If notifications are disabled, getImportance() will return NotificationManager.IMPORTANCE_NONE. If it returns anything else, they're enabled. Here's some code that'll do the job:

public boolean areNotificationsEnabled(Context context, String channelId) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        if(!TextUtils.isEmpty(channelId)) {
            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel channel = manager.getNotificationChannel(channelId);
            return channel.getImportance() != NotificationManager.IMPORTANCE_NONE;
        }
        return false;
    } else {
        return NotificationManagerCompat.from(context).areNotificationsEnabled();
    }
}

Hope this helps!

Solution 3

Starting with Android O, notification groups were introduced AND starting with Android P, it was made possible to disable the entire group. This must be taken into account. See code snippet below with an example solution.

fun NotificationManagerCompat.areNotificationsFullyEnabled(): Boolean {
    if (!areNotificationsEnabled()) return false
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        for (notificationChannel in notificationChannels) {
            if (!notificationChannel.isFullyEnabled(this)) return false
        }
    }
    return true
}

@RequiresApi(Build.VERSION_CODES.O)
fun NotificationChannel.isFullyEnabled(notificationManager: NotificationManagerCompat): Boolean {
    if (importance == NotificationManager.IMPORTANCE_NONE) return false
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        if (notificationManager.getNotificationChannelGroup(group)?.isBlocked == true) return false
    }
    return true
}

You can call it from a fragment for example:

if (!NotificationManagerCompat.from(requireContext()).areNotificationsFullyEnabled()) {
            //todo notifications DISABLED
        }

Solution 4

You can check your system push is enable or disable by this command.

NotificationManagerCompat.from(context).areNotificationsEnabled()
Share:
15,969

Related videos on Youtube

user1402897
Author by

user1402897

Updated on September 16, 2022

Comments

  • user1402897
    user1402897 almost 2 years

    In our Android App (compiled using SDK23) the "push notification" setting is "on" by default after app installation. But of course the user can switch it "off" manually.

    2 Questions:

    From within the app, using the Android API, is it possible (how, in short general terms?):

    1. to check what the current status of the "push notification" setting is (on or off)?

    2. similar as we can redirect a user to the GPS device settings if GPS is "off", can we also redirect the user to the "push notification settings" if the setting is 'off", so that the user can then, if he wants, switch it back "on"?

    To our great surprise (maybe we are wrong? therefore we seek your opinion/confirmation here) it seems that neither "1" nor "2" above is possible???!!

    If we are wrong and it IS possible we appreciate a short "how, in short general terms" to achieve that.

    Thanks for you input !

    • Mike M.
      Mike M. almost 8 years
      If you mean Notifications in general, there's a new method to check enabled status that's supposed to be in the latest support library update, but last I'd heard, it hadn't been released yet, and I'm unable to check at the moment.
    • intellignt_idiot
      intellignt_idiot almost 8 years
      Check this post if it helps. Here some one asked a similar question
  • Florian Walther
    Florian Walther almost 6 years
    I would remove !TextUtils.isEmpty(channelId) and check `channel != null", because an invalid channel id string could also cause that
  • Fazal Hussain
    Fazal Hussain almost 5 years
    It is always returning IMPORTANCE_HIGH even when the notification is disabled from the settings
  • Alex
    Alex about 4 years
    @FlorianWalther btw, TextUtils.isEmpty() checks for both null and length==0
  • Ganesh kumar Raja
    Ganesh kumar Raja over 3 years
    manager.getNotificationChannel(channelId); it retuerns NULL
  • Innova
    Innova about 3 years
    As a Kotlin extension function: fun NotificationManagerCompat.areNotificationsEnabled() = when { areNotificationsEnabled().not() -> false Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> { notificationChannels.firstOrNull { channel -> channel.importance == NotificationManager.IMPORTANCE_NONE } == null } else -> true }
  • Victor Rendina
    Victor Rendina over 2 years
    Don't forget to check if the group is disabled if api level is >= 28.
  • hanswim
    hanswim over 2 years
    In the block of >=Android O, code could be more simple: notificationManager.notificationChannels.none { it.importance == NotificationManager.IMPORTANCE_NONE }