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

Mal*_*bac 13 android firebase firebase-cloud-messaging android-8.0-oreo

我在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
}
Run Code Online (Sandbox Code Playgroud)

我在RemoteMessage对象中找到了这个值

在此输入图像描述

value [3] ="notification_channel_system",因此我可以使用键值android_channel_id https://firebase.google.com/docs/cloud-messaging/http-server-ref将值设置为firebase推送通知,但我无法得到它被设备接收.

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

AL.*_*AL. 11

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

目前没有获取通知渠道ID的功能(aka getChannelId()或来自您的帖子android_channel_id).AFAICT,这是按预期工作的.由于来自FCM的有效负载中包含的通知信道id 应由客户端自动处理.从文档(强调我的):

通知的信道ID(新Android中O).

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

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

这意味着你必须创建通知信道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);
}
Run Code Online (Sandbox Code Playgroud)

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

  • 你是对的,但你错过了这一点.当我们在应用程序处于后台时收到通知时,一切都很顺利.但是,如果我们在应用程序位于前台时收到通知,则目前无法知道通知通道是什么.如果我想在应用程序处于后台时触发本地通知来模仿行为,该怎么办?如果我想根据频道做不同的事情该怎么办?现在根本没有办法做任何这样的事情,因为`RemoteMessage`没有外化通知通道. (8认同)
  • 是的,我做到了.不幸的是,我无法跟踪我的功能请求.现在我们也在自定义数据有效负载中发送通知通道,以便在应用程序位于前台时收到通知时访问它. (4认同)

sky*_*erb 3

从 17.4.0 开始,有官方 API 可以获取它,请参阅 Marko Gaji\xc4\x87\'s下面 Marko Gaji\xc4\x87\ 的答案

\n\n

过时的答案

\n\n

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

\n\n

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

\n\n

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

\n\n
package com.google.firebase.messaging\n\nfun RemoteMessage.getChannel() : String? = zzds.getString("gcm.notification.android_channel_id")\n
Run Code Online (Sandbox Code Playgroud)\n