我最近将我的应用程序更新到了API26,通知不再起作用,甚至没有改变代码.

val notification = NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle("Title")
                .setContentText("Text")
                .build()
(getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(1, notification)

为什么不起作用?API是否有一些我不知道的变化?

推荐答案

From the documentation:

Android O introduces notification channels to provide a unified system to help users manage notifications. When you target Android O, you must implement one or more notification channels to display notifications to your users. If you don't target Android O, your apps behave the same as they do on Android 7.0 when running on Android O devices.

(增加强调)

You do not seem to be associating this Notification with a channel.

Kotlin相关问答推荐

在kotlin中使用List(mylist. size){index—TODO()}或Map迭代>

Kotlin—列出具有不同T的列表之间的操作'

Kotlin是否针对范围和进度优化sum()?

在Mapstruct中重用@映射定义

Microronaut Data 4和JDbi

我可以更改方法中泛型类的类型参数边界吗?

列表在 android WebView 中没有正确迭代

使用调度程序运行异步 Kotlin 代码

Kotlin 接口类型参数

内容更改后的 var 重新计算

如何在 Kotlin 中实现 Composition UML 关系?

Kotlin 静态函数:伴生对象,@JvmStatic @JvmField

Kotlin 解构超过五个组件

Kotlin 语言是如何用 Kotlin 编写的?

Kotlin 有 array.indexOf 但我无法弄清楚如何做 array.indexOfBy { lambda }

变量后的Android问号

Intellij 显示 build.gradle.kts 中的每一行都是红色的

在Kotlin中使用@Service时引发异常

Kotlin 中内部可见性修饰符的范围

Kotlin 的数据类 == C# 的 struct ?