【发布时间】:2016-12-12 14:20:46
【问题描述】:
当应用处于后台时,如何更新包含数据负载的 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());
完整项目在githubhttps://github.com/akshatashan/FirebaseCloudMessagingDemo
【问题讨论】:
-
当你说应用时,它是安卓应用吗?
-
是的一个安卓应用
标签: android firebase-cloud-messaging