【问题标题】:How to find out the Notification channel Id from RemoteMessage如何从 RemoteMessage 中找出通知通道 ID
【发布时间】:2017-10-24 12:27:58
【问题描述】:

我在 Android 应用中注册了通知渠道,关注 GoogleSamples https://github.com/googlesamples/android-NotificationChannels

但是如何从 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 对象中找到了这个值

value[3]="notification_channel_system",所以我可以使用键值 android_channel_id https://firebase.google.com/docs/cloud-messaging/http-server-ref 将值设置为 Firebase 推送通知,但是当设备接收到它时我无法获取它。

如何从 PushNotification 获取此 id 并将其设置为通知生成器?

【问题讨论】:

  • notification_channel_system 是否在您的有效载荷中?还是您只是在 RemoteMessage 中调试并发现这些值?
  • 这是我的有效载荷,我在 firebase 通知控制台中插入文本字段“Android 通知通道”,所以我确信通道数据来自正确的位置

标签: android firebase firebase-cloud-messaging android-8.0-oreo


【解决方案1】:

getChannelId():

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

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


对与 FCM 相关的 Android 通知通道进行了一些挖掘,这就是我得到的结果:

目前没有获取通知频道 ID 的功能(aka android_channel_id 或来自您的帖子 -- notification_channel_system)。 AFAICT,这是按预期工作的。由于来自 FCM 的负载中包含的通知通道 ID 应该由客户端自动处理。来自docs(强调我的):

notification's channel id(Android O 中的新功能)。

应用必须先创建一个具有此 ID 的频道,然后才能收到任何带有此密钥的通知。

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

这意味着您必须先create the notification channel ids 表明您打算使用——我所做的是在应用程序实例中创建通知通道,如下所示:

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

这样当payload进来时,客户端自己应该相应地处理它。

【讨论】:

  • 通知数据通道应该包含在通知负载中,而不是数据负载中。
  • @Malb​​ac Riiight。我以为你添加了自定义。我会做一些测试,如果我设法得到它,我会回到这里进行编辑。
  • @Malb​​ac 编辑完成。我想就是这样。如果有不清楚的地方,请告诉我。干杯!
  • 你是对的,但你没有抓住重点。当我们在应用程序处于后台时收到通知时,一切正常,如您所说。但是,如果我们在应用程序处于前台时收到通知,则目前无法知道通知通道是什么。如果我想触发本地通知来模仿应用程序在后台时的行为怎么办?如果我想根据频道做不同的事情怎么办?现在根本没有办法做任何事情,因为RemoteMessage 没有外部化通知通道。
  • 是的,我已经做到了。不幸的是,我无法跟踪我的功能请求。现在,我们也在自定义数据负载中发送通知通道,以便在应用处于前台时收到通知时访问它。
【解决方案2】:

从 17.4.0 开始,有一个官方 API 可以获取它,请参阅 Marko Gajić 的answer below

过时的答案

RemoteMessage 对象确实在其Bundle 中包含通道,但是getData() 会删除以gcm. 开头的所有内容。不幸的是,这包括频道密钥,即gcm.notification.android_channel_id

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

我可以通过一个简单的两行文件来实现这一点(诚然有点 hacky):

package com.google.firebase.messaging

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

【讨论】:

  • 有趣。目前这听起来是一个足够好的解决方案。然而,如果谷歌决定改变数据的位置(比如把它放在不同的类中),这可能会遇到一些问题——这使得调用(我认为是混淆的)类(zzds)会在未来导致崩溃—— - 要求应用程序处理较新用户的用户强制更新。
  • 是的,它肯定很脆弱,如果库被重新打包/重新混淆,则需要更改。幸运的是,firebase 消息传递依赖项与应用程序一起打包。我认为依赖项更新只会给你一个编译错误,并且可以在同一个应用程序版本中解决。
  • 果然firebase-messaging 的新版本发布了一个不同的混淆。更新了答案。
【解决方案3】:

从 Cloud Messaging 17.4.0 版本开始,RemoteMessage.Notification 类已扩展为 getChannelId() 方法,因此现在正式支持。

来自 Firebase 发行说明:

云消息传递版本 17.4.0

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

【讨论】:

    【解决方案4】:

    试试这个代码

    public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
    
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) 
    {
        String id = remoteMessage.getNotification().getChannelId()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多