简体   繁体   English

如何在后台继续在Android上监听推送通知

[英]How to keep Listening for Push Notifications on Android in the background

I am working on Push Notifications in Android. 我正在研究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. 现在的问题是,我想在应用程序启动后立即在后台运行推送通知,因为我不知道服务器何时将数据推送到设备。

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

An Android application on an Android device doesn't need to be running to receive messages. Android设备上的Android应用程序无需运行即可接收消息。 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. 只要应用程序设置了正确的广播接收器和权限,系统就会在消息到达时通过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 从这里拿样例

https://github.com/ketanpatel25/GCM-Demo/tree/master/gcm https://github.com/ketanpatel25/GCM-Demo/tree/master/gcm

What you're trying to do defeats the purpose of push notifications. 您尝试做的事情会破坏推送通知的目的。 In push notifications, the server sends the message through Google APIs. 在推送通知中,服务器通过Google API发送消息。 These APIs then send a broadcast message to your app, which you listen for. 然后,这些API会向您的应用发送广播消息,您可以收听该消息。 Continuously keeping the app open in the background and asking the server for new messages is called polling. 在后台持续保持应用程序打开并向服务器询问新消息称为轮询。

Read up on the GCM documentation . 阅读GCM文档 Whenever you receive a message, Android will ca the onMessage(); 每当收到消息时,Android都会出现onMessage(); method of your GCMIntentService. GCMIntentService的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM