1 private static PushPayload buildPushObject_all_alias_alert(Integer mid, String message, Integer flag) { 2 JSONObject result = new JSONObject(); 3 result.put("mid", mid); 4 result.put("message", message); 5 result.put("flag", flag); 6 return PushPayload.newBuilder() 7 .setPlatform(Platform.all()) 8 .setAudience(Audience.all()) 9 .setNotification(Notification.alert("")) 10 .setMessage(Message.content(result.toString())) 11 .build(); 12 }
//给机器发送信息 public static void sendJPush(Integer mid, String message, Integer flag) { JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance()); // For push, all you need do is to build PushPayload object. PushPayload payload = buildPushObject_all_alias_alert(mid, message, flag); try { PushResult result = jpushClient.sendPush(payload); log.info("Got result - " + result); } catch (APIConnectionException e) { // Connection error, should retry later log.error("Connection error, should retry later", e); } catch (APIRequestException e) { // Should review the error, and fix the request log.error("Should review the error, and fix the request", e); log.info("HTTP Status: " + e.getStatus()); log.info("Error Code: " + e.getErrorCode()); log.info("Error Message: " + e.getErrorMessage()); } }
/** *测试消息机制 */ @RequestMapping(value = "/testJpush.json", method = RequestMethod.GET) @ResponseBody public void testJpush(Integer mid) { JSONObject json = new JSONObject(); json.put("Untiekes", "0A-01,0B-01"); json.put("Ono", "2019080114156688"); //发送信息给android try { Utils.sendJPush(mid, """ + json + """, 4); } catch (Exception e) { log.info("推送消息异常=========================" + e.getMessage()); } }
详情见极光开发官网-------https://docs.jiguang.cn//jpush/server/sdk/java_sdk/