/**
* 指定用户(根据设备别名)极光推送
* 极光官方文档 极光推送 - Push API v3 - 极光文档
* @param message(消息内容体-推送alert的消息)
* @param to (通知内容体)
?* @param type(定义类型,供用户收到推送后点击跳转到指定页面)
?* @param param(跳转到指定页面需要的参数,可定义为json字符串格式)
* @param token (推送接收方设备别名)
* @return
*/
public Result push(String message, String to, Integer type, String param, JSONObject jsonObject, String... token) {
String appKey = ""; // 极光官网注册创建应用后的appKey
String masterSecret = ""; // 极光官网注册创建应用后的masterSecret
boolean apnsprod = false; // 测试环境false正式环境true
try {
PushPayload payload = pushAllNotify(message, to, type, param, apnsprod, token);
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
PushResult result = jpushClient.sendPush(payload);
return Result.OK("OK",JSON.toJSONString(result));
} catch (Exception e) {
log.error(e.getMessage(), e);
return Result.OK("error",e.getMessage());
}
}
/**
* 全平台指定用户推送
*
* @param message
* @param to
* @param token
* @return
*/
public PushPayload pushAllNotify(String message, String to, Integer type, String param, boolean apnsprod, String... token) {
PushPayload.Builder payload = PushPayload.newBuilder()
.setPlatform(Platform.all());
payload.setAudience(StringUtils.isEmpty(token) ? Audience.all() : Audience.alias(token));
payload.setNotification(Notification.newBuilder().setAlert(message)
.addPlatformNotification(IosNotification.newBuilder()
.addExtra("to", to)
.addExtra("type",type)
.addExtra("param",param)
.build())
.addPlatformNotification(AndroidNotification.newBuilder()
.addExtra("to", to)
.addExtra("type",type)
.addExtra("param",param)
.build())
.build()
);
if (apnsprod) payload.setOptions(Options.newBuilder().setApnsProduction(true).build());
return payload.build();
}
|