繁体   English   中英

通知未显示在Oreo模拟器上

[英]Notifications not showing on Oreo emulator

我一直试图在Oreo模拟器上显示一个简单的通知。 奇怪的是,我什么也没看到。

让我们消除明显的答案:我尝试检查应用程序的通知,尝试了Notifications和NotificationCompat路径。 我尝试使用或不使用频道,尝试使用或不使用群组。

该代码是基本代码(是的,我使用Kotlin,但很容易理解):

class MainActivity : Activity() {

var id = 0

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val button:View = findViewById(R.id.button)
    button.setOnClickListener(this::onAddNotification)
}

private fun onAddNotification(v: View) {
    id++
    val builder = Notification.Builder(this).setSmallIcon(R.drawable.ic_notifications_none)
            .setContentTitle("Content #$id").setContentText("Content text for $id")

    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.notify(id, builder.build())
}
}

不用说,这是在Oreo之前完美运行的代码。 另一方面,Gmail和地图会在该模拟器上显示通知。 我可能忘记了什么?

谢谢

正如Tim Castelijns在上面评论的那样...如果您使用的是API26,则必须使用渠道

请记住, NotificationCompat无法正确处理 (截至2017年9月4日),因此您可以选择:

  • 使用API​​级别25或更早版本
  • 使用渠道。 请注意,Google不推荐使用API​​ 26上的Builder(context)构造函数。这是有充分理由的。

我花了一个小时才意识到模拟器处于“请勿打扰”状态,并且没有显示通知。

Google表示:以Android 8.0(API级别26)为目标时,您必须实现一个或多个通知渠道才能向用户显示通知。 如果您未定位到Android 8.0(API级别26),但您的应用程序在运行Android 8.0(API级别26)的设备上使用,则您的应用程序的行为与运行Android 7.1(API级别25)或更低版本的设备相同。

https://developer.android.com/guide/topics/ui/notifiers/notifications.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM