I use FCM to send messages to mobile devices with this code:
private void sendMessageToToken(final NotificationRequest request) throws InterruptedException, ExecutionException {
Message message = getPreconfiguredMessageToToken(request);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonOutput = gson.toJson(message);
String response = sendAndGetResponse(message);
logger.debug("Sent message to token. Device token: " + request.getToken() + ", " + response + " msg " + jsonOutput
+ " in FirebaseService#sendMessageToToken");
}
private String sendAndGetResponse(Message message) throws InterruptedException, ExecutionException {
return FirebaseMessaging.getInstance().sendAsync(message).get();
}
private AndroidConfig getAndroidConfig(String topic) {
return AndroidConfig.builder().setTtl(Duration.ofMinutes(2).toMillis()).setCollapseKey(topic)
.setPriority(AndroidConfig.Priority.HIGH).setNotification(AndroidNotification.builder().setTag(topic).build()).build();
}
private ApnsConfig getApnsConfig(String topic) {
return ApnsConfig.builder().setAps(Aps.builder().setCategory(topic).setThreadId(topic).build()).build();
}
private Message getPreconfiguredMessageToToken(NotificationRequest request) {
return getPreconfiguredMessageBuilder(request).setToken(request.getToken()).build();
}
private Message.Builder getPreconfiguredMessageBuilder(NotificationRequest request) {
AndroidConfig androidConfig = getAndroidConfig(request.getTopic());
ApnsConfig apnsConfig = getApnsConfig(request.getTopic());
Notification notification = Notification.builder().setTitle(request.getTitle()).setBody(request.getBody()).build();
return Message.builder().setApnsConfig(apnsConfig).setAndroidConfig(androidConfig).setNotification(notification);
}
If I send more than one message to device, only the last message is shown on device.
My question would be if there is an adjustment at the Android device or if I can change something at Java- Code to do this?
Recognized by Mobile Development Collective and Google Cloud Collective