Android上實現Push

來源:互聯網
上載者:User

這些天一直在琢磨如何在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 solutionDisadvantages: 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)以及電量消耗測試,因此只能說,這個可行,也易於實現。

 

最終我們要選擇哪種方式,現在還沒有明確的答案,不過也不遠了。。 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.