我正在开发一款应用程序,使用Push Notification(GCM)。当应用程序运行时,它会接收到推送通知,但是当我从“设置->应用程序->MyApp”中强制停止它后,它就不能接收到推送通知了。我已经测试了同样的问题,WhatsApp在我强制停止它时可以接收到推送通知。那么我该如何实现我的应用程序也能做到这一点呢?
注意:在代码中,我在WakefulBroadcastReceiver的子类中接收推送通知,并在清单中静态注册它,即使应用程序被强制停止,它也不会被调用。
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);
}
}
编辑: 我已经以静态方式注册了GCM_Receiver:
<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>
编辑: 以下是我的GCMIntentService代码:
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);
}}