In attempting to implement notifications in an android app, I am seeing that the notifications no longer work if a user closes the app from the Running App List (top swipe down and then horizontal swipe - not force quit which properly would shut down all future invocations as per google specs). Using the back button sometimes causes the same behaviour, though not always.
My understanding from everything that I have researched is that only a force stop by the user should stop the Broadcast Receiver from being invoked.
Here is the relevant code and settings from the Android Manifest, note that all works fine when the app is open or left via the home button.
Android Manifest Relevant Sections:
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver
android:name="mypackage.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="mypackage.android" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="mypackage.android" />
</intent-filter>
</receiver>
<service
android:name="mypackage.gcm.GCMIntentService"
android:stopWithTask="false"
android:exported="false">
</service>
Here is my class that overrides WakefulBroadcastReceiver
:
public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
try {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(), GCMIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
} catch(Exception e) {
//Log it, and move on
Log.d("GCMBroadcastReceiver", "onReceive", e);
}
}