4

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...

Drarig29
  • 1,902
  • 1
  • 19
  • 42
  • 4
    These services will be started by Google Play services, which is always running on the device. You don't have to and should not start/stop these services yourself. – Arthur Thompson Jun 29 '16 at 06:15
  • @ArthurThompson In my onMessageReceived() method, I've put a Log and when I kill my app and send a downstream notification, the Log is not executed... This is why I thought the services were killed where my app was. – Drarig29 Jun 29 '16 at 09:37
  • 1
    onMessageReceived is always called if you send a data message, but it is sometimes not called if you send a notification message. Which type of downstream message are you sending? – Arthur Thompson Jun 29 '16 at 15:58
  • @ArthurThompson I'm sending data message, but when my app is force closed, I get this error in the logcat : `broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.drarig29.phonecontrol (has extras) }` – Drarig29 Jun 29 '16 at 17:02
  • 2
    Is this happening on multiple devices or just one device, I'm wondering if the OS on the device may be limiting what services can be started when the corresponding app is killed. There may be a setting on the device that allows/disallows this. – Arthur Thompson Jun 29 '16 at 22:46
  • I've only tested this on my phone. I'll test on emulators. – Drarig29 Jun 30 '16 at 07:46
  • 1
    From my experience, In settings force close would stop the service. But swipe close should not. @ArthurThompson Is there a doc somewhere? How can I make sure my service is restarted? Thanks. – Weishi Z Apr 25 '17 at 22:10
  • @Drarig29 Please update how it goes. Are you able to trigger onMessageReceive() once app is closed? – Weishi Z Apr 25 '17 at 22:13
  • Did you found any solution in order to receive notification when app is fully closed? – shubomb Aug 22 '17 at 08:54
  • @Shubhamjain-systematix see my answer :) – Drarig29 Aug 22 '17 at 09:45

1 Answers1

3

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.

Drarig29
  • 1,902
  • 1
  • 19
  • 42
  • 1
    There are also devices where even if the app is still just simply swiped away, even though it's not force closed, the device itself is preventing it from receiving messages. Others say that this can't be the case because apps like WhatsApp were able to do it. The reason I've learned so far for that is because the device manufacturers have whitelisted most of the well-known apps for it to be possible. – Cecil Paul Dec 17 '19 at 07:04