/** 发送jpush到客户端 */ @Override public boolean sendByRegisitrationID( String msg, Map<String, String> extras, Integer imsi, String... regisitrationIDs) { String extrasJson = new Gson().toJson(extras); // extrasJson = extrasJson.replaceAll("\"", "'"); PushPayload payload = null; if (imsi == IMSI_ANDROID) { payload = PushPayload.newBuilder() .setPlatform(Platform.all()) .setAudience(Audience.registrationId(regisitrationIDs)) .setMessage( Message.newBuilder().setMsgContent(msg).addExtra("data", extrasJson).build()) .build(); } // else if(imsi==IMSI_IOS){ // payload = PushPayload.newBuilder() // .setPlatform(Platform.all()) // .setAudience(Audience.registrationId(regisitrationIDs)) // .setNotification(Notification.ios(msg, extras)) //alert(msg) // .build(); // } else { // 默认按iphone处理 payload = PushPayload.newBuilder() .setPlatform(Platform.all()) .setAudience(Audience.registrationId(regisitrationIDs)) .setNotification(Notification.ios(msg, extras)) // alert(msg) .build(); } // PushPayload payload = PushPayload // .newBuilder() // .setPlatform(Platform.all()) // .setAudience(Audience.registrationId(regisitrationIDs)) // .setMessage( // Message.newBuilder().setMsgContent(msg) // .addExtra("data", extrasJson).build()).build(); // // PushPayload payload = PushPayload.newBuilder() // .setPlatform(Platform.all()) // .setAudience(Audience.registrationId(regisitrationIDs)) // .setNotification(Notification.ios(msg, extras)) //alert(msg) // .setMessage( // Message.newBuilder().setMsgContent(msg) // .addExtra("data", extrasJson).build()) // .build(); try { PushResult result = jpushClient.sendPush(payload); return result.isResultOK(); } catch (Exception e1) { errorLogger.writeErrorLog(e1.getMessage()); appLogger.error(e1.getMessage(), e1); debugLogger.error(e1.getMessage(), e1); return false; } }
public PushPayload buildAndroidPayload(String msg, String devId, Map<String, String> extra) { Message.Builder msgBuilder = Message.newBuilder().setMsgContent(msg); if (extra != null) msgBuilder.addExtras(extra); return PushPayload.newBuilder() .setPlatform(Platform.android()) .setAudience(Audience.registrationId(devId)) // .setNotification(Notification.android(msg, msg, extra)) .setMessage(msgBuilder.build()) .build(); }
/** * 构建推送对象:平台是 Andorid 与 iOS,推送目标是 ("tag1" 与 "tag2" 的并集) 且("alias1" 与 "alias2" 的并集),推送内容是 - 内容为 * MSG_CONTENT 的消息, 并且附加字段 from = JPush。 * * @return */ private static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() { return PushPayload.newBuilder() .setPlatform(Platform.android_ios()) .setAudience( Audience.newBuilder() .addAudienceTarget(AudienceTarget.tag("tag1", "tag2")) .addAudienceTarget(AudienceTarget.alias("alias1", "alias2")) .build()) .setMessage( Message.newBuilder().setMsgContent("MSG_CONTENT").addExtra("from", "JPush").build()) .setOptions(Options.newBuilder().setApnsProduction(PRODUCT_MODE).build()) .build(); }
/** 发送jpush到工作端口 */ @Override public boolean sendByRegisitrationIDToWorker( String msg, Map<String, String> extras, String... regisitrationIDs) { // PushPayload payload = PushPayload.newBuilder() // .setPlatform(Platform.all()) // .setAudience(Audience.registrationId(regisitrationIDs)) // .setNotification(Notification.newBuilder() // .addPlatformNotification(AndroidNotification.newBuilder() // .addExtra("orderID", orderID) // .build()) // .build()) // .setMessage(Message.content(msg)) // .build(); String extrasJson = new Gson().toJson(extras); // extrasJson = extrasJson.replaceAll("\"", "'"); PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.all()) .setAudience(Audience.registrationId(regisitrationIDs)) // .setNotification(Notification.ios(msg, extras)) //alert(msg) .setMessage( Message.newBuilder().setMsgContent(msg).addExtra("data", extrasJson).build()) .build(); // PushPayload payload = PushPayload.newBuilder() // .setPlatform(Platform.all()) // .setAudience(Audience.registrationId(regisitrationIDs)) // .setNotification( Notification.android(msg, msg, extras) // //.ios(msg, extras) // Notification.alert("") // ).build(); try { PushResult result = jpushClientForService.sendPush(payload); return result.isResultOK(); } catch (Exception e1) { errorLogger.writeErrorLog(e1.getMessage()); appLogger.error(e1.getMessage(), e1); debugLogger.error(e1.getMessage(), e1); return false; } }
@Test public void testMsgContentAndExtras() { Message message = Message.newBuilder() .setMsgContent("msgContent") .addExtra("key1", "value1") .addExtra("key2", 222) .addExtra("key3", Boolean.FALSE) .build(); JsonObject json = new JsonObject(); json.add("msg_content", new JsonPrimitive("msgContent")); JsonObject extras = new JsonObject(); extras.add("key1", new JsonPrimitive("value1")); extras.add("key2", new JsonPrimitive(222)); extras.add("key3", new JsonPrimitive(Boolean.FALSE)); json.add("extras", extras); Assert.assertEquals("", json, message.toJSON()); }
@Test(expected = IllegalArgumentException.class) public void testIllegal() { Message.newBuilder().build(); }