MIME-version: 1.0 Content-Type: multipart/related; boundary="----=_NextPart_000_0076_01C29953.BE473C30"; type="text/html" X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 This is a multi-part message in MIME format. ------=_NextPart_000_0076_01C29953.BE473C30 Content-Type: text/html; Content-Transfer-Encoding: quoted-printable Content-Location: file:///~tmp1363935081.TMP.html GCM学习
GCM学习
google教程
http://developer.android.com/google/gcm/gs.html
1.创建项目
2.在Services中开启 Google Cloud Messaging for Android
3.Create an OAuth 2.0 client ID
4.API Access 创建 Server Key 和 Android Key
在androidManifest.xml中加入
<permission
android:name=3D"com.myb.app.permission.C2D_MESSAGE"
android:protectionLevel=3D"signature" />
<uses-permission android:name=3D"com.myb.app.permission.C2D_MESSAGE" />
//其中com.myb.app改为相应的包名
<!-- App receives GCM messages. -->
<uses-permission android:name=3D"com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name=3D"android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name=3D"android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name=3D"android.permission.WAKE_LOCK" />
<receiver
android:name=3D"com.google.android.gcm.GCMBroadcastReceiver"
android:permission=3D"com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name=3D"com.google.android.c2dm.intent.RECEIVE" />
<action android:name=3D"com.google.android.c2dm.intent.REGISTRATION" />
<category android:name=3D"com.myb.app" />
</intent-filter>
</receiver>
<service android:name=3D".GCMIntentService" />
在android SdK extras\google\gcm\gcm-client\dist 找到 gcm.jar,加入项目中
向google服务器注册服务
/***
* 向google注册服务
*/
private void GcmRegistrarDevice() {
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId =3D GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} else {
Log.v(TAG, "Already registered:" + regId);
}
}
SENDER_ID 为创建的google项目的 project ID,
创建 GCMIntentService 类,从 GCMBaseIntentService 继承 接受注册的GCM发回的信息
package com.myb.app;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService{
private final static String TAG =3D GCMIntentService.class.getSimpleName();
private NotificationManager mNM;
@Override
public void onCreate() {
super.onCreate();
mNM =3D (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
@Override
protected void onError(Context arg0, String arg1) {
Log.d(TAG, "onError:" + arg1);
}
@Override
protected void onMessage(Context arg0, Intent intent) {
Log.e("GCM MESSAGE", "Message Recieved!!!");
String message = intent.getStringExtra("message");
if (message == null) {
Log.e("NULL MESSAGE", "Message Not Recieved!!!");
} else {
Log.i(TAG, "new message=3D " + message);
showNotifiy(message);
}
}
@Override
protected void onRegistered(Context arg0, String arg1) {
Log.d(TAG, "onRegistered:" + arg1);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
Log.d(TAG, "onUnregistered:" + arg1);
}
private void showNotifiy(String msg){
Intent intent =3D new Intent(this, HomeActivity.class);
PendingIntent contentIntent =3D PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
String tickerText =3D getString(R.string.app_name, msg);
// construct the Notification object.
Notification notif = new Notification(R.drawable.ic_logo, tickerText, System.currentTimeMillis());
notif.setLatestEventInfo(this, "母婴宝", msg, contentIntent);
notif.defaults = Notification.DEFAULT_ALL;
mNM.notify(R.string.app_name, notif);
}
}
后台,我们的后台使用的是maven+spring mvc+jpa, 如果不是maven管理项目则把Android SDK 下面的xtras\google\gcm\gcm-server\dist 中的 gcm-server.jar加入项目。如果是maven,github上有人做了处理
https://github.com/slorber/gcm-server-repository
在pom.xml中加入
<repository>
<id>gcm-server-repository</id>
<url>https://raw.github.com/slorber/gcm-server-repository/master/releases/</url>
</repository>
<dependency>
<groupId>com.google.android.gcm</groupId>
<artifactId>gcm-server</artifactId>
<version>1.0.2</version>
</dependency>
向注册的设备发送消息
Sender sender = new Sender(myApiKey);
Message message = new Message.Builder().addData("message", msg).build();
MulticastResult mr = sender.send(message, devices, 5);
for(Result result : mr.getResults()){
if (result.getMessageId() != null) {
String regId = result.getCanonicalRegistrationId();
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// application has been removed from device - unregister
// database
phoneDao.delete(regId);
}
}
}
当前项目的API key
devices 用户设备的注册ID,在客户端可以得到,这个值应该保存到后台