2

In my app I need to notify user with a custom calling-type screen where he have to respond to an important business decision, its so important that he dint mind the bad user experience it can cause.

I use FCM data message, but the onRecieve method is not triggered when the app is swiped away from current apps list in devices below 6.0 .

Now I am using a service that starts a broadcast listener to listen received message, app is working as expected when its foreground/background, but once its swiped out, the service stops as the app is killed, but in devices above 6.0 app is not killed by swipe , this is my observation till now.

Basically what I am looking for is a service that can run even if your app is swiped away. I am aware of START_STICKY things, it doesn't help. Please help me.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • See some useful details in my answer [here](https://stackoverflow.com/a/39505298/4625829) – AL. Jun 23 '17 at 09:23
  • What device are you testing on? – David Wasser Jun 23 '17 at 11:44
  • @DavidWasser when i test it on devices below 6.0 i dont receive anything and any service with START_STICKY flag dont re initiate , even a foreground service is killed when swiped away. but above 6.0 i see expected behavior. I am using MI, Samsung , Lenovo of both above and below 6.0 – Shubham Sardar Jun 23 '17 at 13:28
  • @AL. i have see your answer before posting this question, if the white list part is true does it mean that a common developer cant achieve that in any way? my app is for very targeted customers who dont mind giving any permission for this. – Shubham Sardar Jun 23 '17 at 13:31
  • On Mi and some Lenovo devices, you may need to add your app to the list of "protected apps". These devices do not allow apps to run in the background unless they are in this list of protected apps. This is a power-saving approach on low-end devices. This would prevent Android from restarting your Service after it has been killed. – David Wasser Jun 23 '17 at 14:27
  • Even with "`START_STICKY`, Android will kill the OS process hosting the app when the app is swiped from the recent tasks list and then create a new OS process and start the `Service` again. There is no way to prevent your `Service` from being killed, however Android should restart it. – David Wasser Jun 23 '17 at 14:29

3 Answers3

1

You should just implement FCM as described in the documentation and onMessageReceived() should be called every time a data-message arrives (even if the app is killed or swiped away).

If the app doesn't receive the message if the app is swiped away it means that you have device that has a non standard version of Android installed.

Unfortunately on those devices this is NOT the only problem your app is going to have.

My suggestion is to contact the manufacturer and explain them that their customization are breaking the Android API.

More info: Push notifications using FCM not received when app is killed android

Diego Giorgini
  • 12,489
  • 1
  • 47
  • 50
0

create foreground service that keeps running always (except when app is force closed) Android - implementing startForeground for a service?

Jonathan F.
  • 2,357
  • 4
  • 26
  • 44
Shubham Goel
  • 1,962
  • 17
  • 25
  • the link cant be reached, for foreground service all we have to do is create a notification and call startforeground() sothing like this `NotificationCompat.Builder notification = new NotificationCompat.Builder(this) .setContentTitle("Tripin Foreground Service") .setTicker("This depends on your service") .setContentText("I am killed if my app is killed :( ") .setSmallIcon(R.mipmap.ic_launcher); startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification.build());` in your service? – Shubham Sardar Jun 23 '17 at 08:49
0

Make sure you have registered your service in the Android.manifest as described in the documentation. This will start your service only when a message is received. You do not need to have a service running constantly.

https://firebase.google.com/docs/cloud-messaging/android/receive

<service
    android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
Kuffs
  • 35,581
  • 10
  • 79
  • 92