【问题标题】:Receive GCM notification even when the app is closed (slide/swiped away)即使应用程序关闭(滑动/滑动)也能接收 GCM 通知
【发布时间】:2016-09-18 08:55:07
【问题描述】:

这是在我的 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/

【问题讨论】:

  • 我认为通过滑出关闭应用程序是强制关闭应用程序,因此除非用户再次启动应用程序,否则它不会收到 GCM 消息。请看我之前的回答SO question
  • 我是用 WakefulBroadcastReceiver 做到的
  • 无论您的应用程序是打开还是关闭,您都可以接收 GCM(现为 FCM)消息。我建议不要编写自己的 WakefulBroadcastReceiver,请使用提供的 GcmListenerService 或更容易使用的 FirebaseMessagingService。查看示例:github.com/firebase/quickstart-android/tree/master/messaging
  • @acer 你是怎么解决的?

标签: android android-studio push-notification firebase-cloud-messaging google-cloud-messaging


【解决方案1】:

通过滑动关闭应用(通常称为将其滑开)并不能完全杀死应用。查看 Android Enthusiasts 社区中的 answer 以获得更详细的说明。你可以看到那里提到:

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

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

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

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

对于停止是完全杀死应用程序 - 所有进程都被杀死,所有服务停止,所有通知被删除,所有警报被删除,等等。

这将导致该应用根本收不到任何通知,因为它是从 3.1 版开始为 Android 设计的,如 answer 中所述:

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

停止状态是:

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

希望这有助于以某种方式清除一些东西。干杯! :D

【讨论】:

    【解决方案2】:

    你可以试试GcmListenerService 当您通过滑出关闭应用程序时,该服务仍会在后台激活,您可以收到推送通知。除非用户手动强制停止应用程序设置,否则此服务可以帮助您。

    【讨论】:

    • 有些设备被设计成当您滑动关闭应用程序时,它与强制停止它们相同(请参阅我的答案)。
    • 还有一些设备,即使应用程序仍然只是简单地刷掉,即使它没有强制关闭,设备本身也会阻止它接收消息。其他人则说,情况并非如此,因为 WhatsApp 等应用程序能够做到这一点。到目前为止,我之所以了解到这一点,是因为设备制造商已将大多数知名应用程序列入白名单,以使其成为可能。这在任何地方都没有记录,因为 (IMO),这个主题也取决于设备并且 FCM 无法完全控制。
    猜你喜欢
    • 2016-09-30
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    相关资源
    最近更新 更多