How to use getSystemService for Android notification channel

Viewed 21

So in the Android developer Docs (https://developer.android.com/training/notify-user/build-notification) it gives this line to create a notification channel:

NotificationManager notificationManager = getSystemService(NotificationManager.class);

But with the exact code block copied I get the error:

cannot find symbol method getSystemService(Class<NotificationManager>)

I think that's a method but wasn't called from it's class (Context)? But then why is in the docs? What should I put instead?

1 Answers

But then why is in the docs?

The docs assume that this line of code is in a method of some implementation of Context, such as an Activity or Service.

What should I put instead?

Either:

  • Move this code into a method of some implementation of Context, such as an Activity or Service, or

  • Call getSystemService() on some instance of Context

We cannot give you more specific advice without knowing more about where you are attempting to use that line of code.

Related