How can I receive notifications from GCM when appl

2019-01-17 13:17发布

问题:

GCMIntentService (extends GCMBaseIntentService) doesn't receive notifications if the application is not running.

From : http://developer.android.com/about/versions/android-3.1.html

Launch controls on stopped applications Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.

Is there a way to get around this setting? Thanks

回答1:

GCMIntentService (extends GCMBaseIntentService) doesn't receive notifications if the application is not running.

Yes, it will. The rest of your question, though, has little to do with whether the application is running, but rather whether it has been run by the user before. What the word "stopped" means in the paragraph you quote is not "not running", but represents a state that an application is in:

  • when it is first installed, before something manually invokes a component (e.g., user launches an activity)

  • after the user force-stops the app, until something manually invokes a component (e.g., user launches an activity)

(and I really really wish they had come up with a more distinctive adjective than "stopped" for this...)

Is there a way to get around this setting?

No. If the user force-stops your app, they are indicating that they do not want your app to run again, for any reason, until they manually launch it again. Your objective is to give the user no reason to force-stop your app. Note that I do mean "force-stop" (i.e., press the "Force Stop" button from Settings) -- ordinary task managers, or swiping from the Recent Tasks list in Android 4.x, does not have this effect.



回答2:

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