Reputation: 663
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?):
to check what the current status of the "push notification" setting is (on or off)?
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 !
Upvotes: 40
Views: 31153
Reputation: 246
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
}
Upvotes: 14
Reputation: 1093
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
}
Upvotes: 90
Reputation: 4199
You can check your system push is enable or disable by this command.
NotificationManagerCompat.from(context).areNotificationsEnabled()
Upvotes: 4
Reputation: 206
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!
Upvotes: 7