gpt4 book ai didi

android - 如何从 RemoteMessage 中找出 Notification channel Id

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:24:21 29 4
gpt4 key购买 nike

我按照 GoogleSamples https://github.com/googlesamples/android-NotificationChannels 在 Android 应用程序中注册了通知 channel

但是我如何从 RemoteMessage 获取通知 channel 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 并将其设置为通知生成器?

最佳答案

参见 getChannelId() :

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.


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

目前没有获取通知 channel ID 的函数(aka android_channel_id 或来自您的帖子 -- notification_channel_system)。 AFAICT,这是按预期工作的。由于来自 FCM 的有效负载中包含的通知 channel 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您打算首先使用 - 我所做的是在应用程序实例中创建通知 channel ,如下所示:

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

因此,当有效负载传入时,客户端本身应该相应地处理它。

关于android - 如何从 RemoteMessage 中找出 Notification channel Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46910621/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com