androidfirebase-cloud-messagingnotification-channel

Android FCM Push Channel Id is always null


I have a custom notification channel that never gets triggered. I read all the documentation for server and client but I'm still missing something.

What I want

I want to send a high-priority push notification via FCM to eventually wake up a foreground service in the app. Therefore I defined a custom notification channel. I want to receive the push, find out that it was sent to the custom channel, and show a notification.

My Problem

I receive all push notifications from firebase as expected but the channelId is always null.

The Setup

We use FCM directly on both sides, the server and the android app. Our users use Android 8.1 and higher. No legacy push is needed.

The backend sends a JSON that looks like the following:

{
    "notification": {
        "android_channel_id": "MY_High_Prio_Push_Channel"
    },
    "data": {
        "notificationBody": "BodyText",
        "notificationTitle": "Title"
    },
    "priority": "high"
}

The App has registered the push channel successfully. I can see it in the device settings. The push channel will be re-registered on any app start like this

private fun registerHighPrioPushToRestartTheForegroundService() {
    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val channel = NotificationChannel(getString(R.string.push_channel_id_high_prio), getString(R.string.push_channgel_sync_with_high_prio), NotificationManager.IMPORTANCE_HIGH)
    channel.setShowBadge(true)
    notificationManager.createNotificationChannel(channel)
}

At Runtime

If the android device receives a push from FCM onNewMessageReceived() is called. I then check what channelId it was sent to by calling:

if (remoteMessage.notification?.channelId == getString(R.string.push_channel_id_high_prio)) {
    // I would love to do my channel specific stuff here
}

As you can see in the screenshot below the body contains the full JSON. All other values are null.

enter image description here


Solution

  • The problem was on the server side. The whole JSON for the push was put in the body tag of the notification. Now that this is fixed we send a JSON like the following and the parsing in the client works as expected. We removed the notification tag so that onMessageReceived() will be called on every single push.

    {
        "to": "{devicePushToken}",
        "android": {
            "priority": "high"
        },
        "priority": 10,
        "data": {
            "title": "Some Title",
            "body": "Some Message",
            "android_channel_id": "yourUniquePushId"
        }
    }