maven依赖
<dependency> <groupId>cn.jpush.apigroupId> <artifactId>jpush-clientartifactId> <version>3.3.4version> dependency>
操作类 使用的时候可以自行更改 用的是message 不是alert
/** * 推送管理 * 推送: * 有别名一个别名 * 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 = "";//必填,例如466f7032ac604e02fb7bda89 public static final String masterSecret = "";//必填,每个应用都对应一个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(ListuserNames, 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 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 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 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")); } }
推送消息
//手机端推送,不用于消息提示,请求接口,更新数据 new JPushManage().sendPushAliasAllMessage(String.format(pushPath + "_%s", newUser.getUsername()),new ResponseNoticeMessage("update_user","修改用户信息,更新用户信息"));