4

I am developing an Application which is using Push Notification (GCM) ,This application is receiving Push Noifications when app is running ,but when I forced stopped it from Settings ->apps->MyApp(click on it) and click on force stop ,then it is not receiving Push Notifications. I have tested same with WhatsApp it is receiving Push Notifications when I force stop it . So how can I implement same with my application .

Note : In code I am receiving PushNotifications in a sub class of WakefulBroadcastReceiver ,I have registered it statically in manifest even it is not called when application force stops.

public class GCM_Receiver extends WakefulBroadcastReceiver {
    //Processes Gcm message .
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "GCM_Receiver", Toast.LENGTH_LONG).show();
        ComponentName comp = new ComponentName(context.getPackageName(),GCMIntentService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

Edit : I have registered GCM_Receiver statically in this way :

<receiver
        android:name="com.myApp.GCM_Receiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.RETRY" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.myApp" />
        </intent-filter>
    </receiver>

Edit : And my GCMIntentService code is below :

public class GCMIntentService extends IntentService{
//Constructor with super().
public GCMIntentService () {
    super("GcmIntentService");
}
//Processes gcm messages .
@Override
protected void onHandleIntent(Intent intent) {
    Log.d("GCMIntentService ", "GCMIntentService Started");
    Toast.makeText(getApplicationContext(), "GCMIntentService Started", Toast.LENGTH_LONG).show();
    GCM_Receiver.completeWakefulIntent(intent);
}}

1 Answers1

1

Only Google Play services, through the com.google.android.c2dm.permission.SEND permission, can invoke that particular broadcast receiver. Since I assume that Play services does not include the FLAG_INCLUDE_STOPPED_PACKAGES flag when sending the broadcast, your force-stopped application will not receive the messages.

Interestingly, I don't receive any WhatsApp messages after force-stopping the app. Regardless of the app though, if it can receive messages while force stopped, then it definitely receives broadcasts from an intent with the FLAG_INCLUDE_STOPPED_PACKAGES flag.

Koh
  • 1,570
  • 1
  • 8
  • 6