Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app - detect if app push notification is off

Tags:

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 !

like image 358
user1402897 Avatar asked Jul 05 '16 08:07

user1402897


People also ask

How do I know if push notifications are on?

Turn on notifications for Android devicesTap More on the bottom navigation bar and select Settings. Tap Turn on notifications. Tap Notifications. Tap Show notifications.

How can I see notifications when screen is off?

Open your phone's Settings app. Notifications. Under "Lock screen," tap Notifications on lock screen or On lock screen. Choose Show alerting and silent notifications.

How do I get notifications when an app is closed Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

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
}
like image 185
ljtomev Avatar answered Oct 04 '22 22:10

ljtomev