问题描述
我按照 GoogleSamples
value[3]="notification_channel_system",所以我可以使用键值 android_channel_id
https://firebase.google.com/docs/cloud-messaging/http-server-ref 但收到时我无法获取按设备.
如何从 PushNotification 获取此 id 并将其设置为通知生成器?
从 17.4.0 开始,有官方 API 可以获取,参见 Marko Gajić 的 在下面回答.
过时的答案
RemoteMessage
对象确实在其 Bundle
中包含通道,但是 getData()
会删除任何开头的内容,除其他外,gcm.
.不幸的是,这包括频道键,即 gcm.notification.android_channel_id
.
出于我在应用程序处于前台时收到推送通知的目的,我仍然想使用从服务器发送的频道 ID 在系统中显示它.
我可以通过一个简单的两行文件来实现这一点(诚然有点 hacky):
包 com.google.firebase.messaging有趣的 RemoteMessage.getChannel() :字符串?= zzds.getString("gcm.notification.android_channel_id")
I registered notification channel in Android app following GoogleSamples https://github.com/googlesamples/android-NotificationChannels
However how can I get notification channel Id from RemoteMessage, so I can set it to 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
}
I found this value in RemoteMessage object
value[3]="notification_channel_system", so I can set the value to firebase push notification using key value android_channel_id
https://firebase.google.com/docs/cloud-messaging/http-server-ref but I cannot get it when it is received by device.
How does one get this id from PushNotification and set it to notification builder?
As of 17.4.0, there is an official API to get it, see Marko Gajić's answer below.
Outdated Answer
The RemoteMessage
object does contain the channel within its Bundle
, however getData()
strips out anything that starts with, among other things, gcm.
. Unfortunately, this includes the channel key, which is gcm.notification.android_channel_id
.
For my purposes when the push notification is received when the app is in the foreground, I still wanted to display it in the system, using the channel id that was sent from the server.
I'm able to achieve this (admittedly a bit hacky) with a simple two-line file:
package com.google.firebase.messaging
fun RemoteMessage.getChannel() : String? = zzds.getString("gcm.notification.android_channel_id")
这篇关于如何从 RemoteMessage 中找出通知通道 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!