您可以将此代码用于服务器部分:
package yourpackage.android.gcm.server;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Sender;
import java.util.ArrayList;
class Notify {
public static void main(String args[]) {
try {
// this API key
Sender sender = new Sender("AIzaSyCn3N2OIm-EDtiGwTyQfSIB8NRvDtIOx30");
ArrayList<String> devicesList = new ArrayList<String>();
//add you deviceID
devicesList.add("APA91bELVJbxB_NLnLbTkkkX87SDdkJc6OfCN2slhC9t4cLq-KA32eGgiW4-Gi--ZEsEMKIh0AtYJMs5rQGswfm3cH1qK853WcpV98bkaplAaC5AiycDmifuVFSRl21vgf-Rqj0dCrFF");
//devicesList.add("APA91bHIdM4XGqrjJLTuwCX5OOrTYG4ACXYEVkZDM1bPs5qFdzJP4Bpql-sZqyKB8BU7fDtdxB84aTygHLyASYg_XNY6lqrcA4wj4sZHJXGVFzz_0UEADMfFCx9NAfRZxunIYso_dkBa");
//APA91bFA-i2l3iEMnIBs0JK80pTLHOsE7p1s-DysRpKGas1MQOVILyIs9xwY7soysSWGz5Uif68uXR6F5Xn0tCTYesv78uQZxhC310a1cvf8aFohhfMGY6awbOSg3t1GRz2i3U-8kVSF
// Use this line to send message without payload data
// Message message = new Message.Builder().build();
// use this line to send message with payload data
Message message = new Message.Builder()
//.collapseKey("message")
//.timeToLive(241000)
.delayWhileIdle(true)
.addData("message", "Your message send")
.build();
/**/
// Use this code to send to a single device
// Result result = sender
// .send(message,
// "APA91bGiRaramjyohc2lKjAgFGpzBwtEmI8tJC30O89C2b3IjP1CuMeU1h9LMjKhmWuZwcXZjy1eqC4cE0tWBNt61Kx_SuMF6awzIt8WNq_4AfwflaVPHQ0wYHG_UX3snjp_U-5kJkmysdRlN6T8xChB1n3DtIq98w",
// 1);
// Use this for multicast messages
MulticastResult result = sender.send(message, devicesList, 1);
//sender.send(message, devicesList, 0);
System.out.println(result.toString());
if (result.getResults() != null) {
int canonicalRegId = result.getCanonicalIds();
if (canonicalRegId != 0) {
}
} else {
int error = result.getFailure();
System.out.println(error);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
您可以将此代码用于客户端部分:
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
super("");
Log.d("GCMIntentService", senderId);
}
private void genNotification(Context context, String message) {
notify(context, message);
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
Log.e("screen on.................................", ""+isScreenOn);
if(isScreenOn==false) {
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
wl.acquire(10000);
PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");
wl_cpu.acquire(10000);
}
}
public static void notify(Context context, String message) {
int icon = R.drawable.icon;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, NotificationListView.class);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationDataHelper helper = new NotificationDataHelper(context);
Notification notification;
notification = new Notification(icon, "message", when);
notificationIntent.putExtra("message", message);
notification.setLatestEventInfo(context, title, "message", intent);
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.setLatestEventInfo(context, title, msg, intent);
}
//set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setAction("notification");
notificationManager.notify(0, notification);
}
@Override
protected void onError(Context arg0, String arg1) {
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.d("GCM", "RECEIVED A MESSAGE");
Log.d("GCM", "RECEIVED A MESSAGE");
Log.d("GCM", "RECEIVED A MESSAGE");
Log.d("GCM", "RECEIVED A MESSAGE");
genNotification(context, intent.getStringExtra("message"));
}
@Override
protected void onRegistered(Context arg0, String arg1) {
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
}
}
首先你像本教程一样注册了设备http://developer.android.com/guide/google/gcm/index.html并获取设备ID,你可以为设备发送消息
private void registerGCM() {
GCMRegistrar.checkDevice(this);
//GCMRegistrar.unregister(this);
//Log.d("info", "unregistered....." + GCMRegistrar.getRegistrationId(this));
GCMRegistrar.checkManifest(this);
if (GCMRegistrar.isRegistered(this)) {
Log.d("info", GCMRegistrar.getRegistrationId(this));
}
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, "1013733918417");
Log.d("info", GCMRegistrar.getRegistrationId(this));
} else {
Log.d("info", "already registered as " + regId);
}
GCMIntentService.notify(this, null);
}