Skip to content

Commit

Permalink
add:支持liveActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanq committed May 26, 2023
1 parent 1ee28b0 commit 4492798
Show file tree
Hide file tree
Showing 6 changed files with 590 additions and 299 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.6.6</version>
<version>3.6.8</version>
<packaging>jar</packaging>
<url>https://github.com/jpush/jpush-api-java-client</url>
<name>JPush API Java Client</name>
Expand Down
617 changes: 322 additions & 295 deletions src/main/java/cn/jpush/api/JPushClient.java

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions src/main/java/cn/jpush/api/push/PushClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import cn.jiguang.common.utils.sm2.SM2Util;
import cn.jpush.api.push.model.*;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.live_activity.LiveActivity;
import com.google.gson.*;

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

public PushClient() {}
public PushClient() {
}

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

public PushClient(String masterSecret, String appKey,ClientConfig clientConfig){
this(masterSecret,appKey,null,clientConfig);
public PushClient(String masterSecret, String appKey, ClientConfig clientConfig) {
this(masterSecret, appKey, null, clientConfig);
}

/**
Expand Down Expand Up @@ -379,6 +381,12 @@ private void checkPushPayload(PushPayload pushPayload) {
}
}


public PushResult sendLiveActivity(LiveActivity liveActivity) throws APIConnectionException, APIRequestException {
ResponseWrapper response = _httpClient.sendPost(_baseUrl + _pushPath, liveActivity.toJSON().toString());
return BaseResult.fromResponse(response, PushResult.class);
}

}


194 changes: 194 additions & 0 deletions src/main/java/cn/jpush/api/push/model/live_activity/LiveActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
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;

public class LiveActivity implements PushModel {

private final Boolean apnsProduction;

private final String liveActivityId;

private final String iOSEvent;
private final JsonObject iOSContentState;
private final String iOSAlertTitle;
private final String iOSAlertAlternateTitle;
private final String iOSAlertBody;
private final String iOSAlertAlternateBody;
private final String iOSAlertSound;
private final Integer iOSDismissalDate;

public LiveActivity(Boolean apnsProduction, String liveActivityId, String iOSEvent, JsonObject iOSContentState, String iOSAlertTitle, String iOSAlertAlternateTitle, String iOSAlertBody, String iOSAlertAlternateBody, String iOSAlertSound, Integer iOSDismissalDate) {
this.apnsProduction = apnsProduction;
this.liveActivityId = liveActivityId;
this.iOSEvent = iOSEvent;
this.iOSContentState = iOSContentState;
this.iOSAlertTitle = iOSAlertTitle;
this.iOSAlertAlternateTitle = iOSAlertAlternateTitle;
this.iOSAlertBody = iOSAlertBody;
this.iOSAlertAlternateBody = iOSAlertAlternateBody;
this.iOSAlertSound = iOSAlertSound;
this.iOSDismissalDate = iOSDismissalDate;
}

public static Builder newBuilder() {
return new Builder();
}

public static class Builder {
private Boolean apnsProduction;
private String liveActivityId;
private String iOSEvent;
private JsonObject iOSContentState;
private String iOSAlertTitle;
private String iOSAlertAlternateTitle;
private String iOSAlertBody;
private String iOSAlertAlternateBody;
private String iOSAlertSound;
private Integer iOSDismissalDate;

public Builder apnsProduction(Boolean apnsProduction) {
this.apnsProduction = apnsProduction;
return this;
}

public Builder liveActivityId(String liveActivityId) {
this.liveActivityId = liveActivityId;
return this;
}

public Builder iOSEvent(LiveActivityEvent iOSEvent) {
if (iOSEvent != null) {
this.iOSEvent = iOSEvent.getValue();
}
return this;
}

public Builder iOSContentState(String key, String value) {
if (this.iOSContentState == null) {
this.iOSContentState = new JsonObject();
}
this.iOSContentState.addProperty(key, value);
return this;
}

public Builder iOSContentState(String key, Number value) {
if (this.iOSContentState == null) {
this.iOSContentState = new JsonObject();
}
this.iOSContentState.addProperty(key, value);
return this;
}

public Builder iOSContentState(String key, Boolean value) {
if (this.iOSContentState == null) {
this.iOSContentState = new JsonObject();
}
this.iOSContentState.addProperty(key, value);
return this;
}

public Builder iOSAlertTitle(String iOSAlertTitle) {
this.iOSAlertTitle = iOSAlertTitle;
return this;
}

public Builder iOSAlertAlternateTitle(String iOSAlertAlternateTitle) {
this.iOSAlertAlternateTitle = iOSAlertAlternateTitle;
return this;
}

public Builder iOSAlertBody(String iOSAlertBody) {
this.iOSAlertBody = iOSAlertBody;
return this;
}

public Builder iOSAlertAlternateBody(String iOSAlertAlternateBody) {
this.iOSAlertAlternateBody = iOSAlertAlternateBody;
return this;
}

public Builder iOSAlertSound(String iOSAlertSound) {
this.iOSAlertSound = iOSAlertSound;
return this;
}

public Builder iOSDismissalDate(Integer iOSDismissalDate) {
this.iOSDismissalDate = iOSDismissalDate;
return this;
}

public LiveActivity build() {
return new LiveActivity(apnsProduction, liveActivityId, iOSEvent, iOSContentState, iOSAlertTitle, iOSAlertAlternateTitle, iOSAlertBody, iOSAlertAlternateBody, iOSAlertSound, iOSDismissalDate);
}

}

@Override
public JsonElement toJSON() {
JsonObject jsonObject = new JsonObject();

JsonArray platformJsonArray = new JsonArray();
platformJsonArray.add(new JsonPrimitive("ios"));

JsonObject audienceJsonObject = new JsonObject();
if (liveActivityId != null) {
audienceJsonObject.addProperty("live_activity_id", liveActivityId);
}

JsonObject optionsJsonObject = new JsonObject();
if (apnsProduction != null) {
optionsJsonObject.addProperty("apns_production", apnsProduction);
}
if (iOSAlertTitle != null || iOSAlertAlternateTitle != null || iOSAlertBody != null || iOSAlertAlternateBody != null || iOSAlertSound != null) {
optionsJsonObject.addProperty("alternate_set", true);
}

JsonObject liveActivityJsonObject = new JsonObject();
JsonObject iOSJsonObject = new JsonObject();
JsonObject alertJsonObject = new JsonObject();

if (iOSAlertTitle != null) {
alertJsonObject.addProperty("title", iOSAlertTitle);
}
if (iOSAlertAlternateTitle != null) {
alertJsonObject.addProperty("alternate_title", iOSAlertAlternateTitle);
}
if (iOSAlertBody != null) {
alertJsonObject.addProperty("body", iOSAlertBody);
}
if (iOSAlertAlternateBody != null) {
alertJsonObject.addProperty("alternate_body", iOSAlertAlternateBody);
}
if (iOSAlertSound != null) {
alertJsonObject.addProperty("sound", iOSAlertSound);
}

if (iOSEvent != null) {
iOSJsonObject.addProperty("event", iOSEvent);
}
if (iOSContentState != null) {
iOSJsonObject.add("content-state", iOSContentState);
}
if (!alertJsonObject.entrySet().isEmpty()) {
iOSJsonObject.add("alert", alertJsonObject);
}
if (iOSDismissalDate != null) {
iOSJsonObject.addProperty("dismissal-date", iOSDismissalDate);
}

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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cn.jpush.api.push.model.live_activity;

public enum LiveActivityEvent {

UPDATE("update", "更新"),
END("end", "结束,dismissal-date为结束展示时间");

private String value;
private String describe;

LiveActivityEvent(String value, String describe) {
this.value = value;
this.describe = describe;
}

public String getValue() {
return this.value;
}

public String getDescribe() {
return this.describe;
}

}
38 changes: 38 additions & 0 deletions src/test/java/cn/jpush/api/push/model/LiveActivityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cn.jpush.api.push.model;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.FastTests;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.live_activity.LiveActivity;
import cn.jpush.api.push.model.live_activity.LiveActivityEvent;
import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category(FastTests.class)
public class LiveActivityTest {

@Test
public void send() {
LiveActivity liveActivity = new LiveActivity.Builder()
.liveActivityId("LiveActivity-1")
.apnsProduction(false)
.iOSEvent(LiveActivityEvent.UPDATE)
.iOSContentState("eventStr", "你好")
.iOSContentState("eventTime", System.currentTimeMillis())
.build();
System.out.println("send liveActivity param:" + liveActivity.toJSON());

try {
JPushClient pushClient = new JPushClient("8d8623440ff329ff38597da3", "2785bc46145eaa91a00c0728");
PushResult pushResult = pushClient.sendLiveActivity(liveActivity);
System.out.println("send liveActivity result:" + pushResult);
} catch (APIConnectionException e) {
throw new RuntimeException(e);
} catch (APIRequestException e) {
throw new RuntimeException(e);
}
}

}

0 comments on commit 4492798

Please sign in to comment.