android - API 29 上未显示 Android 通知
问题描述
我在 Android API 29 上显示通知时遇到问题。虽然代码基于本教程,但执行代码时不会显示通知。
MainActivity.kt:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
...
val notification =
NotificationCompat.Builder(this, NOTIFICATION_GROUP_LOCATION)
.setContentTitle("NoteIfication")
.setContentText("Note: Priority: ")
.setSmallIcon(R.drawable.ic_notifications_black_24dp)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
val channel = NotificationChannel(
NOTIFICATION_CHANNEL_LOCATION,
"group location",
NotificationManager.IMPORTANCE_DEFAULT
).apply {
description = "notification channel for note reminders in app HyperNote"
}
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
with(NotificationManagerCompat.from(this)) {
notify(2134, notification)
}
}
}
解决方案
正如 Mike M. 指出的:“看起来您可能使用了两个不同的频道 ID;NOTIFICATION_GROUP_LOCATION 和 NOTIFICATION_CHANNEL_LOCATION。您在 NotificationCompat.Builder(this, ...) 中传递的 ID 必须与在 NotificationChannel 中传递的 ID 相同(...,“组位置”,IMPORTANCE_DEFAULT)。”
更改 ID 后,将显示通知。
推荐阅读
- authentication - Blazor - 在不同布局之间使用级联身份验证状态
- machine-learning - 如果我的 R2 高但 RMSE 和 MAE 低,这意味着什么
- excel - 如何使用变量选择一行
- react-native - 在 react-native 中使文本模糊
- c++ - 将一个元素从一个向量复制到另一个向量
- java - bean-validation @Pattern 第一个数字是 '1'
- kubernetes-helm - Helm 整数,旨在成为使用转义字符“\u200”解析的字符串值
- c++ - 有人可以解释我如何循环文本直到输入 EOF 字符吗?还有如何使用向量?
- c# - 字符串数据到结构化字符串增值税号
- string - MongoDB:将数据添加到带有两个(双)引号的字符串值中