0

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.

Zachary Sweigart
  • 1,051
  • 9
  • 23

1 Answers1

0

The issue was caused by the format of the notification being sent from our server. The format was

  const message = {
    token: pushToken,
    notification: {
      title: 'Title',
      body: 'Body',
    },
    data: {
      pushId,
    },
  };

which meant that when the app was backgrounded or closed Firebase was building the notification itself and the add functions were never being called. We switched to:

const message = {    
    token: pushToken,
    data: {
      pushId,
      title: 'Title',
      body: 'Body',
    },
  };

Which is fully a data push and allowed us to build the notification ourselves entirely.

This also meant we had to make one other minor change to building the notification. Instead of getting the title and body from the message, we get them directly from the data.

NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
        .setContentTitle(message.data.get("title") ?: "Title")
        .setContentText(message.data.get("body") ?: "Body")
        ...
Zachary Sweigart
  • 1,051
  • 9
  • 23