How to update a Firebase push notification containing data payload when the app is in background? Is there a way to specify the notification id in the notification to the Firebase API?
My request json to the firebase api.
{
"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"
}
}
}
I use the id of the "user" to append to a delivered push notification when the app is in foreground in the onMessageReceived function like so
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());
The full project is in github https://github.com/akshatashan/FirebaseCloudMessagingDemo