即使应用程序关闭(滑动/滑动),也可以接收GCM消息 [英] Receive GCM messages even when app is closed (slide/swiped away)

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

问题描述

  //初始化我们的广播接收器
mRegistrationBroadcastReceiver = new BroadcastReceiver(){

//当广播收到
//我们从GCMRegistrationIntentService发送广播

@覆盖
public void onReceive (Context context,Intent intent){
//如果广播接收成功
//表示设备已成功注册
if(intent.getAction()。equals(GCMRegistrationIntentService.REGISTRATION_SUCCESS )){
//从意图获取注册标记
String标记= intent.getStringExtra(token);
//显示令牌作为toast
Toast.makeText(getApplicationContext(),注册令牌:+令牌,Toast.LENGTH_LONG).show();

//如果意图不成功,则显示错误消息
} else if(intent.getAction()。equals(GCMRegistrationIntentService.REGISTRATION_ERROR)){
Toast.makeText (getApplicationContext(),GCM注​​册错误!,Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),发生错误,Toast.LENGTH_LONG).show();
}
}
};

我跟着这篇关于GCM的教程

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

解决方案

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


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


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

尽管刷卡的结果一个应用程序也可能有所不同取决于它正在运行的设备,可能会停止/强制停止它或其他,就像上面提到的一样,它会停止后台进程。



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


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


哪一个会导致fo该应用根本没有收到任何类型的通知,因为它是为版本3.1以后的Android设计的,正如此回答中所述


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



停止状态为:



当应用程序初始安装时(用户在应用程序中运行某些内容之前)或$ b部队停止后$ b。
您可以在这里找到更多关于此的信息: http:// developer.android.com/about/versions/android-3.1.html#launchcontrols


希望这有助于澄清一些事情不知何故。干杯! :D

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

 //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();
            }
        }
    };

I followed this tutorial about GCM

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

解决方案

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..

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.

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:

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

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

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

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