package com.ives.server;
import java.io.IOException;
import com.google.android.gcm.server.Constants;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
public class Main {
private static final String apiKey = "AIzaSyBVDLKZBI_3jOKjXwMVUQysCRkYC173bRE";
private static String deviceRegId = "APA91bE7A4ZlwR1rVtAUeJsaeUa7hxc5z-aLxwYdOhPRwSNfc7QUsqJVZ5R4ujxlxMvMACHHbqqm5RH-AO6U7WkVAciDl5YhSuDqp9IrXjdvKZXDYtG7drIG-HfJAxwyzPW16sfjy-_YLZAabJz8qv9mzjjers93jQ";
/**
* @param args
*/
public static void main(String[] args) {
Sender sender = new Sender(apiKey);
Message message = new Message.Builder().addData("mine", "test2").build();
Result result = null;
try {
result = sender.send(message, deviceRegId, 5);
System.out.println(result.getMessageId());
System.out.println(result.getCanonicalRegistrationId());
System.out.println(result.getErrorCodeName());
} catch (IOException e) {
e.printStackTrace();
}
//为空,则消息未发送给任何设备
if (result.getMessageId() != null) {
String canonicalRegId = result.getCanonicalRegistrationId();
//用户注册了新的注册id,或者谷歌服务器刷新了注册id。
//用户注册了新id,旧的id会被保存一段时间。此时使用旧id发送消息,设备即使已使用新id,依然可以收到
if (canonicalRegId != null) {
// same device has more than on registration ID: update database
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// application has been removed from device - unregister database
}
}
}
}
- 1
- 2
- 3
- 4
- 5
前往页