0

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);
    }
}
Simulant
  • 19,190
  • 8
  • 63
  • 98
Dylan
  • 11
  • 1
  • 3
  • you should try using it with service. – Amrut Bidri Jul 03 '15 at 07:12
  • Have you debugged your code and check whether your `onMessage()` method for notification is called or not when app is closed and getting any push notification. – Pankaj Jul 03 '15 at 07:19
  • @Amrut -Thank you, is it possible to elaborate ? I've looked into services but from what i'm seeing not sure how that would help. The BroadcastReceiver is invoking a service (IntentService) to post the notification, are you suggesting that i wrap the BroadcastReceiver itself in a service ? – Dylan Jul 03 '15 at 07:20
  • @Clairvoyant - Yes, neither the BroadcastReceiver.OnReceive() nor any methods in the IntentService are being invoked. Actually not sure which 'onMessage()' method though you are referring to. – Dylan Jul 03 '15 at 07:20
  • @Clairvoyant - regarding debugging, the debugger itself is detached when the app is closed by swiping in the Running App List so have not been able to debug under that condition, though tracers in the BroadcastReceiver stop getting output so its clear that onReceive() is not being called. – Dylan Jul 03 '15 at 07:31
  • @Dylan - I am asking if you are implementing `Push notification` then obviously you have to extends a class with `GCMBaseIntentService` in that class there is method `onMessage()` which would get called whenever a push notification arrive for the app. – Pankaj Jul 03 '15 at 07:35
  • @Clairvoyant - I had never heard of "GCMBaseIntentService", looked it up and it appears to be decprecated, however that helped to discover GCMReceiver and GCMListenerService, two classes that must be relatively new (since i first implemented as in OP a couple of years ago). I will look into those and post back here. Thank you for your help. – Dylan Jul 03 '15 at 14:14
  • @Dylan - Could you solve the issue? I am stuck with the same problem. If you could , can you plesae post the answer? – Sahana Prabhakar Jan 18 '17 at 03:32

1 Answers1

0

It looks like you stumbled about the same issue I did and I guess using GCMReceiver and GCMListenerService didn't work for you too. This problem is a reported bug. Hopefully the workarounds in the ticket will help you.

Community
  • 1
  • 1
mheymel
  • 209
  • 3
  • 7