即使应用程序closures,也能收到GCM消息(滑动/滑动)

这里是我的MainActivity中用作BroadcastReceiver的代码

//Initializing our broadcast receiver mRegistrationBroadcastReceiver = new BroadcastReceiver() { //When the broadcast received //We are sending the broadcast from GCMRegistrationIntentService @Override public void onReceive(Context context, Intent intent) { //If the broadcast has received with success //that means device is registered successfully if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_SUCCESS)){ //Getting the registration token from the intent String token = intent.getStringExtra("token"); //Displaying the token as toast Toast.makeText(getApplicationContext(), "Registration token:" + token, Toast.LENGTH_LONG).show(); //if the intent is not with success then displaying error messages } else if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_ERROR)){ Toast.makeText(getApplicationContext(), "GCM registration error!", Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "Error occurred", Toast.LENGTH_LONG).show(); } } }; 

我跟着这个关于GCM的教程

https://www.simplifiedcoding.net/android-push-notification-using-gcm-tutorial/

通过滑动closures应用程序(更常见地称为滑动它 )并不完全杀死该应用程序。 在Android Enthusiasts社区中查看此答案以获取更详细的说明。 你可以看到那里提到:

..它不会直接导致服务停止..

由于GCM通知的监听器是Service ,所以这实际上意味着您的应用仍然可能继续接收它们,无论它被刷掉

虽然刷应用程序的结果也可能因运行的设备不同而不同,但如上所述,可能会终止或强制停止它或停止后台进程。

但是,如果结果是,应用程序被杀死/强制停止,正如上面链接的答案中所述:

对于停止是一个完整的应用程序的终止 – 所有进程被终止 ,所有服务停止 ,所有通知被删除,所有警报被删除等。

这将导致该应用程序根本没有收到任何types的通知,因为它是从3.1版本开始为Androiddevise的,正如在这个答案中所述 :

处于停止状态的应用程序不会收到广播意图。

停止状态是:

当应用程序最初安装(用户在应用程序中运行之前)或强制停止之后。 你可以在这里find更多关于这个: http : //developer.android.com/about/versions/android-3.1.html#launchcontrols

希望这有助于清除某些事情。 干杯! :d

您可以尝试使用GcmListenerService当您通过滑动closures应用程序时,服务仍在后台激活,您可以收到推送通知。 除非用户手动强制停止应用程序的设置,这项服务可以帮助你。