如何从RemoteMessage中找出通知通道ID

问题描述 投票:13回答:3

我在Google应用https://github.com/googlesamples/android-NotificationChannels之后在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对象中找到了这个值

enter image description here

value [3] =“通知通道系统”,所以我可以使用键值qazxsw poi qazxsw poi将值设置为firebase推送通知,但是当设备收到它时我无法得到它。

如何从PushNotification获取此ID并将其设置为通知构建器?

android firebase firebase-cloud-messaging android-8.0-oreo
3个回答
2
投票

截至17.4.0,有一个官方API可以获得它,请参阅MarkoGajić的android_channel_id

过时的答案

https://firebase.google.com/docs/cloud-messaging/http-server-ref对象确实包含其answer below中的通道,但是RemoteMessage除去了Bundle之外的任何东西。不幸的是,这包括频道密钥,即getData()

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

我能用一个简单的双行文件来实现这一点(当然有点hacky):

gcm.

10
投票

gcm.notification.android_channel_id

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

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


与FCM有关的Android Notification Channels进行了一些挖掘,这就是我得到的:

目前没有获取通知渠道ID的功能(也就是package com.google.firebase.messaging fun RemoteMessage.getChannel() : String? = zzds.getString("gcm.notification.android_channel_id") 或来自您的帖子 - getChannelId())。 AFAICT,这是按预期工作的。由于来自FCM的有效负载中包含的通知信道id应由客户端自动处理。来自android_channel_id(强调我的):

notification_channel_system(Android O中的新功能)。

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

如果您未在请求中发送此密钥,或者您的应用尚未创建提供的频道ID,则FCM会使用您的应用清单中指定的频道ID。

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

notification's channel id

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


1
投票

自Cloud Messaging版本17.4.0起,RemoteMessage.Notification类已使用create the notification channel ids方法进行了扩展,因此现在已正式支持。

从Firebase发行说明:

云消息传递版本17.4.0

将getChannelId方法添加到RemoteMessage.Notification以获取通知消息中设置的通道ID。

© www.soinside.com 2019 - 2024. All rights reserved.