gpt4 book ai didi

android - 当应用程序在后台时更新 Firebase 推送通知

转载 作者:行者123 更新时间:2023-11-29 15:40:41 24 4
gpt4 key购买 nike

如何在应用程序处于后台时更新包含数据负载的 Firebase 推送通知?有没有办法在发送给 Firebase API 的通知中指定通知 ID?

我向 firebase api 请求 json。

{
"registration_ids": ["device id"],
"collapse_key": "Updates Available"
"notification": {
"title": "title",
"desc": "description",
"body": "Message received",
"sound": "TYPE_NOTIFICATION",
"click_action": "sometargetAction"
},
"data": {
"user":
{
"id": 2
"name":"leapingwolf",
"occupation": "passionate coder"
}
}
}

当应用程序在 onMessageReceived 函数中处于前台时,我使用“用户”的 ID 附加到传递的推送通知

        User user = remoteMessage.getData().get("user");
Gson gson = new GsonBuilder().create();
User userModel = gson.fromJson(user, User.class);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("FCM Message With Payload")
.setContentText(messageBody)
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(userModel.getId(), notificationBuilder.build());

完整项目在github https://github.com/akshatashan/FirebaseCloudMessagingDemo

最佳答案

我根据链接中发布的答案找到了解决方案 How to handle notification when app in background in Firebase

总而言之,如果请求json没有notification标签,那么无论app是否在后台,都会调用onMessageReceived。发送数据标签中的所有相关字段并在 onMessageReceived 中解析。

关于android - 当应用程序在后台时更新 Firebase 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41102932/

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