C2DM原理和入門(一種面向Android系統的server push方式)

Introduction

Android Cloud toDevice Messaging (C2DM):

·        It allows third-party application servers to send lightweightmessages to their Android applications. The messaging service is not designedfor sending a lot of user content via the messages. Rather, it should be usedto tell the application that there is new data on the server, so that theapplication can fetch it.

·        這個由google提供的服務允許讓我們假設的服務器把消息主動的發送(PUSH)給android端的應用程序。不過發送的數據一般是比較簡短的提示信息(notification)。所謂提示信息就是告訴客戶端服務端有新消息,希望他現在去取。所以客戶端程序收到這個請求後,給服務器發送請求某某信息的請求,然後服務器會通過傳統的響應方式發回數據。

·        C2DM makes no guarantees about delivery or the order ofmessages. So, for example, while you might use this feature to tell an instantmessaging application that the user has new messages, you probably would notuse it to pass the actual messages.

·        應爲C2DM並不能保證信息傳遞的無誤性,完整性和多個信息的次序。所以建議編程者不要用C2DM直接推送實際的數據,而只是發送一個提示信息(這裏參考上一條的內容)。

·        An application on an Android device doesn’t need to be runningto receive messages. The system will wake up the application via Intentbroadcast when the the message arrives, as long as the application is set upwith the proper broadcast receiver and permissions.

·        Android端的應用程序不需要爲了接受push來的信息而一直保持運行狀態,因爲一般情況下,當手機系統受到push來的消息的時候,系統會自己打開對應的應用程序來處理這個消息。(不過這種允許程序被動打開的方式需要程序進行一些額外的設置)

·        It does not provide any built-in user interface or otherhandling for message data. C2DM simply passes raw message data receivedstraight to the application, which has full control of how to handleit. For example, the application might post a notification, display acustom user interface, or silently sync data.

·        C2DM本身沒有給用戶提供什麼內部接口或者其他的處理信息的方法。C2DM的工作就是簡單的把信息發給客戶端程序。所以用戶還是別想着在服務本身上做什麼文章了。

·        It requires devices running Android 2.2 or higher that also havethe Market application installed. However, you are not limited to deploying yourapplications through Market.

·        使用這個服務的android設備終端需要:Android版本在2.2以上,並且終端上安裝了Android Market程序。(但是用戶開發的程序不用必須從Android Market發佈)。

·        It uses an existing connection for Google services. Thisrequires users to set up their Google account on their mobile devices.

·        要使用這個服務需要Android終端上設置Google賬戶並且可以連接網絡。

Architectural Overview

This section gives anoverview of how C2DM works.

這一部分內容簡單的介紹了C2DM的工作原理

This table summarizesthe key terms and concepts involved in C2DM. It is divided into thesecategories:

·        Components — The physical entities thatplay a role in C2DM.

·        Credentials — The IDs and tokens that areused in different stages of C2DM to ensure that all parties have beenauthenticated, and that the message is going to the correct place.

·         

·        下面的表格中是C2DM將會涉及到的設備和ID的解釋

·        Components :在C2DM的服務中需要的實際設備

·        CredentialsC2DM中需要用到的ID號,因爲使用C2DM需要開發者先爲程序做一些ID認證的工作,這樣才能保證服務能正確的把消息推送到正確的終端上正確的應用程序中。

·         

Components

Mobile Device

移動設備

The device that is running an Android application that uses  C2DM. This must be a2.2 Android device that has Market installed, and it  must have at least one logged in Google account.

安裝了Android2.2以上的版本的系統。並要登錄Google賬號

Third-Party Application Server

需要PUSH信息的服務器

An application server that developers set up as part of  implementing C2DM in theirapplications. The third-party application server  sends data to an Android applicationon the device via the C2DM server.

負責push信息給移動終端的服務器

C2DM Servers

GoogleC2DM服務器

The Google servers involved in taking messages from the  third-party applicationserver and sending them to the device.

GoogleC2DM服務器,C2DM服務的實際完成者,C2DM工作的時候,這臺服務器會接收應用程序服務器提交的想要PUSH的信息並根據服務器給的ID發送給移動終端

Credentials

Sender ID

An email account associated with the application's developer.  The sender ID is used in the registration process to identify  a Android application that is permitted to send messages to the device. This  ID is typically role-based rather than being a personal account—- for  example, [email protected].

這個ID是一個郵箱賬號,服務註冊階段中需要,一般開發者會爲某個應用專門註冊一個郵箱來作爲Sender ID

Application ID

The application that is registering to receive messages. The  application is identified by the package name from the manifest. This ensures  that the messages are targeted to the correct application.

相當於一個針對應用程序的唯一ID號,而這個ID號的內容是通過manifest獲取的packege  name:比如com.app.xxx。當C2DM服務器PUSH信息的時候,可以根據這個ID找到正確的應用程序。

Registration ID

An ID issued by the C2DM servers to the Android application  that allows it toreceive messages. Once the application has the registration  ID, it sends it to the third-party application server, which uses it to identify  each device that has registered to receive messages for a given application.  In other words, a registration ID istied to a particular application running  on a particular device.

註冊ID:當手機A上使用了C2DM服務的應用程序運行的時候就會聯繫GoogleC2DM服務器,並要到一個ID號,這個ID號對應某個終端的某個應用(終端不一樣,應用不一樣,ID都會不同)。應用程序獲得這個ID後會把ID發給server(相當於告server:我是你裝了你的客戶端的手機,你有新的信息就發給我把)。

Google User Account

For C2DM to work, the mobile device must include at least one  logged in Googleaccount.

Google賬戶,一個手機或者pad要使用C2DM服務就必須先登錄Google賬號

Sender Auth Token

A ClientLogin Auth token that is saved on the third-party  application server that gives the application server authorized access to  Google services. The token isincluded in the header of POST requests that  send messages. For more discussionof ClientLogin Auth tokens, see ClientLogin for Installed Applications.

A ClientLogin Auth token:保存在服務端的一個信息,這個信息給了server使用google  C2DM的權限,serverpush消息給客戶端的時候,就在發給Google C2DM服務器的數據的頭中包含這個認證信息,google服務器覈對了這個認證信息後就會接受數據並按照server的期望把數據發送到移動終端上。

Lifecycle Flow

工作流程

Here are the primaryprocesses involved in cloud-to-device messaging:

下面是實現一次C2DM服務的3個步驟:

·        Enabling C2DM. An Androidapplication running on a mobile device registers to receive messages.

·        google上爲android應用程序註冊,獲得試用C2DM的權限。註冊時候需要填寫的內容包括Sender IDApplication ID

·        Sending a message. A third-partyapplication server sends messages to the device.

·        發送消息:想要把信息PUSHAndroid終端上的server把想發的消息發送給googleC2DM服務器。

·        Receiving a message. An Androidapplication receives a message from a C2DM server.

·        接收消息:應用程序接收C2DM服務器發來的消息。

These processes aredescribed in more detail below.

下面更加詳細的介紹每個步驟

Enabling C2DM

註冊獲得C2DM使用權

This is the sequence of events that occurs when an Androidapplication running on a mobile device registers to receive messages:

下面的流程是一個移動設備上的應用程序要註冊C2DM服務時候經歷的流程:

1.      The first time the application needs to use the messaging service,it fires off a registration Intent to a C2DM server.

This registration Intent (com.google.android.c2dm.intent.REGISTER) includes thesender ID (that is, the account authorized to send messages to the application,which is typically the email address of an account set up by the application'sdeveloper), and the application ID.

首先,程序會利用手機的發送信息的服務給googleC2DM服務器發送一個想要申請C2DM的請求,請求的內容包括sender ID(開發者註冊的郵箱),和ApplicationIDpackage名形式的ID)。

2.      If the registration is successful, the C2DM server broadcasts a REGISTRATION Intent which gives the application a registration ID.

The application should store this ID for later use. Note thatGoogle may periodically refresh the registration ID, so you should design yourapplication with the understanding that the REGISTRATION Intent may be called multiple times. Your applicationneeds to be able to respond accordingly.

如果註冊過程是成功的,那麼C2DM會返回一個registration ID(綁定了一臺移動設備和移動設備上某個應用的序列號)。終端設備收到這個ID後需要存起來以後使用,而且要注意這個ID以後google會改變並把新的發給終端,所以終端程序要能夠動態的接收這個ID並存儲下來。

3.      To complete the registration, the application sends theregistration ID to the application server. The application server typicallystores the registration ID in a database.

The registration IDlasts until the application explicitly unregisters itself, or until Google refreshesthe registration ID for your application.

註冊流程的最後一步是應用程序把自己獲得的registration ID發送給應用程序的serverServer收到這個ID後會存到數據庫中。當應用程序在服務端請求撤銷註冊(unregister)或者Google發來了新的registrationID的時候,數據庫中的老的registrationID纔會被server刪除。

Sending a Message

消息的發送

For an application serverto send a message, the following things must be in place:

對於一個應用程序的服務器,下面的內容必須準備好

·        The application has a registration ID that allows it to receivemessages for a particular device.

·        一個應用程序已經獲得了一個registration ID(和某個設備上的某個應用綁定的ID),使得這個應用可以爲某個設備接收消息。

·        The third-party application server has stored the registrationID.

·        這個應用程序的服務器必須已經存放了這個應用申請的registrationID

There is one more thing that needs to be in place for theapplication server to send messages:

另外還有一個準備工作需要服務器做好:

ClientLogin authorization token.This is something that the developer must have already set up on theapplication server for the application (for more discussion, see Role of the Third-PartyApplication Server). Now it will getused to send messages to the device.

一個ClientLogin authorization token(一個訪問權限),這個東西是開發者提前在應用服務器上設置好的。並且這個權限會被用來發送信息到移動終端上。

The ClientLogin tokenauthorizes the application server to send messages to a particular Androidapplication. An application server has one ClientLogin token for a particular3rd party app, and multiple registration IDs. Each registration ID represents aparticular device that has registered to use the messaging service for aparticular 3rd party app.

ClientLogin authorization token允許服務器給某個指定的android應用發送消息。一個服務器對於一個應用程序會存放一個唯一的ClientLogin authorization token,但是服務器上會對這個程序存放多個registrationID(因爲每一個registrationID對應一個設備和設備上的一個應用程序,多個裝了同一個應用的設備自然會有多個registrationID)。

Here is the sequenceof events that occurs when the application server sends a message:

下面是服務器發送消息的整個過程:

1.      The application server sends a message to C2DM servers.

1.應用程序的服務器發送消息到C2DM服務器

2.      Google enqueues and stores the message in case the device isinactive.

2.google的服務器把收到的消息入隊並保存下來,以防要接收消息的設備暫時無法接收。

3.      When the device is online, Google sends the message to thedevice.

3.當接收設備準備好接收時候,Google就會發送消息到這個設備上(監視一個設備時候當前是否可以接收應該是根據這個設備的Google賬戶的登錄情況,這應該也是爲什麼這個服務需要接收設備登錄google賬戶吧)

4.      On the device, the system broadcasts the message to thespecified application via Intent broadcast with proper permissions, so that onlythe targeted application gets the message. This wakes the application up. Theapplication does not need to be running beforehand to receive the message.

4.在設備端,系統把接收到的信息(消息中應該有registration ID和與ClientLogin authorization token相關的權限信息)發送給指定的應用(尋找應用應該是根據registration ID吧),並且這個系統轉發發送過程需要權限(這裏用到的應該是ClientLogin authorization token)。所以在設備接收信息的時候,對應的應用不需要保持運行,因爲它可以被系統喚醒。

5.      The application processes the message. If the application isdoing non-trivial processing, you may want to grab a wake lock and do any processing in a Service.

5.應用程序最終受到PUSH來的消息,如果應用對這個消息的處理並不複雜,那麼開發者可以設計獲取一個wakelock(當有應用獲取這個鎖的時候,系統永遠也不會休眠,除非。。。移動設備電池沒電),並且創建一個android服務來進行相關的處理。

An application canunregister C2DM if it no longer wants to receive messages.

當然,應用程序可以不接受serverPUSH來的信息,只要發送請求,在server端刪除對應自己的registration ID就行了(簡單的說就是unregister C2DM)。

Receiving a Message

接收信息

This is the sequenceof events that occurs when an Android application running on a mobile devicereceives a message:

下面是一個運行在移動設備上的android應用程序在接受信息的時候的流程:

1.      The system receives the incoming message and extracts the rawkey/value pairs from the message payload.

1.系統接受信息發來的信息,並且提取出key/value值(就是server想發的數據)。

2.      The system passes the key/value pairs to the targeted Androidapplication in a com.google.android.c2dm.intent.RECEIVE Intent as aset of extras.

2.系統將上一步得到的值發送給應用程序com.google.android.c2dm.intent.RECEIVE 

3.      The Android application extracts the raw data from the RECEIVE Intent by key and processes the data.

3.Android程序從接收到的Intent信息中提取出key/value值,並且處理這個數據

What Does the UserSee?

When mobile deviceusers install an applications that include C2DM, they will get a permissionfrom Android Market informing them that the application includes C2DM. Theymust approve the use of this feature to install the application. Depending onthe implementation of the application, it may offer users the option ofunregistering to receive messages. Uninstalling the application also has theeffect of unregistering.


技術相關更多文章猛擊:哇啦天堂論壇技術區

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章