我正在玩弄Wear OS上的通知,并在配备Android API 28的Fossil Falster 3上进行测试。
为什么在独立应用程序中以下通知没有被触发?代码基本上是从Google文档中直接复制的。
button_in.setOnClickListener {
val notificationId = 1
// The channel ID of the notification.
val id = "my_channel_01"
// Build intent for notification content
val viewPendingIntent = Intent(this, MainActivity::class.java).let { viewIntent ->
PendingIntent.getActivity(this, 0, viewIntent, 0)
}
// Notification channel ID is ignored for Android 7.1.1
// (API level 25) and lower.
val notificationBuilder = NotificationCompat.Builder(this, id)
.setLocalOnly(true)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("TITLE")
.setContentText("TEXT")
.setContentIntent(viewPendingIntent)
NotificationManagerCompat.from(this).apply {
notify(notificationId, notificationBuilder.build())
}
Log.d(TAG, "button was pressed!")
}
我可以看到“按钮已按下”的文本,但我没有收到任何通知。