Receive push noification when the application was stopped. (Android)

I am developing an application using Push Notification (GCM). This application receives Push Noifications when the application starts, but when I forcibly stopped it from Settings → apps-> MyApp (click on it) and click on the stop of the forced action, after which it will not receive Push notifications. I tested the same with WhatsApp when it receives push notifications, when I force it to stop. So how can I implement it with my application.

Note . In the code, I get PushNotifications in a subclass of WakefulBroadcastReceiver, I registered it statically in the manifest, even if it is not called when the application’s power ceases.

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 registered GCM_Receiver statically 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);
}}
+4
source share
1 answer

Only Google Play services, through permission, com.google.android.c2dm.permission.SENDcan call this particular broadcast receiver. Since I believe that Play Services does not include the FLAG_INCLUDE_STOPPED_PACKAGES flag when sending a broadcast, the force stop application will not receive messages.

, WhatsApp . , , , , FLAG_INCLUDE_STOPPED_PACKAGES.

+1

Source: https://habr.com/ru/post/1589032/


All Articles