diff --git a/pom.xml b/pom.xml index 1ba4fb00..7dc3ce9c 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ cn.jpush.api jpush-client - 3.7.0 + 3.7.1 jar https://github.com/jpush/jpush-api-java-client JPush API Java Client diff --git a/src/main/java/cn/jpush/api/push/model/live_activity/LiveActivity.java b/src/main/java/cn/jpush/api/push/model/live_activity/LiveActivity.java index eefc1b80..2a8fa091 100644 --- a/src/main/java/cn/jpush/api/push/model/live_activity/LiveActivity.java +++ b/src/main/java/cn/jpush/api/push/model/live_activity/LiveActivity.java @@ -1,10 +1,7 @@ package cn.jpush.api.push.model.live_activity; import cn.jpush.api.push.model.PushModel; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; +import com.google.gson.*; public class LiveActivity implements PushModel { @@ -15,13 +12,15 @@ public class LiveActivity implements PushModel { private final String iOSEvent; private final JsonObject iOSContentState; private final Long iOSDismissalDate; + private final JsonObject iOSAlert; - public LiveActivity(Boolean apnsProduction, String liveActivityId, String iOSEvent, JsonObject iOSContentState, Long iOSDismissalDate) { + public LiveActivity(Boolean apnsProduction, String liveActivityId, String iOSEvent, JsonObject iOSContentState, Long iOSDismissalDate, JsonObject iOSAlert) { this.apnsProduction = apnsProduction; this.liveActivityId = liveActivityId; this.iOSEvent = iOSEvent; this.iOSContentState = iOSContentState; this.iOSDismissalDate = iOSDismissalDate; + this.iOSAlert = iOSAlert; } public static Builder newBuilder() { @@ -34,6 +33,7 @@ public static class Builder { private String iOSEvent; private JsonObject iOSContentState; private Long iOSDismissalDate; + private JsonObject iOSAlert; public Builder apnsProduction(Boolean apnsProduction) { this.apnsProduction = apnsProduction; @@ -81,15 +81,55 @@ public Builder iOSDismissalDate(Long iOSDismissalDate) { return this; } + public Builder iOSAlertTitle(String iosAlertTitle) { + if (this.iOSAlert == null) { + this.iOSAlert = new JsonObject(); + } + this.iOSAlert.addProperty("title", iosAlertTitle); + return this; + } + + public Builder iOSAlertAlternateTitle(String iosAlertAlternateTitle) { + if (this.iOSAlert == null) { + this.iOSAlert = new JsonObject(); + } + this.iOSAlert.addProperty("alternate_title", iosAlertAlternateTitle); + return this; + } + + public Builder iOSAlertBody(String iosAlertBody) { + if (this.iOSAlert == null) { + this.iOSAlert = new JsonObject(); + } + this.iOSAlert.addProperty("body", iosAlertBody); + return this; + } + + public Builder iOSAlertAlternateBody(String iosAlertAlternateBody) { + if (this.iOSAlert == null) { + this.iOSAlert = new JsonObject(); + } + this.iOSAlert.addProperty("alternate_body", iosAlertAlternateBody); + return this; + } + + public Builder iOSAlertSound(String iosAlertSound) { + if (this.iOSAlert == null) { + this.iOSAlert = new JsonObject(); + } + this.iOSAlert.addProperty("sound", iosAlertSound); + return this; + } + public LiveActivity build() { - return new LiveActivity(apnsProduction, liveActivityId, iOSEvent, iOSContentState, iOSDismissalDate); + return new LiveActivity(apnsProduction, liveActivityId, iOSEvent, iOSContentState, iOSDismissalDate, iOSAlert); } } @Override public JsonElement toJSON() { - JsonObject jsonObject = new JsonObject(); + JsonObject pushJsonObject = new JsonObject(); JsonArray platformJsonArray = new JsonArray(); platformJsonArray.add(new JsonPrimitive("ios")); @@ -99,37 +139,42 @@ public JsonElement toJSON() { audienceJsonObject.addProperty("live_activity_id", liveActivityId); } - JsonObject optionsJsonObject = new JsonObject(); - if (apnsProduction != null) { - optionsJsonObject.addProperty("apns_production", apnsProduction); - } - JsonObject liveActivityJsonObject = new JsonObject(); JsonObject iOSJsonObject = new JsonObject(); - JsonObject alertJsonObject = new JsonObject(); if (iOSEvent != null) { iOSJsonObject.addProperty("event", iOSEvent); } + if (iOSContentState != null) { iOSJsonObject.add("content-state", iOSContentState); } - if (!alertJsonObject.entrySet().isEmpty()) { - iOSJsonObject.add("alert", alertJsonObject); - } - if (!alertJsonObject.entrySet().isEmpty()) { + + if (iOSDismissalDate != null) { iOSJsonObject.addProperty("dismissal-date", iOSDismissalDate); } + if (iOSAlert != null) { + iOSJsonObject.add("alert", iOSAlert); + } + if (!iOSJsonObject.entrySet().isEmpty()) { liveActivityJsonObject.add("ios", iOSJsonObject); } - jsonObject.add("platform", platformJsonArray); - jsonObject.add("audience", audienceJsonObject); - jsonObject.add("live_activity", liveActivityJsonObject); - jsonObject.add("options", optionsJsonObject); - return jsonObject; + JsonObject optionsJsonObject = new JsonObject(); + if (apnsProduction != null) { + optionsJsonObject.addProperty("apns_production", apnsProduction); + } + if (iOSAlert != null) { + optionsJsonObject.addProperty("alternate_set", true); + } + + pushJsonObject.add("platform", platformJsonArray); + pushJsonObject.add("audience", audienceJsonObject); + pushJsonObject.add("live_activity", liveActivityJsonObject); + pushJsonObject.add("options", optionsJsonObject); + return pushJsonObject; } } diff --git a/src/test/java/cn/jpush/api/push/model/LiveActivityTest.java b/src/test/java/cn/jpush/api/push/model/LiveActivityTest.java index 5dfed5b4..b8d1a797 100644 --- a/src/test/java/cn/jpush/api/push/model/LiveActivityTest.java +++ b/src/test/java/cn/jpush/api/push/model/LiveActivityTest.java @@ -21,6 +21,13 @@ public void update() { .iOSEvent(LiveActivityEvent.UPDATE) .iOSContentState("eventStr", "更新") .iOSContentState("eventTime", System.currentTimeMillis()) + .iOSContentState("eventBool", true) + // 需要联系商务开通 alternate_set 才能使用 + // .iOSAlertTitle("alertTitle") + // .iOSAlertAlternateTitle("alertAlternateTitle") + // .iOSAlertBody("alertBody") + // .iOSAlertAlternateBody("alertAlternateBody") + // .iOSAlertSound("alertSound") .build(); System.out.println("send liveActivity param:" + liveActivity.toJSON());