即使应用已关闭(slide/swiped 离开)也会收到 GCM 通知
Receive GCM notification even when the app is closed (slide/swiped away)
这是在我的 MainActivity 中用作 BroadcastReceiver 的代码
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/
通过滑动关闭应用程序(通常称为 滑动它)并不会完全终止应用程序。查看 Android 爱好者社区中的 answer 以获得更详细的描述。你可以看到那里提到:
..It won't directly causes services to stop..
由于 GCM 通知的侦听器为 Service
,这实际上意味着您的应用仍有可能继续接收它们,无论它是否为 一扫.
虽然滑动应用程序的结果也可能会有所不同取决于设备它是运行,但可以kill/force停止它或其他方式上面提到的,将停止后台进程。
但是,如果结果是,应用程序 killed/force 已停止,如上面 link 的回答中所述:
For stop is a complete kill of the app -- all processes are killed, all services stopped, all notifications removed, all alarms removed, etc.
这将导致该应用根本不会收到任何类型的通知,因为它是为 Android 自版本 3.1 设计的,如此 answer 中所述:
Apps that are in the stopped state do not receive broadcast Intents.
Stopped state is:
when the app is initially installed (before the user runs something in the app) or
after a Force Stop.
You can find more about this here: http://developer.android.com/about/versions/android-3.1.html#launchcontrols
希望这有助于以某种方式清除一些东西。干杯! :D
你可以试试 GcmListenerService
当您通过滑动关闭您的应用程序时,该服务仍在后台激活,您可以收到推送通知。除非用户手动强制停止应用程序设置,否则此服务可以帮助您。
这是在我的 MainActivity 中用作 BroadcastReceiver 的代码
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/
通过滑动关闭应用程序(通常称为 滑动它)并不会完全终止应用程序。查看 Android 爱好者社区中的 answer 以获得更详细的描述。你可以看到那里提到:
..It won't directly causes services to stop..
由于 GCM 通知的侦听器为 Service
,这实际上意味着您的应用仍有可能继续接收它们,无论它是否为 一扫.
虽然滑动应用程序的结果也可能会有所不同取决于设备它是运行,但可以kill/force停止它或其他方式上面提到的,将停止后台进程。
但是,如果结果是,应用程序 killed/force 已停止,如上面 link 的回答中所述:
For stop is a complete kill of the app -- all processes are killed, all services stopped, all notifications removed, all alarms removed, etc.
这将导致该应用根本不会收到任何类型的通知,因为它是为 Android 自版本 3.1 设计的,如此 answer 中所述:
Apps that are in the stopped state do not receive broadcast Intents.
Stopped state is:
when the app is initially installed (before the user runs something in the app) or after a Force Stop. You can find more about this here: http://developer.android.com/about/versions/android-3.1.html#launchcontrols
希望这有助于以某种方式清除一些东西。干杯! :D
你可以试试 GcmListenerService
当您通过滑动关闭您的应用程序时,该服务仍在后台激活,您可以收到推送通知。除非用户手动强制停止应用程序设置,否则此服务可以帮助您。