Android GCM(推送通知):如果应用程序停止,设备不会收到通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12073449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android GCM (push notification): device doesn't receive notification if application is stopped
提问by tundundun
I use push notification (GCM) in my Android project.
我在我的 Android 项目中使用推送通知 (GCM)。
According to GCM tutorial I implemented broadcast receiver and registered it in AndroidManifest.xml
.
根据 GCM 教程,我实现了广播接收器并在AndroidManifest.xml
.
This kind of broadcast receivers should receive messages even if my app is closed (not only if my app is in background but even if it was force stopped).
即使我的应用程序关闭,这种广播接收器也应该接收消息(不仅当我的应用程序在后台而且即使它被强制停止)。
But it doesn't work as I expect. onReceive()
method isn't being called if the app is closed. It seems that my understanding of broadcast receivers is correct and the problem is in my expectations about GCM.
但它不像我预期的那样工作。onReceive()
如果应用程序关闭,则不会调用方法。看来我对广播接收器的理解是正确的,问题出在我对 GCM 的期望中。
One of the possible reasons is that google server doesn't even send a notification to the device if the app is closed. So, is it correct that my app can receive a message (and onReceive()
method will be invoked in broadcast receiver) only if it's running or in background (but not closed)?
可能的原因之一是,如果应用程序关闭,谷歌服务器甚至不会向设备发送通知。那么,仅当我的应用程序onReceive()
正在运行或在后台(但未关闭)时,它才能接收消息(并且方法将在广播接收器中调用)是否正确?
Thanks in advance.
提前致谢。
采纳答案by CommonsWare
This kind of broadcast receivers should receive messages even if my app is closed (not only if my app is in background but even if it was force stopped).
即使我的应用程序关闭,这种广播接收器也应该接收消息(不仅当我的应用程序在后台而且即使它被强制停止)。
If a user force-stops your app from Settings, your code will never ever run again, until something manually runs one of your components, typically the user manually launching an activity (as of Android 3.1). Hence, if the user force-stops your app from Settings, you will not receive GCM messages on newer devices/emulators.
如果用户从“设置”强制停止您的应用程序,您的代码将永远不会再次运行,直到手动运行您的组件之一,通常是用户手动启动活动(从 Android 3.1 开始)。因此,如果用户从“设置”强制停止您的应用程序,您将不会在较新的设备/模拟器上收到 GCM 消息。
So, is it correct that my app can receive a message (and onReceive() method will be invoked in broadcast receiver) only if it's running or in background (but not closed)?
那么,仅当我的应用程序正在运行或在后台(但未关闭)时,它才能接收消息(并且 onReceive() 方法将在广播接收器中调用)是否正确?
There is no concept of "closed" in Android from an application standpoint. If, by "closed", you mean "has no running process, where the last process was terminated normally", then yes, you should receive GCM messages and other broadcasts. But, again, force-stop is not "terminated normally".
从应用程序的角度来看,Android 中没有“封闭”的概念。如果“关闭”是指“没有正在运行的进程,最后一个进程正常终止”,那么是的,您应该收到 GCM 消息和其他广播。但是,同样,强制停止不会“正常终止”。
回答by Evan
According to Francesco Nerieri in this android-gcm thread:
根据 Francesco Nerieri 在这个 android-gcm 线程中的说法:
So if you force stop the app, the intended behavior for ICS is for the app to not receive the message. In JB this means that GCM will also unregister the app, this is an unfortunate behavior and we are working to change this (the unregister part in JB).
因此,如果您强制停止应用程序,ICS 的预期行为是应用程序不接收消息。在 JB 中,这意味着 GCM 也将取消注册应用程序,这是一个不幸的行为,我们正在努力改变这一点(JB 中的取消注册部分)。
回答by Parvin Gasimzade
In the documentationit says that :
在文档中它说:
An Android application on an Android device doesn't need to be running to receive messages. The system will wake up the Android application via Intent broadcast when the message arrives, as long as the application is set up with the proper broadcast receiver and permissions.
Android 设备上的 Android 应用程序无需运行即可接收消息。只要应用程序设置了适当的广播接收器和权限,系统就会在消息到达时通过 Intent 广播唤醒 Android 应用程序。
Check your broadcast receiver implementation and permissions.
检查您的广播接收器实现和权限。