Skip to content

Commit 4492798

Browse files
author
zhanq
committed
add:支持liveActivity
1 parent 1ee28b0 commit 4492798

File tree

6 files changed

+590
-299
lines changed

6 files changed

+590
-299
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>cn.jpush.api</groupId>
55
<artifactId>jpush-client</artifactId>
6-
<version>3.6.6</version>
6+
<version>3.6.8</version>
77
<packaging>jar</packaging>
88
<url>https://github.com/jpush/jpush-api-java-client</url>
99
<name>JPush API Java Client</name>

src/main/java/cn/jpush/api/JPushClient.java

Lines changed: 322 additions & 295 deletions
Large diffs are not rendered by default.

src/main/java/cn/jpush/api/push/PushClient.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import cn.jiguang.common.utils.sm2.SM2Util;
1111
import cn.jpush.api.push.model.*;
1212
import cn.jpush.api.push.model.audience.Audience;
13+
import cn.jpush.api.push.model.live_activity.LiveActivity;
1314
import com.google.gson.*;
1415

1516
import java.util.List;
@@ -45,7 +46,8 @@ public class PushClient {
4546
// encrypt type, the default value is empty
4647
private String _encryptType;
4748

48-
public PushClient() {}
49+
public PushClient() {
50+
}
4951

5052
/**
5153
* Create a Push Client.
@@ -57,8 +59,8 @@ public PushClient(String masterSecret, String appKey) {
5759
this(masterSecret, appKey, null, ClientConfig.getInstance());
5860
}
5961

60-
public PushClient(String masterSecret, String appKey,ClientConfig clientConfig){
61-
this(masterSecret,appKey,null,clientConfig);
62+
public PushClient(String masterSecret, String appKey, ClientConfig clientConfig) {
63+
this(masterSecret, appKey, null, clientConfig);
6264
}
6365

6466
/**
@@ -379,6 +381,12 @@ private void checkPushPayload(PushPayload pushPayload) {
379381
}
380382
}
381383

384+
385+
public PushResult sendLiveActivity(LiveActivity liveActivity) throws APIConnectionException, APIRequestException {
386+
ResponseWrapper response = _httpClient.sendPost(_baseUrl + _pushPath, liveActivity.toJSON().toString());
387+
return BaseResult.fromResponse(response, PushResult.class);
388+
}
389+
382390
}
383391

384392

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.jpush.api.push.model.live_activity;
2+
3+
public enum LiveActivityEvent {
4+
5+
UPDATE("update", "更新"),
6+
END("end", "结束,dismissal-date为结束展示时间");
7+
8+
private String value;
9+
private String describe;
10+
11+
LiveActivityEvent(String value, String describe) {
12+
this.value = value;
13+
this.describe = describe;
14+
}
15+
16+
public String getValue() {
17+
return this.value;
18+
}
19+
20+
public String getDescribe() {
21+
return this.describe;
22+
}
23+
24+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cn.jpush.api.push.model;
2+
3+
import cn.jiguang.common.resp.APIConnectionException;
4+
import cn.jiguang.common.resp.APIRequestException;
5+
import cn.jpush.api.FastTests;
6+
import cn.jpush.api.JPushClient;
7+
import cn.jpush.api.push.PushResult;
8+
import cn.jpush.api.push.model.live_activity.LiveActivity;
9+
import cn.jpush.api.push.model.live_activity.LiveActivityEvent;
10+
import org.junit.Test;
11+
import org.junit.experimental.categories.Category;
12+
13+
@Category(FastTests.class)
14+
public class LiveActivityTest {
15+
16+
@Test
17+
public void send() {
18+
LiveActivity liveActivity = new LiveActivity.Builder()
19+
.liveActivityId("LiveActivity-1")
20+
.apnsProduction(false)
21+
.iOSEvent(LiveActivityEvent.UPDATE)
22+
.iOSContentState("eventStr", "你好")
23+
.iOSContentState("eventTime", System.currentTimeMillis())
24+
.build();
25+
System.out.println("send liveActivity param:" + liveActivity.toJSON());
26+
27+
try {
28+
JPushClient pushClient = new JPushClient("8d8623440ff329ff38597da3", "2785bc46145eaa91a00c0728");
29+
PushResult pushResult = pushClient.sendLiveActivity(liveActivity);
30+
System.out.println("send liveActivity result:" + pushResult);
31+
} catch (APIConnectionException e) {
32+
throw new RuntimeException(e);
33+
} catch (APIRequestException e) {
34+
throw new RuntimeException(e);
35+
}
36+
}
37+
38+
}

0 commit comments

Comments
 (0)