更新时间:2022-11-26 13:37:46
onMessageReceived()方法 如果应用程序在后台或仅在收到消息时终止,则不会被调用通过Firebase控制台发送.
应用未运行时,您仍然会从Firebase控制台收到通知 .但是,如果您想通过 onMessageReceived()拦截数据,则必须创建一个自定义应用服务器,该服务器仅将发送 数据有效载荷发送到fcm端点.简而言之,如果您想在这种情况下更有效地利用FCM,则必须创建一个应用服务器.
您可以看看这两个问题,它们讨论了更多有关同一问题的内容,在第二个问题中,我还简要讨论了如何使用node.js和java servlet进行相同操作:
请让我知道这是否为您提供了一些信息.
I am trying to get familiar with Firebase Notifications. It works fine, but I am stuck with receiving messages from the notification console when the app is not turned on.
I know that documentation says that:
if your app in foreground or background you can receive message in onMessageReceived method, otherwise user will receive notification in tray... click on it will open main activity with data inside intent
But is there are any way to catch every message from the notification console even if the application is closed?
==== ANSWER ====
Find answer here
There is no way to send data message from the notification console.
But there are other ways to send notification to devices and they will be caught inside onMessageReceived!
You can use terminal (Mac or Linux) or some service like Postman to send Post request on this link: https://fcm.googleapis.com/fcm/send
with the next body:
{
"to": "/topics/your_topic_here",
"data": {
"text":"text",
"text1":"text1",
...
}
}
also you need to add 2 headers:
To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key
onMessageReceived() method will not be called if the app is in background or killed only when the message is sent through Firebase Console.
When app is not running you will anyway receive notification from firebase console. But if you want to intercept data via onMessageReceived() then you will have to create a custom app server which will send only data payload to fcm endpoint. In short you will have to create an app server if you want to actually utilize FCM more efficiently in this case .
You can have a look at these two questions which discuss more regarding same and in second question I also discuss briefly over using node.js and java servlet to do the same:
Do let me know if this provides you with some info.