Firebasemessagingservice: No Notifications When Phone Is Sleeping
Solution 1:
You need to take a look at these:
Solution 2:
To make firebase library to call your onMessageReceived()
in the following cases
- App in foreground
- App in background
- App has been killed
You must not use the notification
property as seen here:
{
"to": "/topics/journal",
"notification": {
"title" : "title",
"text": "data!",
"icon": "ic_notification"
}
}
But instead, bundle it as a data FCM - which lets the device handle it directly.
{
"to": "/topics/dev_journal",
"data": {
"text":"text",
"title":"",
"line1":"Journal",
"line2":"some data"
}
}
The message is sent in the argument RemoteMessage
along with your data object as Map<String, String>
of which you can manage within your app's onMessageReceived()
.
Solution 3:
If messages should appear when the device is in the doze mode, the messages must have set a high priority. Messages sent with 'normal' priority don't come when your phone is in doze mode. Here is a possible solution that worked for me:
privatevoidforwardMessageToFCM(String notificationMessage, boolean highPriority) {
Message message;
if (highPriority) {
message = createFCMMessageWithHighPriority(notificationMessage + APPEND_FCM_NOTIFICATION_MESSAGE);
} else {
message = createFCMMessageWithNormalPriority(notificationMessage + APPEND_FCM_NOTIFICATION_MESSAGE);
}
try {
firebaseMessaging.send(message);
printInfo("Sent message to FCM: " + notificationMessage);
} catch (FirebaseMessagingException e) {
printException(e);
return;
}
}
privateMessagecreateFCMMessageWithNormalPriority(String notificationMessage) {
returnMessage.builder().setNotification(createFCMNotification(notificationMessage)).setToken(registrationToken)
.setAndroidConfig(createAndroidConfigWithNormalMessagePriority()).build();
}
privateMessagecreateFCMMessageWithHighPriority(String notificationMessage) {
returnMessage.builder().setNotification(createFCMNotification(notificationMessage)).setToken(registrationToken)
.setAndroidConfig(createAndroidConfigWithHighMessagePriority()).build();
}
privateAndroidConfigcreateAndroidConfigWithHighMessagePriority() {
returncreateAndroidConfigWithPriority(Priority.HIGH);
}
privateAndroidConfigcreateAndroidConfigWithNormalMessagePriority() {
returncreateAndroidConfigWithPriority(Priority.NORMAL);
}
privateAndroidConfigcreateAndroidConfigWithPriority(Priority priority) {
returnAndroidConfig.builder().setPriority(priority).build();
}
privateNotificationcreateFCMNotification(String notificationMessage) {
returnNotification.builder().setBody(notificationMessage).setTitle(NOTIFICATION_CONTENT_TITLE).build();
}
Use createFCMMessageWithHighPriority-method in order to create a notification message with high priority. But according to doc, this should only used for important messages. Further info about priorities: https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message
Post a Comment for "Firebasemessagingservice: No Notifications When Phone Is Sleeping"