Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GCM (push notification): device doesn't receive notification if application is stopped

Tags:

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.

like image 379
tundundun Avatar asked Aug 22 '12 12:08

tundundun


People also ask

How does GCM push notification work?

The first step in GCM is that a third-party server (such as an email server) sends a request to Google's GCM server. This server then sends the message to your device, through that open connection. The Android system looks at the message to determine which app it's for, and starts that app.

Why do I not receive push notifications?

Settings > Sounds & Vibration > Do Not Disturb: if this setting is enabled, Push Notifications will not be received. Make sure this is disabled. Settings > General > Background App Refresh: this setting allows the app to run in the background and must be turned on.

How do I force push notifications on Android?

Go to Settings > Notifications & status bar > Notification Manager > select the app and turn on "Allow Notifications", "Display on Lockscreen", "Display at the top of the screen", "Notification Sound and Vibration", and "Priority notification". Then, restart your phone.


2 Answers

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

like image 118
CommonsWare Avatar answered Nov 11 '22 11:11

CommonsWare


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

like image 34
Evan Avatar answered Nov 11 '22 11:11

Evan