Android上实现Push

这些天一直在琢磨如何在android device上实现一套Push功能,也google很多资料,看似无外乎以下三个方案(以下转载):

1)轮询:应用程序应当阶段性的与服务器进行连接并查询是否有新的消息到达,你必须自己实现与服务器之间的通信,例如消息排队等。而且你还要考虑轮询的频率,如果太慢可能导致某些消息的延迟,如果太快,则会大量消耗网络带宽和电池。

2)SMS:在Android平台上,你可以通过拦截SMS消息并且解析消息内容来了解服务器的意图。这是一个不错的想法,我就见过采用这个方案的应用程序。这个方案的好处是,可以实现完全的实时操作。但是问题是这个方案的成本相对比较高,你很难找到免费的短消息发送网关,关于这个方案的实现,可以参考如下链接:https://labs.ericsson.com/apis/mobile-java-push/

3)持久连接:这个方案可以解决由轮询带来的性能问题,但是还是会消耗手机的电池。Apple的推送服务之所以工作的很好,是因为每一台手机仅仅保持一个与服务器之间的连接,事实上C2DM也是这么工作的。不过这个方案也存在不足,就是我们很难在手机上实现一个可靠的服务。Android操作系统允许在低内存情况下杀死系统服务,所以你的通知服务很可能被操作系统Kill掉了 

  • Poll? The name obviously tells you that it’s really not even push. The idea here is to periodically poll the server for new messages from a background local or remote service. The more often you poll the closer you get to the real-time push.
Advantages: easy to implement. no cost solution
Disadvantages: Obviously, you will never be actually real-time. If you polling interval is 30 min, you can get a message that is 29 minutes and 59 seconds late. Moreover, polling more often than every 15-30 min will kill your battery pretty quickly: https://labs.ericsson.com/apis/mobile-java-push/blog/save-device-battery-mobile-java-push
  • SMS Android allows you to intercept SMS messages. Your server sends a specially encoded SMS to your phone, whenever there is something new. Your app intercepts all messages, looks for the ones from the server, then pops up a notification.
Advantages: easy to implement. Fully real-time updates. Known drop-in solutions exist such as one provided by Ericsson  Labs: https://labs.ericsson.com/apis/mobile-java-push/
Disadvantages: Can be costly to you and the user. There are only a few services that allow you send around free SMS and even those are often limited to North America. If you want to have a reliable SMS-based service that is available worldwide, you will likely need to pay. Similar goes for the users. Not everyone has an SMS plan and you don’t want your users getting charged by 3rd party for using your app.
  • Persistent TCP/IP The phone initiates a long-lived mostly idle TCP/IP connection with the server and maintains it by occasionally sending keepalive messages. Whenever there is something new on the server, it sends a messages to the phone over the TCP connection.
Advantages: Fully real-time updates.

Disadvantages: Hard to implement a reliable service on both the phone and the server side. The Android OS is known to be able to kill services when it’s running low on memory, so your notifications service can easily disappear. What happens when your phone goes to sleep? Some people complain about battery life issues related to maintaining an active connection. 

 这里是两篇我觉得说得很好的网址:

http://tokudu.com/2010/how-to-implement-push-notifications-for-android/ 

http://blog.csdn.net/joshua_yu/article/details/6563587 

 

当然,我的第一选择也是尝试C2DM,在注册完C2DM服务的账号后,google发的“AC2DM invitation”邮件里有提到仍处在试验中,加上我们要实现的产品需要一个稳定环境,所以尝试至此,也就中途放弃了,有兴趣的可以继续:http://code.google.com/intl/zh-CN/android/c2dm/#intro


之后,我开始尝试使用openfire+smack来自己搭建一个Push platform,用openfire作为Push service,然后借用anroidpn开源工程代码(做了一定的修改),能够做到从Server Push信息到android client。对于这套方案,我并没有做并发测试(这里有提到:http://wenku.baidu.com/view/85cfcd5f312b3169a451a41f.html)以及电量消耗测试,因此只能说,这个可行,也易于实现。

 

最终我们要选择哪种方式,现在还没有明确的答案,不过也不远了。。 

 

posted on 2011-12-06 12:15  xirihanlin  阅读(11392)  评论(5编辑  收藏  举报