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 {
@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{
public GCMIntentService () {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("GCMIntentService ", "GCMIntentService Started");
Toast.makeText(getApplicationContext(), "GCMIntentService Started", Toast.LENGTH_LONG).show();
GCM_Receiver.completeWakefulIntent(intent);
}}
source
share