C2DM简介

来源:互联网 发布:伊沃人工智能 编辑:程序博客网 时间:2024/05/22 00:38

http://blog.sina.com.cn/s/blog_465d6cba0100y0pv.html



  1. C2DM是什么?

Android Cloud to Device Messaging (C2DM) google提供的一项服务,用于帮助开发者向Android应用程序推送信息。

简单的说,C2DMGoogleAndroid提供的IP Push服务。第三方应用服务器可以利用这项服务向自己的Android客户端推送信息。

 

  1. 开始之前

    1. 使用前提:

      1. Android2.2或更高版本
      2. 需要用户在设备上设置google账号。因为所有通过C2DM Server推送的消息都共用一个Google服务的连接。
      3. 终端设备需要安装Market Application(电子市场)。(或许用于接收推送信息的长连接是由Market Application维护的?)

 

  1. 适宜场景(能干什么?不能干什么?)

 

C2DM主要设计用于发送轻量级通知消息,应用程序应在接到通知消息后到自己的服务器获取用户数据。不要用C2DM直接推送用户数据信息。

C2DM不保证数据送达,也不保证消息的顺序。

 

  1. C2DM组件及认证信息

    1. 组件

组件

Android终端

必须Android2.2或以上版本,必须安装电子市场,至少一个Google登录账户。

第三方应用服务器

开发者搭建,用于向Android应用发送数据。

C2DM Servers

Google维护的服务器,从第三方应用服务器接收数据并推送到android设备上的第三方应用。

 

  1. 组件间交互流程

 

1,客户端向C2DM发起注册

2,C2DM响应客户端Registration ID

3,客户端将Registration ID发送给自己对应的消息服务器

4, 服务器通过https://www.google.com/accounts/ClientLogin 获取Auth Token并保存。以后就可以通过这个Auth Token调用C2DM服务发送消息了。

 

  1. Credentials

Credentials

Sender ID

标识开发者身份的Email账户。

Application ID

通常是应用程序包名,用来唯一标识接收消息的应用。

Registration ID

C2DM服务器分配给Android应用程序的ID。这个ID唯一标识特定设备上的特定应用。C2DM服务器利用这个ID来确定消息接收者。

Google User Account

Google账号。在一个接收C2DM消息的设备上至少需要登录一个google账号。

Sender Auth Token

第三方应用服务器授权,该授权允许第三方应用服务器使用C2DM发送通知消息。

 

  1. 使用C2DM

    1. 开始之前

到http://code.google.com/intl/zh-CN/android/c2dm/signup.html申请C2DM服务。注册Sender ID。

 

  1. 客户端工作

    1. 实现注册功能。

接收C2DM的客户端首先需要实现注册功能,可以应用启动后触发,或者通过用户事件触发注册,由客户端逻辑决定。大致如下:

 

Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplateregistrationIntent.putExtra("sender", emailOfSender);startService(registrationIntent);

 

  1. 实现Broadcast Receiver接收C2DM广播消息。

 

com.google.android.c2dm.intent.REGISTRATION.

com.google.android.c2dm.intent.RECEIVE

 

public void onReceive(Context context, Intent intent) {if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {handleRegistration(context, intent);} else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {handleMessage(context, intent);}}

 

private void handleRegistration(Context context, Intent intent) {String registration = intent.getStringExtra("registration_id"); if (intent.getStringExtra("error") != null) {// Registration failed, should try again later.} else if (intent.getStringExtra("unregistered") != null) {// unregistration done, new messages from the authorized sender will be rejected} else if (registration != null) {// Send the registration ID to the 3rd party site that is sending the messages.// This should be done in a separate thread.// When done, remember that all registration is done. }}

 

protected void handleMessage (Context context, Intent intent) {String message = intent.getExtras().getString(Config.C2DM_MESSAGE_EXTRA);

 

//Handle message}

 

  1. Server工作

    1. 获取Sender Auth Token

 

curl https://www.google.com/accounts/ClientLogin -d Email=your_user -d "Passwd=your_password" -d accountType=GOOGLE -d source=Google-cURL-Example -d service=ac2dm

从响应消息中提取Auth Token。( "Auth="后面的字符串)

 

  1. 接收并存储客户端上传的Registration ID
  2. 通过C2DM服务器发送消息

 

curl --header "Authorization: GoogleLogin auth=your_authenticationid""https://android.apis.google.com/c2dm/send" -d registration_id=your_registration -d"data.payload=This data will be send to your application as payload" -d collapse_key=0

 

 

  1. 更好的资源

http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html

http://blog.csdn.net/ichliebephone/article/details/6591071

http://code.google.com/intl/zh-CN/android/c2dm/index.html


0 0