pom
<!-- https://mvnrepository.com/artifact/com.google.gcm/gcm-server -->
<dependency>
<groupId>com.google.gcm</groupId>
<artifactId>gcm-server</artifactId>
<version>1.0.0</version>
</dependency>
Demo
Sender sender = new Sender("your_google_key");//需要去谷歌注册,然后获取你的key
Message message = new Message.Builder().addData("标题", "消息体").build();
MulticastResult resultMulti = null;
try {
Integer retries = 5;//如果失败重新发送的次数
List<String> appKeys = new ArrayList<>();
appKeys.add("手机的唯一标识");
resultMulti = sender.send(message, appKeys, retries);
Integer successCount = resultMulti.getSuccess();
Integer failCount = resultMulti.getFailure();
// TODO:统计成功和失败次数
return Boolean.TRUE;
} catch (IOException e) {
LOGGER.error("androidMessage send fail" + title + body);
return Boolean.FALSE;
}
网友评论