Service to keep alive FirebaseMessagingService even if app is not running and show notifications with Data payload

拜拜、爱过 提交于 2021-01-02 22:53:26

问题


I am working in a application that uses FCM notifications, when app is running (foreground or background) the notification receives but when i clear the app from my recent apps i do not receive any notification i am using FirebaseMessagingService. So i want to create a service that will keep alive myService which is extending FirebaseMessaging service even if app is not running or killed.


回答1:


I found a reason for this problem when you Release-APK is not from GPlay some mobile companies block services which want to auto start the app. you can do two think first download app from GPlay or in your phone settings disable battery optimization for your app or permit your app auto start.




回答2:


I've tried everything - in my opinion theres no reliable solution to prevent any service from being killed. The only way is to make sure to deliver the notification to the system tray not to the application.

There are 2 types of FCM notifications: Notification message and Data message.

Data messages are delivered to system tray and are always display - even if service is not running.

Notification message looks like:

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    }
  }
}

and triggers method OnMessageReceaved() of FirebaseMessagingService. Many devices (especially Huawei and Xiaomi) try to do everything to kill background services to prevent battery drain. So the FirebaseMessagingService isn't the best way to handle notifications.

Second type is Data message:

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "data":{
      "Nick" : "Mario",
      "body" : "great match!",
      "Room" : "PortugalVSDenmark"
    }
  }
}

This type is handled by the system tray, so you don't need any of service running to get the notification. Its much more convenient method, but as far i know, it can't be achieved with the console.

You would probably need server API to send Data message.

Read this for more details.




回答3:


When the application is killed. You need to push silent notification (payload without notitication property) to handle in the FirebaseMessagingService.

You can refer this link: https://firebase.google.com/docs/cloud-messaging/concept-options




回答4:


You will need to use Data-Messages from FCM to have control of your notification payload, by using Data-Messages your app's onMessageReceived will be called even if your app is in background or killed, you will have to create a custom notification yourself to show your payload. On the other hand I used Notification-Messages in FCM, they were delivered even if the app was killed.



来源:https://stackoverflow.com/questions/58022896/service-to-keep-alive-firebasemessagingservice-even-if-app-is-not-running-and-sh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!