Reputation: 5747
I am playing around with notifications on Wear OS and testing on a Fossil Falster 3 with Android API 28.
Why is the following notification not being triggered, in an standalone app. The code is pretty much right from the Google documentation.
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!")
}
I can see the "button was pressed!" text, but I am not getting any notifications.
Upvotes: 3
Views: 860
Reputation: 691
Watch apps require a notification channel, unlike Android apps which do not work like this.
In your code, val notificationId = 1
refers to the notification channel ID.
You can construct a NotificationChannel
and register it like this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel
val name = getString(R.string.channel_name)
val descriptionText = getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val mChannel = NotificationChannel(1, name, importance) // 1 is the channel ID
mChannel.description = descriptionText
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
}
Notice in the commented code, in val mChannel = ...
, you can see that the first parameter value 1
refers to the channel ID, as specified in your code in the OP.
You can read more about notification channels here: https://developer.android.com/training/notify-user/channels
Upvotes: 1