8

I'm trying to handle notification with Android Oreo (SDK 27).

Here is my code creating the NotificationChannel:

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// .. building mChannel, the NotificationChannel instance
notificationManager.createNotificationChannel(mChannel);

Android Studio tells this

Error:(67, 32) error: cannot find symbol method createNotificationChannel(NotificationChannel)

I've a dependency on support-compat:27.0.0 set into my core/build.gradle file:

compile 'com.android.support:support-compat:27.0.0'
matdev
  • 4,115
  • 6
  • 35
  • 56

1 Answers1

17

There is no createNotificationChannel() method on NotificationManagerCompat. You have to use the native NotificationManager for that.

UPDATE 2019-07-29: As Onkar Nene points out, they finally added createNotificationChannel() on NotificationManagerCompat. Use androidx.appcompat:appcompat:1.1.0-rc01 or newer.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • True ! Thanks; I was misled by the accepted answer of this post: https://stackoverflow.com/questions/47192859/android-notification-doesnt-appear – matdev Jan 12 '18 at 20:44
  • @matdev: I am hoping that they eventually update `NotificationManagerCompat`, so perhaps in the future, that answer will be correct. – CommonsWare Jan 12 '18 at 20:46
  • 18
    A bit sad that the official docs suggest to use `NotificationManagerCompat` here: https://developer.android.com/training/notify-user/build-notification.html#Priority – Attila Molnár Mar 03 '18 at 09:58
  • 5
    Android documentation is a bit sad all over the place. :/ – Martin Marconcini Mar 21 '18 at 21:58
  • 2
    Wow so the official Android documentation is incorrect. That's great. – ShadowGod Apr 03 '18 at 01:12
  • Welcome to Android development! :D – Andrew Koster Aug 08 '18 at 23:04
  • Seriously though, please explain how to create a native NotificationManager, @CommonsWare – Andrew Koster Aug 08 '18 at 23:05
  • 2
    @AndrewKoster: Call `getSystemService()` on a `Context`. Where you get the `Context` from varies widely, based on where you are trying to raise the `Notification`. – CommonsWare Aug 08 '18 at 23:33
  • Thanks. It's a completely different syntax and namespace from the NotificationManagerCompat example above, so it's a useful example now that the NotificationManagerCompat class is incompatible with Android 9's notification channels. – Andrew Koster Aug 14 '18 at 23:32
  • 1
    Now with `androidx.appcompat:appcompat:1.1.0-rc01` you can use `NotificationManagerCompat.createNotificationChannel()` for API 26+. – Onkar Nene Jul 29 '19 at 10:08