添加maven依赖
最新版本请看激光推送官网
<!-- 极光推送 --> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.3.4</version> </dependency>
/** * 使用别名 * 用于给某特定用户推送消息。别名,可以近似地被认为,是用户帐号里的昵称。 * <p> * 使用标签 * 用于给某一群人推送消息。 * 标签类似于博客里为文章打上 tag ,即为某资源分类。 * Created by Administrator on 2018/2/9. */ public class JPushClientPC { protected static final Logger LOG = LoggerFactory.getLogger(JPushClientPC.class); /** * 发送所有的消息 全部人 * * @param responseNoticeMessage * @return */ public static PushPayload buildPushObject_all_message(ResponseNoticeMessage responseNoticeMessage) { return PushPayload.newBuilder() .setPlatform(Platform.all())//设置接受的平台 .setAudience(Audience.all())//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到 //.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage))) .setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket .build(); } /** * 根据别名集合 推送 * * @param userNames 别名List * @param responseNoticeMessage 消息 * @return */ public static PushPayload buildPushObject_all_alias_message(List<String> userNames, ResponseNoticeMessage responseNoticeMessage) { return PushPayload.newBuilder() .setPlatform(Platform.all())//设置接受的平台 .setAudience(Audience.alias(userNames))// //.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage))) .setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket .build(); } /** * 根据别名 推送 * * @param userName 别名 * @param responseNoticeMessage 消息 * @return */ public static PushPayload buildPushObject_all_alias_message(String userName, ResponseNoticeMessage responseNoticeMessage) { return PushPayload.newBuilder() .setPlatform(Platform.all())//设置接受的平台 .setAudience(Audience.alias(userName))// //.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage))) .setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket .build(); } /** * 根据标签集合推送 * * @param tags 标签List * @param responseNoticeMessage 消息 * @return */ public static PushPayload buildPushObject_all_tags_message(List<String> tags, ResponseNoticeMessage responseNoticeMessage) { return PushPayload.newBuilder() .setPlatform(Platform.all())//设置接受的平台 .setAudience(Audience.tag(tags))// //.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage))) .setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket .build(); } /** * 根据标签推送 * * @param tag 标签 * @param responseNoticeMessage 消息 * @return */ public static PushPayload buildPushObject_all_tag_message(String tag, ResponseNoticeMessage responseNoticeMessage) { return PushPayload.newBuilder() .setPlatform(Platform.all())//设置接受的平台 .setAudience(Audience.tag(tag))// //.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage))) .setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket .build(); } }
/** * 推送管理 * 推送: * 有别名一个别名 * 1,格式:xyaq-username * 有两个标签 * 1,格式:xyaq-tag-area-区域id * 2,格式:xyaq-tag-area-区域id-depa-部门id * Created by Administrator on 2018/3/1. */ public class JPushManage { private Logger logger = LoggerFactory.getLogger(this.getClass()); public static final String pushPath = "xyaq"; //在极光注册上传应用的 appKey 和 masterSecret public static final String appKey = "9a7c45d4db3d65aefdb42428";//必填,例如466f7032ac604e02fb7bda89 public static final String masterSecret = "47f0c5a8e04c7a86833484a4";//必填,每个应用都对应一个masterSecret public static final Integer maxRetryTimes = 3; /** * 推送所有平台 用于广播消息 用于管理员使用 * * @param responseNoticeMessage 消息 */ public void sendPushAllMessage(ResponseNoticeMessage responseNoticeMessage) { JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance()); //生成推送的内容,这里我们先测试全部推送 PushPayload payload = JPushClientPC.buildPushObject_all_message(responseNoticeMessage); try { PushResult result = jpushClient.sendPush(payload); //Thread.sleep(5000); // 请求结束后,调用 NettyHttpClient 中的 close 方法,否则进程不会退出。 jpushClient.close(); logger.info("sendPushAllAlert :Got result - " + result); } catch (APIConnectionException e) { logger.error("sendPushAllAlert :Connection error. Should retry later. ", e); } catch (APIRequestException e) { logger.error("sendPushAllAlert : Error response from JPush server. Should review and fix it. -=- {}", e.toString()); logger.info("sendPushAllAlert : HTTP Status -=- {}", e.getStatus()); logger.info("sendPushAllAlert : Error Code -=- {}", e.getErrorCode()); logger.info("sendPushAllAlert : Error Message -=- {}", e.getErrorMessage()); logger.info("sendPushAllAlert : Msg ID -=- {}", e.getMsgId()); } } /** * 根据别名推送所有平台 * 一次推送最多 1000 个。 * * @param userNames 别名 * @param responseNoticeMessage 消息 */ public void sendPushAliasAllMessage(List<String> userNames, ResponseNoticeMessage responseNoticeMessage) { JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance()); //生成推送的内容,这里我们先测试全部推送 PushPayload payload = JPushClientPC.buildPushObject_all_alias_message(userNames, responseNoticeMessage); try { PushResult result = jpushClient.sendPush(payload); Thread.sleep(5000); // 请求结束后,调用 NettyHttpClient 中的 close 方法,否则进程不会退出。 jpushClient.close(); logger.info("sendPushAliasAllAlert :Got result - " + result); } catch (APIConnectionException e) { logger.error("sendPushAliasAllAlert :Connection error. Should retry later. ", e); } catch (APIRequestException e) { logger.error("sendPushAliasAllAlert : Error response from JPush server. Should review and fix it. -=- {}", e.toString()); logger.info("sendPushAliasAllAlert : HTTP Status -=- {}", e.getStatus()); logger.info("sendPushAliasAllAlert : Error Code -=- {}", e.getErrorCode()); logger.info("sendPushAliasAllAlert : Error Message -=- {}", e.getErrorMessage()); logger.info("sendPushAliasAllAlert : Msg ID -=- {}", e.getMsgId()); } catch (InterruptedException e) { logger.error("sendPushAliasAllAlert -=- {}", e.toString()); } } /** * 根据别名推送所有平台 * * @param userName 别名 * @param responseNoticeMessage 消息 */ public void sendPushAliasAllMessage(String userName, ResponseNoticeMessage responseNoticeMessage) { JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance()); //生成推送的内容,这里我们先测试全部推送 PushPayload payload = JPushClientPC.buildPushObject_all_alias_message(userName, responseNoticeMessage); try { PushResult result = jpushClient.sendPush(payload); Thread.sleep(5000); // 请求结束后,调用 NettyHttpClient 中的 close 方法,否则进程不会退出。 jpushClient.close(); logger.info("sendPush :Got result - " + result); } catch (APIConnectionException e) { logger.error("sendPush :Connection error. Should retry later. ", e); } catch (APIRequestException e) { logger.error("sendPush : Error response from JPush server. Should review and fix it. -=- {}", e.toString()); logger.info("sendPush : HTTP Status -=- {}", e.getStatus()); logger.info("sendPush : Error Code -=- {}", e.getErrorCode()); logger.info("sendPush : Error Message -=- {}", e.getErrorMessage()); logger.info("sendPush : Msg ID -=- {}", e.getMsgId()); } catch (InterruptedException e) { logger.error("sendPush -=- {}", e.toString()); } } /** * 根据标签推送所有平台 * 一次推送最多 20 个。 * * @param tags 标签名 * @param responseNoticeMessage 消息 */ public void sendPushTagsAllMessage(List<String> tags, ResponseNoticeMessage responseNoticeMessage) { JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance()); //生成推送的内容,这里我们先测试全部推送 PushPayload payload = JPushClientPC.buildPushObject_all_tags_message(tags, responseNoticeMessage); try { PushResult result = jpushClient.sendPush(payload); Thread.sleep(5000); // 请求结束后,调用 NettyHttpClient 中的 close 方法,否则进程不会退出。 jpushClient.close(); logger.info("sendPushTagsAllAlert :Got result - " + result); } catch (APIConnectionException e) { logger.error("sendPushTagsAllAlert :Connection error. Should retry later. ", e); } catch (APIRequestException e) { logger.error("sendPushTagsAllAlert : Error response from JPush server. Should review and fix it. -=- {}", e.toString()); logger.info("sendPushTagsAllAlert : HTTP Status -=- {}", e.getStatus()); logger.info("sendPushTagsAllAlert : Error Code -=- {}", e.getErrorCode()); logger.info("sendPushTagsAllAlert : Error Message -=- {}", e.getErrorMessage()); logger.info("sendPushTagsAllAlert : Msg ID -=- {}", e.getMsgId()); } catch (InterruptedException e) { logger.error("sendPushTagsAllAlert -=- {}", e.toString()); } } /** * 根据标签推送所有平台 * * @param tag 标签名 * @param responseNoticeMessage 消息 */ public void sendPushTagsAllMessage(String tag, ResponseNoticeMessage responseNoticeMessage) { JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance()); //生成推送的内容,这里我们先测试全部推送 PushPayload payload = JPushClientPC.buildPushObject_all_tag_message(tag, responseNoticeMessage); try { PushResult result = jpushClient.sendPush(payload); Thread.sleep(5000); // 请求结束后,调用 NettyHttpClient 中的 close 方法,否则进程不会退出。 jpushClient.close(); logger.info("sendPushTagAlert :Got result - " + result); } catch (APIConnectionException e) { logger.error("sendPushTagAlert :Connection error. Should retry later. ", e); } catch (APIRequestException e) { logger.error("sendPushTagAlert : Error response from JPush server. Should review and fix it. -=- {}", e.toString()); logger.info("sendPushTagAlert : HTTP Status -=- {}", e.getStatus()); logger.info("sendPushTagAlert : Error Code -=- {}", e.getErrorCode()); logger.info("sendPushTagAlert : Error Message -=- {}", e.getErrorMessage()); logger.info("sendPushTagAlert : Msg ID -=- {}", e.getMsgId()); } catch (InterruptedException e) { logger.error("sendPushTagAlert -=- {}", e.toString()); } } public static void main(String[] args) { JPushManage jPushManage = new JPushManage(); //所有平台所有人 jPushManage.sendPushAllMessage(new ResponseNoticeMessage("test", "测试1")); //根据别名推送 一次推送最多 1000 个。 List<String> userNames = new ArrayList<>(); userNames.add(String.format(pushPath + "_%s", "108zx")); jPushManage.sendPushAliasAllMessage(userNames, new ResponseNoticeMessage("test", "108zx测试2")); String userName = String.format(pushPath + "_%s", "108zx"); jPushManage.sendPushAliasAllMessage(userName, new ResponseNoticeMessage("test", "108zx测试3")); //根据标签推送 一次推送最多 20 个。 List<String> tags = new ArrayList<>(); tags.add(String.format(pushPath + "_tag_area_%s", "5")); tags.add(String.format(pushPath + "_tag_area_%s_depa_%s", "5", "5")); jPushManage.sendPushTagsAllMessage(tags, new ResponseNoticeMessage("test", "测试4")); String tag = String.format(pushPath + "_tag_area_%s", "5"); jPushManage.sendPushTagsAllMessage(tag, new ResponseNoticeMessage("test", "测试5")); } }