Issue
If I send a Firebase message to my mobile over https://console.firebase.google.com/... the message appears on my mobile no matter whether the app is open on the device or not.
If I send a Firebase message with this Java code:
public void sendFirebaseMessage() { try { String registrationToken = "c4QGdbL_TTqZXaBC3F..."; Message message = Message.builder().putData("score", "850").putData("time", "11:06").setToken(registrationToken).build(); final FirebaseApp app = FirebaseApp.getInstancemy-app"); String response = FirebaseMessaging.getInstance(app).send(message); logger.info("successfully sent firebase message with response '{}' in FirebaseServiceImpl#sendFirebaseMessage", response); } catch ... }
then the app must be open so that the message appears.
My question would be what do I have to do so that I can send Firebase messages over Java code that behave like in 1.?
Solution
There are two types of notification messages:
Notification messages, which are considered high-level messages and are handled directly by the Firebase Cloud Messaging SDK automatically. This means that if the application is in the background or killed, the notifications are going directly to the system tray notification. On the other hand, if the application is in the foreground then it will get delivered to the
onMessageReceived()callback.Data messages, which are handled by the client application and can carry only the data payload. Besides that, the notifications are delivered to the app’s
onMessageReceived()callback regardless of whether the app is in the foreground or in the background.
So if you need to receive messages even if the app is in the background or even killed, you have to send notification messages, and for that, I recommend you use Cloud Functions for Firebase.
Answered By - Alex Mamo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.