10

How does NotificationManagerCompat.areNotificationsEnabled() work? I have tried to do the following appPushEnabled = String.valueOf(NotificationManagerCompat.areNotificationsEnabled()); however it is giving me an error?

Also it says it only works on some devices do I need a try catch on it?

4
  • Well, the docs and published source are lacking currently, but I'd imagine it's not static, if that's what you mean by "giving me an error". You'd need to call it on an instance, which you can get with the static from(Context) method. Beyond that, AFAIK, it'll return true unconditionally for any API<19, but should work as expected above that. Commented Jun 24, 2016 at 15:02
  • 1
    Post the error that it throws Commented Jun 24, 2016 at 15:07
  • What is the error? Commented Jun 24, 2016 at 15:20
  • I dont get an error so much as I can't compile, it is just underlined in red Commented Jun 25, 2016 at 8:30

4 Answers 4

21

After testing for few hours, here is what I have found.

In App Gradle file, com.android.support:support should be minimum 24 and compileSdkVersion has to be 24

android { compileSdkVersion 24 } dependencies { compile 'com.android.support:support-v4:24.0.0' } 

And then @petey 's answer will be working

NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context); boolean areNotificationsEnabled = notificationManagerCompat.areNotificationsEnabled(); 
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this in my MainActivity's onCreate method. But context is unresolved. Where can I get context?
@UniSoundWaterloo you can change it to "this" if you implemented method in Activity
@UniSoundWaterloo If, you're trying the method under a listener or some closed constructor, then change this to YourActivity.this
9

Try using NotificationManagerCompat.from(Context context) method to get an instance of a NotificationManagerCompat object you may then be able to call areNotificationsEnabled() on.

NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context) boolean areNotificationsEnabled = notificationManagerCompat.areNotificationsEnabled(); String appPushEnabled = String.valueOf(areNotificationsEnabled); 

4 Comments

I am still getting an error cannot resolve method areNotifcationsEnabled()
it doesnt compile, in android studio it is underlined in red and says cannot resolve method areNotificationsEnabled
Ahh.. please check your dependencies (what version of support you are using) and api targets.
V4 how do I check api targets?
1

You should set your gradle with
compile "com.android.support:support-v4:24.0.0" minimum.

1 Comment

thanks, I am getting the following error This support library should not use a different version (24) than the compileSdkVersion (23) How do i edite the compileSdkVersion?
-2

NotificationManager Api

Looked up for the Api document, and I found that: the minimum api level should be 24.

1 Comment

use AndroidX so it will work with previous versions

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.