即使应用程序关闭(滑动/滑动)也会收到 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 爱好者社区中的这个 answer 以获得更详细的说明.你可以看到那里提到:

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天全站免登陆