How to send a message from Firebase to the device when the application is killed?

I am trying to read Firebase notifications. It works fine, but I am stuck in receiving messages from the notification console when the application is not turned on.

I know the documentation says that:

If your application is in the foreground or in the background, you can receive a message in the onMessageReceived method, otherwise the user will receive a notification in the tray ... clicking on it will open the main action with the data inside the intent

But is there a way to catch every message from the notification console, even if the application is closed?

==== ANSWER ====

Find the answer here

Unable to send data message from notification console.

But there are other ways to send notifications to devices, and they will be found inside onMessageReceived!

You can use a terminal (Mac or Linux) or some service, such as Postman, to send a request for sending via this link: https://fcm.googleapis.com/fcm/send

with the following body:

{ "to": "/topics/your_topic_here", "data": { "text":"text", "text1":"text1", ... } } 

You also need to add 2 headers:

  • Authorization - key = your_server_key_here
  • Content-Type - application / json

To get the server key, you can find it in the firebase console: Your project → settings → Project settings → Cloud messaging → Server key

enter image description here

enter image description here

+5
source share
1 answer

the onMessageReceived () method will not be called if the application is in the background or is killed only when the message is sent through the Firebase Console.

When the application is not running, you will still receive a notification from the firebase console . But if you want to intercept data using onMessageReceived () , you will need to create a custom application server that will only send data to the fcm endpoint. In short, you will need to create an application server if you want to use FCM in this case more efficiently.

You can take a look at these two questions that discuss more of the same thing, and in the second question I will also briefly discuss the use of node.js and the java servlet to do the same:

Let me know if this provides you with some information.

+2
source

Source: https://habr.com/ru/post/1257461/


All Articles