即使应用程序关闭(滑动/滑动)也能接收 GCM 通知 [英] Receive GCM notification even when the app is closed (slide/swiped away)

本文介绍了即使应用程序关闭(滑动/滑动)也能接收 GCM 通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is the code that is used as BroadcastReceiver inside my MainActivity

    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 的教程

I followed this tutorial about GCM

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

推荐答案

通过滑动关闭应用(通常称为滑动它)并不完全杀死应用程序.查看 Android 爱好者社区中的此答案,了解更详细的说明.你可以看到那里提到:

Closing the app by sliding it (more commonly known as swiping it away) doesn't totally kill the app. Check out this answer in Android Enthusiasts community for a more detailed description. You can see there that it is mentioned that:

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

..It won't directly causes services to stop..

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

Since listeners for GCM notifications are Services, this would actually mean that there is still a possibility that your app may still continue to receive them regardless if it is swiped away.

虽然滑动应用的结果也可能会有所不同取决于正在运行的设备,但可能会杀死/强制停止它,或者如上所述,会停止后台进程.

Though the result of swiping an app may also differ depending on the device it is running, one may kill/force stop it or other just as mentioned above, will stop background processes.

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

If the result is, however, the app is killed/force stopped, as mentioned in the answer from the link above:

对于停止是完全终止应用程序 - 所有进程都被终止,所有服务停止,所有通知被移除,所有警报被移除,等等.

For stop is a complete kill of the app -- all processes are killed, all services stopped, all notifications removed, all alarms removed, etc.

这将导致应用程序根本不会收到任何类型的通知,因为它是从 3.1 版开始为 Android 设计的,如本 回答:

Which would result for the app to don't receive any kind of notifications at all, as it was designed for Android since version 3.1, as stated in this answer:

处于停止状态的应用不会收到广播 Intent.

停止状态是:

最初安装应用时(在用户在应用中运行某些内容之前)或强制停止后.您可以在此处找到更多相关信息:http://developer.android.com/about/versions/android-3.1.html#launchcontrols

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

Hope this helps clear some things somehow. Cheers! :D

这篇关于即使应用程序关闭(滑动/滑动)也能接收 GCM 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆