Handle onMessageReceived() and onTokenRefresh() ev

2019-02-25 00:34发布

问题:

I have an app with 2 services :

MessagingService extends FirebaseMessagingService

and

InstanceIDService extends FirebaseInstanceIdService

There are declared in the AndroidManifest.xml file. The problem is that when my app's process is forced close itself, the services are also killed.

I'd like them not to be killed. I've heard about the START_STICKY flag for the Service class, but I can't override the onStartCommand() method in these services so as to return this flag...

回答1:

To answer to the comments, I didn't find any "solution" to my problem because it's not a problem. If the user wants to close my app, he has the right to.

Since my onMessageReceived() method belongs to my MessagingService in my app, if the app is force-closed by the user, neither the app nor the service will restart.

As Weishi Zeng said in the comments, the "settings force close" stops all about my app including the services, so the notification is received by the Google Play services but my app isn't here to see it. Then the onMessageReceived() method is not executed. However the service is not closed with the "swipe close".

Eventually, I didn't want to find any workaround since this would be contradicting the user choice.

See here.