如何从 RemoteMessage 中找出 Notification channel Id
How to find out the Notification channel Id from RemoteMessage
我在遵循 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 对象中找到了这个值
value[3]="notification_channel_system",所以我可以使用键值 android_channel_id
https://firebase.google.com/docs/cloud-messaging/http-server-ref 将值设置为 firebase 推送通知,但是当设备接收到它时我无法获取它.
如何从 PushNotification 获取此 ID 并将其设置为通知生成器?
Gets the channel id from the notification. Note that this method does not perform verification on the existence of a channel, nor does it fallback to the manifest defined default or the default FCM channel.
Returns channel id that was provided when the message was sent, null otherwise.
使用 Android 与 FCM 相关的通知通道进行了一些挖掘,这就是我得到的:
目前没有获取通知渠道 ID 的功能(aka android_channel_id
或来自您的 post -- notification_channel_system
)。 AFAICT,这是按预期工作的。由于来自 FCM 的有效载荷中包含的通知通道 ID 应该 由客户端自动处理。来自 docs(强调我的):
The notification's channel id (new in Android O).
The app must create a channel with this ID before any notification with this key is received.
If you don't send this key in the request, or if the channel id provided has not yet been created by your app, FCM uses the channel id specified in your app manifest.
这意味着您必须 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);
}
这样当有效载荷进来时,客户端本身应该相应地处理它。
从17.4.0开始,有官方API获取,看Marko Gajić的.
过时的答案
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")
从云消息17.4.0版本开始,RemoteMessage.Notificationclass扩展了getChannelId()方法,正式支持
来自 Firebase 发行说明:
Cloud Messaging version 17.4.0
Added getChannelId method to RemoteMessage.Notification for getting the channel ID set in a notification message.
试试这个代码
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
String id = remoteMessage.getNotification().getChannelId()
}
我在遵循 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 对象中找到了这个值
value[3]="notification_channel_system",所以我可以使用键值 android_channel_id
https://firebase.google.com/docs/cloud-messaging/http-server-ref 将值设置为 firebase 推送通知,但是当设备接收到它时我无法获取它.
如何从 PushNotification 获取此 ID 并将其设置为通知生成器?
Gets the channel id from the notification. Note that this method does not perform verification on the existence of a channel, nor does it fallback to the manifest defined default or the default FCM channel.
Returns channel id that was provided when the message was sent, null otherwise.
使用 Android 与 FCM 相关的通知通道进行了一些挖掘,这就是我得到的:
目前没有获取通知渠道 ID 的功能(aka android_channel_id
或来自您的 post -- notification_channel_system
)。 AFAICT,这是按预期工作的。由于来自 FCM 的有效载荷中包含的通知通道 ID 应该 由客户端自动处理。来自 docs(强调我的):
The notification's channel id (new in Android O).
The app must create a channel with this ID before any notification with this key is received.
If you don't send this key in the request, or if the channel id provided has not yet been created by your app, FCM uses the channel id specified in your app manifest.
这意味着您必须 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);
}
这样当有效载荷进来时,客户端本身应该相应地处理它。
从17.4.0开始,有官方API获取,看Marko Gajić的
过时的答案
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")
从云消息17.4.0版本开始,RemoteMessage.Notificationclass扩展了getChannelId()方法,正式支持
来自 Firebase 发行说明:
Cloud Messaging version 17.4.0
Added getChannelId method to RemoteMessage.Notification for getting the channel ID set in a notification message.
试试这个代码
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
String id = remoteMessage.getNotification().getChannelId()
}