20

I use push notification (GCM) in my Android project.

According to GCM tutorial I implemented broadcast receiver and registered it in AndroidManifest.xml.

This kind of broadcast receivers should receive messages even if my app is closed (not only if my app is in background but even if it was force stopped).

But it doesn't work as I expect. onReceive() method isn't being called if the app is closed. It seems that my understanding of broadcast receivers is correct and the problem is in my expectations about GCM.

One of the possible reasons is that google server doesn't even send a notification to the device if the app is closed. So, is it correct that my app can receive a message (and onReceive() method will be invoked in broadcast receiver) only if it's running or in background (but not closed)?

Thanks in advance.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
tundundun
  • 634
  • 1
  • 5
  • 12
  • 2
    I have a same problem and I don't get the answer. did you solve it? – Mahdi Feb 21 '14 at 12:23
  • 1
    If you want to simulate your app closing normally (kind of), on your device, under developer options, choose 'Do Not Keep Activities'. Then whenever one of your activities enters the background it'll be closed. But your Broadcast Receiver will keep running – Fraser Dec 17 '14 at 20:24
  • @Fraser you're right, but an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately (http://developer.android.com/about/versions/android-3.1.html#launchcontrols). – John May 10 '16 at 16:20

3 Answers3

16

This kind of broadcast receivers should receive messages even if my app is closed (not only if my app is in background but even if it was force stopped).

If a user force-stops your app from Settings, your code will never ever run again, until something manually runs one of your components, typically the user manually launching an activity (as of Android 3.1). Hence, if the user force-stops your app from Settings, you will not receive GCM messages on newer devices/emulators.

So, is it correct that my app can receive a message (and onReceive() method will be invoked in broadcast receiver) only if it's running or in background (but not closed)?

There is no concept of "closed" in Android from an application standpoint. If, by "closed", you mean "has no running process, where the last process was terminated normally", then yes, you should receive GCM messages and other broadcasts. But, again, force-stop is not "terminated normally".

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    I just tested this using the [airbop-client](http://github.com/indigorose/airbop-client) and I can confirm that the onReceive() method was not called after the app was force stopped. I see a logcat entry for the c2dm.intent.RECEIVE tagged with GTalkService showing result=CANCELLED. – Lorne Laliberte Dec 04 '12 at 17:43
  • Does it exist a documentation from android about "force close" ? – mrroboaat Oct 21 '13 at 08:36
  • @CommonsWare I just sent an email after force stopping(from settings) gmail app and still got notification. "If a user force-stops your app from Settings, your code will never ever run again" How did Gmail send a notification? – Seshu Vinay Dec 27 '14 at 08:37
  • @SeshuVinay: You ran Gmail, and it sent your email. Apps will run when manually invoked after being force-stopped, but they will not respond to things like broadcasts. – CommonsWare Dec 29 '14 at 00:40
  • 1
    @CommonsWare No. I sent email from desktop. And I am sure gmail app wasn't running. Because Force stop button got disabled after I pressed it once. So no process was running. – Seshu Vinay Dec 29 '14 at 05:12
  • 1
    Just to make sure, I did this again. Force stopped Gmail app, Sent email from desktop, and got notification. BTW I tested this in Nexus 7(2012) device. Gmail version: 5.0.1(1642443) – Seshu Vinay Dec 29 '14 at 05:25
  • 1
    Same happened with Inbox app. I dont know if I am missing something. – Seshu Vinay Dec 29 '14 at 05:39
  • 1
    @SeshuVinay I too am facing the same problem , I am just curious as to whether you got the answer or not .This is the same case with watsapp too. – D V Ramana Dec 31 '14 at 09:42
  • No. I still didn't get any solution. – Seshu Vinay Dec 31 '14 at 10:11
  • I think it's only about having a process or not and it doesn't matter if it's forced to be stopped or system decided to stop it to free up space. – stdout Jan 11 '16 at 15:39
  • @Seshu Vinay Google might have tweaked their push handling system to notify Gmail and Inbox, using the FLAG_INCLUDE_STOPPED_PACKAGES flag. By default, you can't wake up an app that has been forced stopped. But if you add this flag to your intent, it should work. See: http://developer.android.com/about/versions/android-3.1.html#launchcontrols . If you test it, I'm interested. – John May 10 '16 at 16:29
3

According to Francesco Nerieri in this android-gcm thread:

So if you force stop the app, the intended behavior for ICS is for the app to not receive the message. In JB this means that GCM will also unregister the app, this is an unfortunate behavior and we are working to change this (the unregister part in JB).

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Evan
  • 749
  • 6
  • 11
  • They seem to have worked it out since. I tested sending a push to a forced close app and GCM responds '"success":1,"failure":0'. When the app is uninstalled, then it reponds that the registrationId is obsolete and should be unregistered (from your application server). – John May 10 '16 at 16:35
  • @John: Sucess: 1 means push is delivered to your device. If you connect your device to android studio, and check Android monitor, you will see something like: W/GCM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 – jee May 22 '18 at 13:32
2

In the documentation it says that :

An Android application on an Android device doesn't need to be running to receive messages. The system will wake up the Android application via Intent broadcast when the message arrives, as long as the application is set up with the proper broadcast receiver and permissions.

Check your broadcast receiver implementation and permissions.

Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
  • 3
    The docs only say "Wake up" which to me implies an app is "sleeping" or in the background. But when you force close an app it has no running process to "wake up", it needs to be started. – Pete Jul 28 '14 at 13:46