- 浏览: 156208 次
- 性别:
- 来自: 五指山
-
文章分类
最新评论
-
dengdonglin888:
qq_30221445 写道你好 请问这种数据能解吗 < ...
Simple XML -
qq_30221445:
你好 请问这种数据能解吗 <request> ...
Simple XML -
画个逗号给明天qu:
画个逗号给明天qu 写道
Android上传文件到服务器 -
画个逗号给明天qu:
...
Android上传文件到服务器 -
alranger:
我在jsp页面加上这一段代码后,问题还是存在。
解决Ext在ie9报错:不支持extjs对象的“createContextualFragment属性或方法”
Writing Android Application that use C2DM
想要写一个使用C2DM的程序,你必须有一个程序服务器端能够执行 Role of the Third-Party Application Server所描述的任务。这一节描述了你创建一个使用C2DM客户端的步骤。
请记住C2DM是没有用户界面的。怎么在程序里处理消息取决于你。
写个程序客户端有两个主要步骤:
1. 创建一个manifest文件。这个文件包含程序使用C2DM需要使用的权限。
2. 写java代码。要使用C2DM,程序要包括:
A. 开始和停止注册服务的代码。
B. Receivers for com.google.android.c2dm.intent.C2D_MESSAGE 和com.google.android.c2dm.intent.REGISTRATION。
Creating the Manifest
每一个程序在根目录下都有一个AndroidManifest.xml文件。这个文件提供程序的必要信息给Android系统,这些信息是系统在运行任何程序代码之前必须要有的。要使用C2DM,这个文件必须包含:
1. com.google.android.c2dm.permission.RECEIVE。程序拥有注册和接受消息的权限。
2. android.permission.INTERNET。程序拥有联网的权限。
3. applicationPackage+”.permission.C2D_MESSAGE”防止其他程序注册和接受这个程序的消息。
4. Receivers for com.google.android.c2dm.intent.RECEIVE和com.google.android.c2dm.intent.REGISTRATION.category设置成applicationPackage。receiver需要com.google.android.c2dm.SEND权限,这样C2DM就可以发送消息给它。Registration和消息接收都是通过Intents来实现的。
5. 如果C2DM对于你的程序是一个至关重要的功能,就一定要在AndroidManifest.xml里设置andorid:minSdkVersion=”8”。这样就确保了程序不会装在一些程序不能正常运行的手机上。
Received C2D_MESSAGE Intents包含第三方服务器发送过来的键值对。一个特别的key是collapse_key。这是发送者设置的允许离线的设备在上线时处理消息。
下面是一个支持C2DM的manifest的例子:
<manifest package="com.example.myapp" ...>
<!—只有这个程序能注册和接受消息 -->
<permission android:name="yourpackagename.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="yourpackagename.permission.C2D_MESSAGE" />
<!—这个程序有注册和接收消息的权限 -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!—发送registration id到服务器需要的权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<!—只有C2DM服务器可以发送消息给程序. 如果下面的权限不设置,其他的程序也能发送 -->
<receiver android:name=".C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND">
<!—接收消息 -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="yourpackagename" />
</intent-filter>
<!—接收registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="yourpackagename " />
</intent-filter>
</receiver>
Registering for C2DM
Android程序在接收任何消息前需要向C2DM服务器注册。如果要注册,需要发送一个intent(com.google.android.c2dm.intent.REGISTER),包含2个参数:
1. sender:是一个授权发送消息到程序的ID,通常是程序开发者设置的一个gmail地址。
2. app:application’s ID.通过PendingIntent设置来允许registration service提取程序信息。
比如:
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate registrationIntent.putExtra("sender", emailOfSender); startService(registrationIntent);
直到程序把registration ID发送到第三方程序服务器,注册才结束。第三方程序服务器使用这个registration ID发送消息给目标机器上的目标程序。
Unregistering from C2DM
Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER"); unregIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); startService(unregIntent);
Handling Registration Results
在AndroidManifest.xml里,定义了一个receiver:com.google.android.c2dm.intent.REGISTRATION。同样定义了一个receiver:com.google.android.c2dm.intent.RECEIVE。registration和接受消息都是通过Intents实现的。
REGISTRATION的主要作用是允许程序接收registration ID。这个Intent可以在任何时候发送。Google可能定期刷新receiver ID。一个程序收到Intent包含registration_id
这个参数,必须确保第三方程序服务器收到registration ID。可以通过保存registration ID并发送到服务器来实现。如果网络断了或者有错误,程序应该尝试重新发送registration ID当网络连接上的时候。程序应该追踪registration的状态并且尝试重新注册当处理没完成的时候。
当注册没有完成的时候,REGISTRATION通常产生错误。如果发生了错误,程序应该稍后重试。当程序解除注册的时候,会发送包含unregisteered作为参数的REGISTRATION Intent。
下面是REGISTRATION Intent可能的error codes:
Error Code |
Description |
SERVICE_NOT_AVAILABLE |
手机不能读取响应或者有500/503错误.程序应该使用指数退避然后重试。 |
ACCOUNT_MISSING |
手机上没有登录google账户. 程序应该要求用户打开账户控制并增加一个账户。 |
AUTHENTICATION_FAILED |
错误的密码,程序应该让用户输入正确的密码,并在稍后手动重试。 |
TOO_MANY_REGISTRATIONS |
用户有太多的注册程序. 程序应该告诉用户卸载一部分注册程序。然后手动重试。 |
INVALID_SENDER |
Sender account不能被识别。 |
PHONE_REGISTRATION_ERROR |
这个手机现在不支持C2DM。 |
程序收到一个REGISTRATION Intent 广播当第三方程序服务器尝试发送消息给它的时候。但是registration IDs不存在的原因有以下几种:
1. 程序第一次运行,还没有registration ID。
2. 程序解除注册。
3. C2DM服务器端定时刷新了registration IDs。
程序必须准备好去应对这几种情况,比如:
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) { // 注册失败。稍后重试 } else if (intent.getStringExtra("unregistered") != null) { // 解除注册成功。授权的服务器发送的新消息将会被拒绝。
} else if (registration != null) { // 发送 registration ID 到发送消息的第三方程序服务器。 //应该开启一个新的线程去发送 registration ID。到此,注册就完了。
} }
Handling Received Data
当C2DM服务器收到从第三方程序服务器发送过来的消息时,C2DM服务器从消息中提取出键值对然后以com.google.android.c2dm.intent.RECEIVE Intent作为载体,把键值对发送到程序。程序根据key从消息中提取数据并处理数据。
比如:
protected void onReceive(Context context, Intent intent) { String accountName = intent.getExtras().getString(Config.C2DM_ACCOUNT_EXTRA); String message = intent.getExtras().getString(Config.C2DM_MESSAGE_EXTRA); if (Config.C2DM_MESSAGE_SYNC.equals(message)) { if (accountName != null) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Messaging request received for account " + accountName); } ContentResolver.requestSync( new Account(accountName, SyncAdapter.GOOGLE_ACCOUNT_TYPE), JumpNoteContract.AUTHORITY, new Bundle()); } } }
发表评论
-
xUtils简介
2014-11-25 10:04 881xUtils 包含了很多实用的android工具。 xU ... -
直接拿来用!最火的Android开源项目
2014-07-25 11:01 730转 http://www.admin10000.com/d ... -
Android APK反编译详解(附图)
2014-03-28 10:56 857http://blog.csdn.net/ithomer/ar ... -
小米人
2014-02-17 17:23 716http://www.xiaomiren.net/ -
android开发之gallery 兑现滚动一张且短距离滑动实现滚动
2013-07-02 15:28 701http://www.myexception.cn/andro ... -
TextView显示插入的图片
2013-07-01 11:29 754http://orgcent.com/android-text ... -
TextView使用SpannableString设置复合文本
2013-07-01 11:29 691http://orgcent.com/android-text ... -
转:::Android TextView文字横向自动滚动(跑马灯)
2013-06-17 11:45 1541TextView实现文字滚动需要以下几个要点: 1.文字长度长 ... -
相片滤镜开源
2013-04-27 15:01 771https://github.com/daizhenjun/I ... -
android图片特效处理之模糊效果
2013-04-27 14:57 866http://blog.csdn.net/sjf0115/ar ... -
android图片处理方法(不断收集中)
2013-04-27 14:57 597http://gundumw100.iteye.com/blo ... -
Android, WindowsPhone7, IOS ,vc2010平台40多套图片滤镜开源
2013-04-27 14:56 704http://www.cnblogs.com/daizhj/a ... -
移动云存储平台
2013-04-25 16:13 932http://bmob.cn 关于Bmob 对于很多 ... -
android ExpandableListView简单应用及listview模拟ExpandableListView
2013-02-28 11:45 725http://blog.csdn.net/jj120522/a ... -
android_App集成支付宝
2013-02-28 11:43 824http://www.cnblogs.com/qianxude ... -
Android Pull Refresh View 插件
2012-12-01 12:43 888Android Pull Refresh View htt ... -
Android-TelephoneManager(转载)
2012-10-09 22:08 1411好文章齐分享。原文地址:http://blog.si ... -
android 开源 listview separato
2012-08-27 22:51 692http://code.google.com/p/androi ... -
fragment开源项目 学习
2012-08-13 12:02 965https://github.com/tisa007/Andr ... -
Fragment学习
2012-08-13 11:53 708http://www.eoeandroid.com/threa ...
相关推荐
Introducing Cloud to Device Messaging Implementing Copy Protection Using the License Verification Library Introducing In-App Billing Using Wake Locks Using AIDL to Support Inter-Process Communication ...
C2DM(Cloud to Device Messaging)是Google提供的一种服务,用于帮助开发者从服务器向Android设备上的应用程序发送数据。该服务不仅仅是一个简单的推送通知服务,而是与Android系统紧密集成的。 C2DM框架的一个...
### 开始使用 Xamarin.Android 开发 Android ... - **Google Cloud Messaging**:使用 GCM 发送消息。 - **GCM 通知演练**:实践推送通知。 6. **Web 服务**: - **演练:使用 WCF**:使用 WCF 作为 Web 服务。 #...
Paperback: 338 pages Publisher: Packt Publishing - ebooks Account (August 3, 2015) ...Learn how to generate a device specific installer for an Ionic app using Ionic CLI as well as Ionic Cloud services
极光推送基于GCM/FCM(Google Cloud Messaging/ Firebase Cloud Messaging)和APNs(Apple Push Notification service)进行消息传递。它为开发者提供了一个API接口,通过这个接口,开发者可以向指定的设备或用户...
例如,使用Java编写的服务器可以与Google的Firebase Cloud Messaging (FCM) 或Apple的Push Notification Service (APNs) 进行交互,发送消息到目标设备。 4. **使用Firebase Cloud Messaging (FCM)** FCM是Google...