Skip to content

Commit

Permalink
add:同步官网更新
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanq committed Jun 13, 2023
1 parent c9f7cf4 commit 14e80b2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.7.0</version>
<version>3.7.1</version>
<packaging>jar</packaging>
<url>https://github.com/jpush/jpush-api-java-client</url>
<name>JPush API Java Client</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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() {
Expand All @@ -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;
Expand Down Expand Up @@ -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"));
Expand All @@ -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;
}

}
7 changes: 7 additions & 0 deletions src/test/java/cn/jpush/api/push/model/LiveActivityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down

0 comments on commit 14e80b2

Please sign in to comment.