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, my-app@gmail.com.

这个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.


技术相关更多文章猛击:哇啦天堂论坛技术区

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值