其他分享
首页 > 其他分享> > android-如何从RemoteMessage找出通知通道ID

android-如何从RemoteMessage找出通知通道ID

作者:互联网

我在GoogleSamples 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] =“ notification_channel_system”,因此我可以使用键值android_channel_id https://firebase.google.com/docs/cloud-messaging/http-server-ref将值设置为Firebase推送通知,但是当设备接收到它时我无法获取它.

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

解决方法:

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

过时的答案

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

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

我可以通过一个简单的两行文件来实现这一目标(当然有点棘手):

package com.google.firebase.messaging

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

标签:android-8-0-oreo,android,firebase,firebase-cloud-messaging
来源: https://codeday.me/bug/20191010/1886766.html