找不到方法createNotificationChannel(NotificationChannel)

mat*_*dev 8 android

我正在尝试使用Android Oreo(SDK 27)处理通知.

这是我创建NotificationChannel的代码:

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// .. building mChannel, the NotificationChannel instance
notificationManager.createNotificationChannel(mChannel);
Run Code Online (Sandbox Code Playgroud)

Android Studio告诉了这一点

Error:(67, 32) error: cannot find symbol method createNotificationChannel(NotificationChannel)
Run Code Online (Sandbox Code Playgroud)

我依赖于support-compat:27.0.0设置到我的core/build.gradle文件中:

compile 'com.android.support:support-compat:27.0.0'
Run Code Online (Sandbox Code Playgroud)

Com*_*are 15

没有createNotificationChannel()方法NotificationManagerCompat.你必须使用原生NotificationManager的.

  • 官方文档建议在这里使用"NotificationManagerCompat"有点遗憾:https://developer.android.com/training/notify-user/build-notification.html#Priority (17认同)
  • Android文档在整个地方都有点难过.:/ (4认同)
  • 哇所以官方Android文档不正确.那很棒. (2认同)
  • @AndrewKoster:在`Context` 上调用`getSystemService()`。根据您尝试提出“通知”的位置,您从何处获取“上下文”的情况有很大差异。 (2认同)