Android 如何从RemoteMessage中查找通知通道Id

Android 如何从RemoteMessage中查找通知通道Id,android,firebase,firebase-cloud-messaging,android-8.0-oreo,Android,Firebase,Firebase Cloud Messaging,Android 8.0 Oreo,我在Android应用程序中注册了通知频道,下面是谷歌的示例 但是,如何从RemoteMessage获取通知通道Id,以便将其设置为NotificationBuilder public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remote

我在Android应用程序中注册了通知频道,下面是谷歌的示例

但是,如何从RemoteMessage获取通知通道Id,以便将其设置为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
}
我在RemoteMessage对象中找到此值

值[3]=“notification\u channel\u system”,因此我可以使用键值
android\u channel\u id
将该值设置为firebase push notification,但当设备接收到该值时,我无法获取该值

如何从PushNotification获取此id并将其设置为notification builder?

请参阅:

从通知中获取通道id。请注意,此方法不会对通道的存在性执行验证,也不会回退到清单定义的默认或默认FCM通道

返回发送消息时提供的通道id,否则返回null


对与FCM相关的Android通知渠道进行了一些挖掘,以下是我得到的:

目前没有获取通知频道id的功能(也称为android\u频道id或从您的帖子--
通知频道系统
)。AFAICT,这正在按预期工作。由于FCM有效负载中包含的通知通道id应由客户端自动处理。从(我的)重点:

新版本(Android O中的新版本)

在收到任何带有此密钥的通知之前,应用程序必须使用此ID创建频道。

如果您没有在请求中发送此密钥,或者如果您的应用程序尚未创建提供的频道id,FCM将使用您的应用程序清单中指定的频道id

这意味着您必须首先使用您想要使用的内容--我所做的是在应用程序实例中创建通知通道,如下所示:

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);
}

因此,当有效负载进入时,客户端本身应该相应地处理它。

从17.4.0开始,有一个官方API来获取它,请参见Marko Gajić的

过时的答案
RemoteMessage
对象确实在其
捆绑包中包含通道,但是
getData()
会除去以
gcm开头的任何内容。
。不幸的是,这包括通道密钥,即
gcm.notification.android\u channel\u id

出于我的目的,当应用程序在前台收到推送通知时,我仍然希望使用从服务器发送的通道id在系统中显示推送通知

我可以通过一个简单的两行文件来实现这一点(无可否认有点粗糙):

package com.google.firebase.messaging

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

自云消息17.4.0版以来,RemoteMessage.Notification类已使用该方法进行了扩展,因此现在正式支持该方法

从Firebase发行说明中:

云消息17.4.0版

向RemoteMessage.Notification添加了getChannelId方法,用于获取通知消息中设置的通道ID

试试这个代码

公共类FirebaseMessagingService扩展com.google.firebase.messagingService.FirebaseMessagingService{
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage)
{
字符串id=remoteMessage.getNotification().getChannelId()
}

是您的有效负载上的
通知通道系统
吗?还是您刚刚调试并在RemoteMessage中输入了这些值?这是我的有效负载,我将其插入firebase通知控制台的文本字段“安卓通知通道”,所以我确信通道数据来自正确的位置通知数据通道应该包含在通知有效负载中,而不是数据有效负载中。@Malbac RiiLight。我以为您添加了自定义内容。我将进行一些测试,如果我能够获得它,我将返回这里进行编辑。@Malbac完成了编辑。我想就这些了。如果有什么问题,请告诉我不清楚。干杯!你说得对,但你没有抓住要点。当我们在应用程序位于后台时收到通知,一切都像你说的那样好。但是,如果我们在应用程序位于前台时收到通知,目前无法知道通知通道是什么。如果我想触发本地通知,该怎么办继续模拟应用程序在后台时的行为?如果我想根据频道做不同的事情怎么办?现在根本没有办法这么做,因为
RemoteMessage
没有将通知频道外部化。是的,我已经做到了。但不幸的是,我无法跟踪我的功能请求。目前我们正在在自定义数据负载中也发送通知通道,以便在应用程序处于前台时收到通知时访问它。很有趣。目前这听起来是一个足够好的解决方案。但是,如果Google决定更改数据的位置(例如将其放在不同的类中),这可能会遇到一些问题--这使得调用(我假定是模糊的)类(
zzds
)导致将来崩溃--需要对应用程序处理较新应用程序的用户进行强制更新。是的,它确实很脆弱,如果库被重新打包/重新混淆,则需要进行更改。幸运的是,firebase消息依赖项与应用程序打包在一起。我认为依赖项更新只会给您一个编译错误,并且可以在同一个应用程序版本中解决。果然,firebase messaging的新版本发布时使用了不同的模糊处理。更新了答案。