How to find out the Notification channel Id from RemoteMessage

三世轮回 提交于 2019-12-04 02:50:53

问题


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?


回答1:


As of 17.4.0, there is an official API to get it, see Marko Gajić's answer below.

Outdated Answer

The RemoteMessage object does contain the channel within its Bundle, however getData() strips out anything that starts with, among other things, gcm.. Unfortunately, this includes the channel key, which is gcm.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:

package com.google.firebase.messaging

fun RemoteMessage.getChannel() : String? = zzds.getString("gcm.notification.android_channel_id")



回答2:


See getChannelId():

Gets the channel id from the notification. Note that this method does not perform verification on the existence of a channel, nor does it fallback to the manifest defined default or the default FCM channel.

Returns channel id that was provided when the message was sent, null otherwise.


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):

The notification's channel id (new in Android O).

The app must create a channel with this ID before any notification with this key is received.

If you don't send this key in the request, or if the channel id provided has not yet been created by your app, FCM uses the channel id specified in your app manifest.

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:

private void initNotificationChannels() {
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    String channelIdOne = "com.my.fcm.test.app.test_channel_one";
    CharSequence nameOne = getString(R.string.channel_one_name);
    String descriptionOne = getString(R.string.channel_one_description);
    int importanceOne = NotificationManager.IMPORTANCE_HIGH;

    NotificationChannel channelOne = new NotificationChannel(channelIdOne, nameOne, importanceOne);
    channelOne.setDescription(descriptionOne);
    channelOne.enableLights(true);
    channelOne.setLightColor(Color.GREEN);
    channelOne.enableVibration(false);
    mNotificationManager.createNotificationChannel(channelOne);

    String channelIdTwo = "com.my.fcm.test.app.test_channel_two";
    CharSequence nameTwo = getString(R.string.channel_two_name);
    String descriptionTwo = getString(R.string.channel_two_description);
    int importanceTwo = NotificationManager.IMPORTANCE_DEFAULT;

    NotificationChannel channelTwo = new NotificationChannel(channelIdTwo, nameTwo, importanceTwo);
    // Configure the notification channel.
    channelTwo.setDescription(descriptionTwo);
    channelTwo.enableVibration(false);
    mNotificationManager.createNotificationChannel(channelTwo);
}

So that when the payload comes in, the client itself should handle it accordingly.




回答3:


Since Cloud Messaging version 17.4.0, RemoteMessage.Notification class has been extended with the getChannelId() method, so this is now officially supported.

From the Firebase Release Notes:

Cloud Messaging version 17.4.0

Added getChannelId method to RemoteMessage.Notification for getting the channel ID set in a notification message.




回答4:


Try this code

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) 
{
    String id = remoteMessage.getNotification().getChannelId()
}


来源:https://stackoverflow.com/questions/46910621/how-to-find-out-the-notification-channel-id-from-remotemessage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!