java 如何在后台继续监听 Android 上的推送通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12427171/
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
How to keep Listening for Push Notifications on Android in the background
提问by Shax
I am working on Push Notifications in Android. Now the issue is that I want to keep running my Push Notifications on the back ground as soon as the app start because I have no idea when the server will push the data to the devices.
我正在研究 Android 中的推送通知。现在的问题是,我想在应用程序启动后立即在后台继续运行我的推送通知,因为我不知道服务器何时将数据推送到设备。
The main requirement is that our corporate app is having more than 10 activities and based on the notification received, I have to bring the related activity on the foreground so that user can preform action on that or do some silent action in the background regardless the activity is in foreground.
主要要求是我们的企业应用程序有超过 10 个活动,并且根据收到的通知,我必须将相关活动放在前台,以便用户可以对其执行操作或在后台执行一些静音操作,而不管活动如何在前台。
Can somebody suggest how can I implement this type of requirement. Do I need to do it in a Service.
有人可以建议我如何实现这种类型的要求。我是否需要在服务中执行此操作。
Thanks
谢谢
回答by Shiva
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 应用程序。
take look at this;
看看这个;
http://developer.android.com/guide/google/gcm/gcm.html
http://developer.android.com/guide/google/gcm/gcm.html
when message received from gcm server
从 gcm 服务器收到消息时
onMessage(Context context, Intent intent): method of GCMIntentService gets fire,
onMessage(Context context, Intent intent):GCMIntentService 方法被触发,
so you write your code there
所以你在那里写你的代码
take sample example from here
从这里获取示例示例
回答by Raghav Sood
What you're trying to do defeats the purpose of push notifications. In push notifications, the server sends the message through Google APIs. These APIs then send a broadcast message to your app, which you listen for. Continuously keeping the app open in the background and asking the server for new messages is called polling.
您尝试做的事情违背了推送通知的目的。在推送通知中,服务器通过 Google API 发送消息。然后,这些 API 向您的应用程序发送广播消息,您可以监听该消息。在后台持续保持应用程序打开并向服务器询问新消息称为轮询。
Read up on the GCM documentation. Whenever you receive a message, Android will ca the onMessage(); method of your GCMIntentService.
阅读GCM 文档。每当您收到消息时,Android 都会调用 onMessage();您的 GCMIntentService 的方法。