(一) 在上一章,我们提到Android 消息推送的基本流程和需要的几个键值。
这里介绍一下怎样使用Google Cloud Messaging (GCM)。
对于GCM的实现,google 早期使用的接口可以参考这篇文章。文章中已经充分介绍了在老接口下怎样实现GCM。
http://blog.csdn.net/huangjialiang1986/article/details/8056221
下面是对这篇文章代码的部分更改:
Android 客户端的代码为:
https://github.com/yuxiaohui78/Google_Cloud_Messaging/tree/master/GCMClientOnMobile_deprecated_version
模拟第三方服务器的消息发送部分代码:
https://github.com/yuxiaohui78/Google_Cloud_Messaging/tree/master/GCMSendOnPC
(二)由于google已经决定废弃老接口。所以下面我们来介绍一下新接口怎样实现。
新的Api接口需要google service 3.1+ 的支持。
1.在AndroidManifest.xml 中增加支持,允许接收云消息com.google.android.c2dm.permission.RECEIVE
<manifest package="com.example.gcm" ...>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.example.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
<application ...>
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<