I am using FCM to display push notifications but when the app is backgrounded or closed the notification does not show the actions.
I get the message in on message received and build a notification using NotificationCompat.Builder
val notification: Notification =
NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
.setContentTitle(message.notification?.title ?: "Title")
.setContentText(message.notification?.body ?: "Body")
.setStyle(
NotificationCompat.BigTextStyle()
.bigText(message.notification?.body ?: "Grant access?")
)
.addAction(getApproveAction(message.data["pushId"] ?: ""))
.addAction(getRejectAction(message.data["pushId"] ?: ""))
.setSmallIcon(R.drawable.logo)
.build()
val manager = NotificationManagerCompat.from(applicationContext)
manager.notify(NOTIFICATION_ID, notification)
the action methods look pretty similar with the only differences being the action and the ACCEPTED
boolean value. Here's what the approve action looks like:
private fun getApproveAction(pushId: String): NotificationCompat.Action {
val approveIntent =
Intent(this, NotificationActionReceiver::class.java).setAction(getString(R.string.notification_action_approve))
.apply {
putExtra(PUSH_ID, pushId)
putExtra(ACCEPTED, true)
}
val approvePendingIntent: PendingIntent =
PendingIntent.getBroadcast(this, 1, approveIntent, PendingIntent.FLAG_CANCEL_CURRENT)
return NotificationCompat.Action(R.drawable.done, "Accept", approvePendingIntent)
}
When the app is foregrounded the notification displays perfectly with two actions. But when the app is backgrounded or closed I only see the title and body, no actions. I am testing on a pixel 3a emulator with Android 10.
I have tried updating the intent format to be like the one described here https://stackoverflow.com/a/47032464/4801470 but didn't have any luck.