博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android上实现Push
阅读量:6038 次
发布时间:2019-06-20

本文共 2831 字,大约阅读时间需要 9 分钟。

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

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

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

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: 
  • 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: 
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. 

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

 

 

 

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

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

 

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

 

你可能感兴趣的文章
腰间盘突出方(刘力红)
查看>>
C#加密解密方法(转)
查看>>
code only 和 code first的关系 !! code only 就是 code first !!
查看>>
WCF入门(九)——未处理异常
查看>>
集合划分问题
查看>>
程序执行vhdl中延时器的编写
查看>>
导致flash屏幕重绘的几种方式及避免重绘的方法
查看>>
解读思维导图(一)误区
查看>>
[2014AMC]Navier-Stokes equations with regularity in two entries of the velocity gradient tensor
查看>>
java多线程:ReentrantReadWriteLock读写锁使用
查看>>
salesforce 零基础开发入门学习(三)sObject简单介绍以及简单DML操作(SOQL)
查看>>
OGG常见问题处理
查看>>
Mysql 自增列 主键
查看>>
Android Studio中提示:Project SDK is not defined
查看>>
NOIP2015pj求和
查看>>
ASP.NET Core 中文文档 第四章 MVC(2.1)模型绑定
查看>>
【Linux】rpm常用命令及rpm参数介绍
查看>>
能上架App的GooglePlay开发者账号获取流程
查看>>
WIN7 WIN10赋予文件或者文件夹完全访问权限
查看>>
Myeclipse创建Maven项目
查看>>