20
votes

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.

3
I have a same problem and I don't get the answer. did you solve it?Mahdi
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 runningFraser
@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 (developer.android.com/about/versions/…).John

3 Answers

16
votes

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

3
votes

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

2
votes

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.