|
| 1 | +package cn.jpush.api.push.model.live_activity; |
| 2 | + |
| 3 | +import cn.jpush.api.push.model.PushModel; |
| 4 | +import com.google.gson.JsonArray; |
| 5 | +import com.google.gson.JsonElement; |
| 6 | +import com.google.gson.JsonObject; |
| 7 | +import com.google.gson.JsonPrimitive; |
| 8 | + |
| 9 | +public class LiveActivity implements PushModel { |
| 10 | + |
| 11 | + private final Boolean apnsProduction; |
| 12 | + |
| 13 | + private final String liveActivityId; |
| 14 | + |
| 15 | + private final String iOSEvent; |
| 16 | + private final JsonObject iOSContentState; |
| 17 | + private final String iOSAlertTitle; |
| 18 | + private final String iOSAlertAlternateTitle; |
| 19 | + private final String iOSAlertBody; |
| 20 | + private final String iOSAlertAlternateBody; |
| 21 | + private final String iOSAlertSound; |
| 22 | + private final Integer iOSDismissalDate; |
| 23 | + |
| 24 | + public LiveActivity(Boolean apnsProduction, String liveActivityId, String iOSEvent, JsonObject iOSContentState, String iOSAlertTitle, String iOSAlertAlternateTitle, String iOSAlertBody, String iOSAlertAlternateBody, String iOSAlertSound, Integer iOSDismissalDate) { |
| 25 | + this.apnsProduction = apnsProduction; |
| 26 | + this.liveActivityId = liveActivityId; |
| 27 | + this.iOSEvent = iOSEvent; |
| 28 | + this.iOSContentState = iOSContentState; |
| 29 | + this.iOSAlertTitle = iOSAlertTitle; |
| 30 | + this.iOSAlertAlternateTitle = iOSAlertAlternateTitle; |
| 31 | + this.iOSAlertBody = iOSAlertBody; |
| 32 | + this.iOSAlertAlternateBody = iOSAlertAlternateBody; |
| 33 | + this.iOSAlertSound = iOSAlertSound; |
| 34 | + this.iOSDismissalDate = iOSDismissalDate; |
| 35 | + } |
| 36 | + |
| 37 | + public static Builder newBuilder() { |
| 38 | + return new Builder(); |
| 39 | + } |
| 40 | + |
| 41 | + public static class Builder { |
| 42 | + private Boolean apnsProduction; |
| 43 | + private String liveActivityId; |
| 44 | + private String iOSEvent; |
| 45 | + private JsonObject iOSContentState; |
| 46 | + private String iOSAlertTitle; |
| 47 | + private String iOSAlertAlternateTitle; |
| 48 | + private String iOSAlertBody; |
| 49 | + private String iOSAlertAlternateBody; |
| 50 | + private String iOSAlertSound; |
| 51 | + private Integer iOSDismissalDate; |
| 52 | + |
| 53 | + public Builder apnsProduction(Boolean apnsProduction) { |
| 54 | + this.apnsProduction = apnsProduction; |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + public Builder liveActivityId(String liveActivityId) { |
| 59 | + this.liveActivityId = liveActivityId; |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + public Builder iOSEvent(LiveActivityEvent iOSEvent) { |
| 64 | + if (iOSEvent != null) { |
| 65 | + this.iOSEvent = iOSEvent.getValue(); |
| 66 | + } |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + public Builder iOSContentState(String key, String value) { |
| 71 | + if (this.iOSContentState == null) { |
| 72 | + this.iOSContentState = new JsonObject(); |
| 73 | + } |
| 74 | + this.iOSContentState.addProperty(key, value); |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + public Builder iOSContentState(String key, Number value) { |
| 79 | + if (this.iOSContentState == null) { |
| 80 | + this.iOSContentState = new JsonObject(); |
| 81 | + } |
| 82 | + this.iOSContentState.addProperty(key, value); |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + public Builder iOSContentState(String key, Boolean value) { |
| 87 | + if (this.iOSContentState == null) { |
| 88 | + this.iOSContentState = new JsonObject(); |
| 89 | + } |
| 90 | + this.iOSContentState.addProperty(key, value); |
| 91 | + return this; |
| 92 | + } |
| 93 | + |
| 94 | + public Builder iOSAlertTitle(String iOSAlertTitle) { |
| 95 | + this.iOSAlertTitle = iOSAlertTitle; |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + public Builder iOSAlertAlternateTitle(String iOSAlertAlternateTitle) { |
| 100 | + this.iOSAlertAlternateTitle = iOSAlertAlternateTitle; |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + public Builder iOSAlertBody(String iOSAlertBody) { |
| 105 | + this.iOSAlertBody = iOSAlertBody; |
| 106 | + return this; |
| 107 | + } |
| 108 | + |
| 109 | + public Builder iOSAlertAlternateBody(String iOSAlertAlternateBody) { |
| 110 | + this.iOSAlertAlternateBody = iOSAlertAlternateBody; |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + public Builder iOSAlertSound(String iOSAlertSound) { |
| 115 | + this.iOSAlertSound = iOSAlertSound; |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + public Builder iOSDismissalDate(Integer iOSDismissalDate) { |
| 120 | + this.iOSDismissalDate = iOSDismissalDate; |
| 121 | + return this; |
| 122 | + } |
| 123 | + |
| 124 | + public LiveActivity build() { |
| 125 | + return new LiveActivity(apnsProduction, liveActivityId, iOSEvent, iOSContentState, iOSAlertTitle, iOSAlertAlternateTitle, iOSAlertBody, iOSAlertAlternateBody, iOSAlertSound, iOSDismissalDate); |
| 126 | + } |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public JsonElement toJSON() { |
| 132 | + JsonObject jsonObject = new JsonObject(); |
| 133 | + |
| 134 | + JsonArray platformJsonArray = new JsonArray(); |
| 135 | + platformJsonArray.add(new JsonPrimitive("ios")); |
| 136 | + |
| 137 | + JsonObject audienceJsonObject = new JsonObject(); |
| 138 | + if (liveActivityId != null) { |
| 139 | + audienceJsonObject.addProperty("live_activity_id", liveActivityId); |
| 140 | + } |
| 141 | + |
| 142 | + JsonObject optionsJsonObject = new JsonObject(); |
| 143 | + if (apnsProduction != null) { |
| 144 | + optionsJsonObject.addProperty("apns_production", apnsProduction); |
| 145 | + } |
| 146 | + if (iOSAlertTitle != null || iOSAlertAlternateTitle != null || iOSAlertBody != null || iOSAlertAlternateBody != null || iOSAlertSound != null) { |
| 147 | + optionsJsonObject.addProperty("alternate_set", true); |
| 148 | + } |
| 149 | + |
| 150 | + JsonObject liveActivityJsonObject = new JsonObject(); |
| 151 | + JsonObject iOSJsonObject = new JsonObject(); |
| 152 | + JsonObject alertJsonObject = new JsonObject(); |
| 153 | + |
| 154 | + if (iOSAlertTitle != null) { |
| 155 | + alertJsonObject.addProperty("title", iOSAlertTitle); |
| 156 | + } |
| 157 | + if (iOSAlertAlternateTitle != null) { |
| 158 | + alertJsonObject.addProperty("alternate_title", iOSAlertAlternateTitle); |
| 159 | + } |
| 160 | + if (iOSAlertBody != null) { |
| 161 | + alertJsonObject.addProperty("body", iOSAlertBody); |
| 162 | + } |
| 163 | + if (iOSAlertAlternateBody != null) { |
| 164 | + alertJsonObject.addProperty("alternate_body", iOSAlertAlternateBody); |
| 165 | + } |
| 166 | + if (iOSAlertSound != null) { |
| 167 | + alertJsonObject.addProperty("sound", iOSAlertSound); |
| 168 | + } |
| 169 | + |
| 170 | + if (iOSEvent != null) { |
| 171 | + iOSJsonObject.addProperty("event", iOSEvent); |
| 172 | + } |
| 173 | + if (iOSContentState != null) { |
| 174 | + iOSJsonObject.add("content-state", iOSContentState); |
| 175 | + } |
| 176 | + if (!alertJsonObject.entrySet().isEmpty()) { |
| 177 | + iOSJsonObject.add("alert", alertJsonObject); |
| 178 | + } |
| 179 | + if (iOSDismissalDate != null) { |
| 180 | + iOSJsonObject.addProperty("dismissal-date", iOSDismissalDate); |
| 181 | + } |
| 182 | + |
| 183 | + if (!iOSJsonObject.entrySet().isEmpty()) { |
| 184 | + liveActivityJsonObject.add("ios", iOSJsonObject); |
| 185 | + } |
| 186 | + |
| 187 | + jsonObject.add("platform", platformJsonArray); |
| 188 | + jsonObject.add("audience", audienceJsonObject); |
| 189 | + jsonObject.add("live_activity", liveActivityJsonObject); |
| 190 | + jsonObject.add("options", optionsJsonObject); |
| 191 | + return jsonObject; |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments