true

I have read a similar question on SO, however, I was not able to get the correct answer from it.

I have a system wherein we send notification to around 500 devices.

Unfortunately, many of these devices are not receiving the notification. I have found that OPPO F1 series phones are particularly not getting the notification.

I have observed that this occurs if the app is stopped from multi-task tray. How do I resolve this?

Update: I have observed that when I close the app from task-tray, my app is forced stop in application manager. While when I close Whatsapp from task-tray, it is still not forced stop. How is that being handled by Whatsapp?

1 upvote
  flag
the same issue solved can be solved .. //allinonescript.com/a/45810771/1993001 – Rahul Devanavar

9 Answers 11

Update 03/2017 - Including a part of my answer here.

For the topic with regards to swipe closed/killed/force stopped, this topic has been discussed for quite some time and there doesn't seem to be a definite answer. During one of my testings, I am able to still receive a message (tested with a data-only message payload) if I Swipe close my app. But when I force closed it from the Settings menu, I wasn't able to receive any messages. Do note that this is not always the behavior.

There are some devices that were designed that when you swipe close the app, it will be the same as force stopping them (see my answer here).

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.

This is not documented anywhere because (IMO), this is a topic that depends also on the device and that FCM has no total control over.


Original Answer:

Since it's device specific (as you mentioned in your post: OPPO F1 series phones), it may very well be possible that when an app is stopped from multi-task tray in that device, it is actually killing the app, causing the services and other background processes associated with it to also be destroyed. See this answer for a little more idea of what I'm trying to say.

If you search around the community, what is commonly suggested here is to make use of the START_STICKY flag. However, I've seen that it was previously mentioned before for FirebaseMessagingService (see this post, comment by @ArthurThompson):

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.

With that said, there is also the possibility of (again from the comments):

There may be a setting on the device that allows/disallows this.


I suggest doing further testing if the services are being killed by the device itself or see if there are settings that are blocking the notifications.

upvote
  flag
The strange is that WhatsApp notifications are being received by the mobile even when it is closed by swiping off. – milan m
upvote
  flag
@milanm WhatsApp is often the example on this topic. Though I'm not exactly sure how they do it. Maybe using an AlarmManager and restarting the service would help? I've seen a lot of people in the community also mentioning it. – AL.
upvote
  flag
When you are using remote push notifications, Android will wake up your app and send you the notification by using an Intent. In the majority of the devices, when you swipe left the app in the multi task tray, you'll be effectively killing the app and its services, but this does not stop the app from receiving push notifications. – tomacco
upvote
  flag
@tomacco Killing an app would normally also halt all services which are needed when receiving notifications. This is by design since Android 3.1. – AL.
2 upvote
  flag
@AL that is true (I did't say otherwise), but it won't prevent you from receiving push notifications. Google Play Services has a permanent connection to GCM /FCM, and as soon it receives a message it will start your declared service to deliver you the message. So it does not matter if your app is completely dead, if you have implemented GCM correctly, you will receive the message. – tomacco
upvote
  flag
@tomacco Interesting. Was that how it's always been for Google Play Services and GCM/FCM? Or was it only for recent versions? So the issue at hand may possibly be with the devices itself. Maybe something in the OS or phone setting is intentionally blocking the notification? – AL.
upvote
  flag
@AL Probably is the device itself. Or probably it is the implementation, I am looking at my existing push code in order to provide an accurate answer – tomacco
upvote
  flag
@tomacco I'm thinking it's highly unlikely to be in the implementation. Since it works, just not for the OPPO F1 series phones. Also, I was wondering if you can point me to a documentation with regards to what you said in your previous comment about Google Play Services has a permanent connection to GCM /FCM? :) I would like to check it out and update some of my answers here in StackOverflow if need be. :) – AL.
1 upvote
  flag
@AL. Yes sure, check this out: stuff.mit.edu/afs/sipb/project/android/docs/google/gcm/gcm.h‌​tml They barely speak about it but it is an interesting document. Also you can find more info in other SO answers. //allinonescript.com/questions/19111901/… – tomacco
2 upvote
  flag
@milanm many China vendors, including OPPO, have the tendency of imitating iOS. In my company, we found that many of them consider closing in Recent Screen the same as force-killing the app, and more importantly this behavior is not enforced uniformly, i.e. popular IM apps are exempt from this behavior. So I'd suggest you download other less popular apps and test their behavior. – Kai
upvote
  flag
@AL "During one of my testings, I am able to still receive a message (tested with a data-only message payload) if I Swipe close my app." Can you provide example for this? – Bishwash
upvote
  flag
@Bishwash As far as I can remember, I used a physical device to test this (I think Nexus 6) and sent the request through Postman. – AL.
upvote
  flag
@AL. the device manufacturers have whitelisted most of the well-known apps means we can't do anything for our app to receive notifications because it is not famous till date so basically it's a dependency on manufacturers totally – Kapil Rajput
upvote
  flag
@KapilRajput Unfortunately, that seems like the case. You could try to send over a request to them, but I don't know what could happen beyond that. – AL.
2 upvote
  flag
@KapilRajput do you have a link to support your claim so I could show it to my boss? :D – lelloman
upvote
  flag
@lelloman I mentioned "This is not documented anywhere because (IMO), this is a topic that depends also on the device and that FCM has no total control over." -- currently, this still seems to be the case. – AL.
upvote
  flag
@AL. apologies if I'm a bit slow but I still need to understand one thing, if my app is force closed, is there any chance I can send a "notification" only push and having it shown? The documentation says that GCM/FCM shows the notification on behalf of the app. But I've been trying it and it doesn't seem to work if the app is force closed. – lelloman
upvote
  flag
@lelloman No worries. The behavior you mentioned is GCM/FCM's default behavior when sending notification-only message payloads, but only if the notification is *not blocked by the device. In your case, it's highly probable that the device itself is blocking the notifications. – AL.
1 upvote
  flag
@AL. it's an emulator with stock android 25 with google apis, it is working when the app is not forced closed though. Do you have any advice on how to debug this? also the same on a OnePlus 3T with android 7.1.1 – lelloman
upvote
  flag
same also with emulator with android 4.4 – lelloman

I was also facing the same issue, But then I realized after lots of debugging that, i was stopping the services that receive the Firebase notifications in on stop method of one of the activities.

  1. Please check whether you are stopping these services anywhere in the app.
  2. Make sure you are using service and not intent-service.
  3. Swiping the app will never stop services. So try to debug the app for first two point.

I've been through the same but in my case, it was Xiaomi phones instead of Oppo phones. What actually happens is that when you close the app from system tray, the system kills the app entirely. What that means is your app won't be able to receive notifications via GCM/FCM. WAKE_LOCK permission doesn't help either.

That does NOT mean that phone is not receiving the notification. It is. It just won't let the apps show it. You can verify this by sending a broadcast from adb and looking at your logcat.

One possible solution to this problem is to use SyncAdapter. Although it is NOT advised, I've seen some apps using it. Other possible solutions are to use some kind of background service which is always running. Some people also use AlarmManager as it almost never gets killed. My point is - you cannot rely on GCM/FCM for your notifications.

Let's talk about WhatsApp now -

In Xiaomi phones, they whitelist or blacklist an app based on certain criteria. If you download an app and if it is in their whitelist, they'll permit the app to show notifications. If not, you already know what happens. But the good thing is that you can change these settings. Look for an app named Security. If you revoke the right permissions, even WhatsApp will stop showing notifications.

upvote
  flag
Could you elaborate on how to take advantage of SyncAdapter to deliver notifications? We're already using SyncAdapter for syncing our data. How can we handle push notifications too? – SepehrM
upvote
  flag
@SepehrM I've only seen other apps using it, never used it myself so I don't really know. – thetanuj

Have you tried to use stopWithTask attribute on your service class?

<service
    android:name="com.yourapp.YourPushService"
    android:stopWithTask="false" />

If set to true, this service with be automatically stopped when the user remove a task rooted in an activity owned by the application. The default is false.

If the flag is false, there is an onTaskRemoved callback in your Service class.

In the case you can detect the "swipe" event, and you can implement a workaround.

1 upvote
  flag
Thanks... you saved my day – thiagolsilva
upvote
  flag
this worked for me. In the com.yourapp.YourPushService you need to enter your class which extends FirebaseMessagingService :) – dev.mi
upvote
  flag
thanks...its working – Mahesh Gawhane
upvote
  flag
It's work, thanks! – Mete
upvote
  flag
so set true or false ? for prevent kill service – grizzly
upvote
  flag
Didn't work on SonyZ3 with android 6.0 – Hamzeh Soboh
upvote
  flag
Doesn't work on Samsung Galaxy S3, Android 4.3). – Makalele
upvote
  flag
the default value of stopWithTask is false, so whats the point? – codepeaker

Answer was found here

There are no way to send data message from notification console.

But there are other way to send notification to devices and them will be catch inside onMessageReceived!

You need 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:

  1. Authorization - key=your_server_key_here
  2. Content-Type - application/json

To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key

enter image description here

enter image description here

When the app is closed, it shutdowns the service. You must to restart the service. See here

I tried to override the notification icon using meta-data tags. it worked for me.

<meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@mipmap/ic_launcher" />

<meta-data android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorPrimary" />
upvote
  flag
It doesn't work any one Not Try – Ashish Shahi

try this work for me.

My Problem

1) onMessageReceived() call when app is open

2) onMessageReceived() not call when app killed

  • after many research, I will find a solution

dependencies {

    compile 'com.google.firebase:firebase-core:11.2.0'
    compile 'com.github.erdalceylan:com-google-firebase-messaging:v1-11.2.0'
}

-use this dependency

There are two solutions:

  1. Enable auto start in device setting

    setting --> Apps --> click on gear wheel --> last option App select > 
    app auto launch --> find your app enable the switch
    
  2. Using Notifications payload

    • Using data

      "data" {
          "title": "Your Title",
          "description" :"App Description",
          "image" :"ImageURL",  
          "deeplink" :"deeplink",
          //... some other code
      }
      
    • Using notification

      "notification" {
          "title":"Your Title",
          "description" : "App Description",
          "click_action" :"Activity Action"
          //... some other code
      }
      

Found solutions here.

Not the answer you're looking for? Browse other questions tagged or ask your own question.