I registered notification channel in Android app following GoogleSamples https://github.com/googlesamples/android-NotificationChannels
However how can I get notification channel Id from RemoteMessage, so I can set it to NotificationBuilder.
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
//int id = remoteMessage.getNotificationChannel(); // -something like this I could not find
}
I found this value in RemoteMessage object
value[3]="notification_channel_system", so I can set the value to firebase push notification using key value android_channel_id
https://firebase.google.com/docs/cloud-messaging/http-server-ref but I cannot get it when it is received by device.
How does one get this id from PushNotification and set it to notification builder?
Did some digging with Android Notification Channels in relation with FCM and here's what I got:
There is currently no function to get the notification channel id (aka
android_channel_id
or from your post --notification_channel_system
). AFAICT, this is working as intended. Since the notification channel id included in the payload from FCM should be handled automatically by the client. From the docs (emphasis mine):Which means you have to create the notification channel ids that you intend to use first -- what I did was create the notification channels in the application instance, like so:
So that when the payload comes in, the client itself should handle it accordingly.
The
RemoteMessage
object does contain the channel within it'sBundle
, howevergetData()
strips out anything that starts with, among other things,gcm.
. Unfortunately, this includes the channel key, which isgcm.notification.android_channel_id
.For my purposes when the push notification is received when the app is in the foreground, I still wanted to display it in the system, using the channel id that was sent from the server.
I'm able to achieve this (admittedly a bit hacky) with a simple two-line file: