From c3edecb51de360e0428953ea5971180d755b6170 Mon Sep 17 00:00:00 2001 From: wicked-tc130 Date: Thu, 1 Aug 2024 11:00:09 +0800 Subject: [PATCH] support group push, update version to 5.1.3. --- README.md | 4 +- example-for-spring/pom.xml | 4 +- .../jiguang/app/config/JiguangApiConfig.java | 21 +++- .../src/main/resources/application.yml | 2 + .../cn/jiguang/app/api/GroupPushApiTest.java | 96 +++++++++++++++++++ .../target/classes/application.yml | 2 + jiguang-sdk/pom.xml | 4 +- .../java/cn/jiguang/sdk/api/GroupPushApi.java | 81 ++++++++++++++++ .../sdk/bean/push/GroupPushSendParam.java | 8 ++ .../sdk/bean/push/GroupPushSendResult.java | 42 ++++++++ .../jiguang/sdk/client/GroupPushClient.java | 17 ++++ .../java/cn/jiguang/sdk/codec/ApiDecoder.java | 6 +- .../java/cn/jiguang/sdk/codec/ApiEncoder.java | 7 +- pom.xml | 2 +- 14 files changed, 279 insertions(+), 17 deletions(-) create mode 100644 example-for-spring/src/test/java/cn/jiguang/app/api/GroupPushApiTest.java create mode 100644 jiguang-sdk/src/main/java/cn/jiguang/sdk/api/GroupPushApi.java create mode 100644 jiguang-sdk/src/main/java/cn/jiguang/sdk/bean/push/GroupPushSendParam.java create mode 100644 jiguang-sdk/src/main/java/cn/jiguang/sdk/bean/push/GroupPushSendResult.java create mode 100644 jiguang-sdk/src/main/java/cn/jiguang/sdk/client/GroupPushClient.java diff --git a/README.md b/README.md index 3c529d6..9461c9e 100644 --- a/README.md +++ b/README.md @@ -14,13 +14,13 @@ ## 1. 集成 引入sdk包 ```xml - + io.github.jpush jiguang-sdk - 5.1.2 + 5.1.3 ``` diff --git a/example-for-spring/pom.xml b/example-for-spring/pom.xml index ddfe66d..0084b63 100644 --- a/example-for-spring/pom.xml +++ b/example-for-spring/pom.xml @@ -13,7 +13,7 @@ io.github.jpush example-for-spring - 5.1.2 + 5.1.3 8 @@ -26,7 +26,7 @@ io.github.jpush jiguang-sdk - 5.1.2 + 5.1.3 diff --git a/example-for-spring/src/main/java/cn/jiguang/app/config/JiguangApiConfig.java b/example-for-spring/src/main/java/cn/jiguang/app/config/JiguangApiConfig.java index 7101869..7636100 100644 --- a/example-for-spring/src/main/java/cn/jiguang/app/config/JiguangApiConfig.java +++ b/example-for-spring/src/main/java/cn/jiguang/app/config/JiguangApiConfig.java @@ -1,9 +1,7 @@ package cn.jiguang.app.config; -import cn.jiguang.sdk.api.AdminApi; -import cn.jiguang.sdk.api.DeviceApi; -import cn.jiguang.sdk.api.PushApi; -import cn.jiguang.sdk.api.ReportApi; +import cn.jiguang.sdk.api.*; +import feign.Logger; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -23,6 +21,12 @@ public class JiguangApiConfig { @Value("${jiguang.api.dev-secret}") private String devSecret; + @Value("${jiguang.api.group-key}") + private String groupKey; + + @Value("${jiguang.api.group-master-secret}") + private String groupMasterSecret; + @Bean public PushApi pushApi() { return new PushApi.Builder() @@ -55,4 +59,13 @@ public AdminApi adminApi() { .build(); } + @Bean + public GroupPushApi groupPushApi() { + return new GroupPushApi.Builder() + .setGroupKey(groupKey) + .setGroupMasterSecret(groupMasterSecret) + .setLoggerLevel(Logger.Level.FULL) + .build(); + } + } diff --git a/example-for-spring/src/main/resources/application.yml b/example-for-spring/src/main/resources/application.yml index 2b949cf..cf50d7c 100644 --- a/example-for-spring/src/main/resources/application.yml +++ b/example-for-spring/src/main/resources/application.yml @@ -16,5 +16,7 @@ jiguang: master-secret: bcf3b3327000abce4764f862 dev-key: c2dc75e97486529205528b23 dev-secret: d219e2f001df2fe4f08b6754 + group-key: 2ed1465b94aab3f03f6778e0 + group-master-secret: 5b69dce5f9dc0dabe70aa33a diff --git a/example-for-spring/src/test/java/cn/jiguang/app/api/GroupPushApiTest.java b/example-for-spring/src/test/java/cn/jiguang/app/api/GroupPushApiTest.java new file mode 100644 index 0000000..70cd854 --- /dev/null +++ b/example-for-spring/src/test/java/cn/jiguang/app/api/GroupPushApiTest.java @@ -0,0 +1,96 @@ +package cn.jiguang.app.api; + +import cn.jiguang.sdk.api.GroupPushApi; +import cn.jiguang.sdk.bean.push.GroupPushSendParam; +import cn.jiguang.sdk.bean.push.GroupPushSendResult; +import cn.jiguang.sdk.bean.push.audience.Audience; +import cn.jiguang.sdk.bean.push.message.notification.NotificationMessage; +import cn.jiguang.sdk.bean.push.options.Options; +import cn.jiguang.sdk.constants.ApiConstants; +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +@Slf4j +@SpringBootTest() +@RunWith(SpringRunner.class) +public class GroupPushApiTest { + + @Autowired + private GroupPushApi groupPushApi; + + @Test + public void send() { + GroupPushSendParam param = new GroupPushSendParam(); + // 通知内容 + NotificationMessage.Android android = new NotificationMessage.Android(); + android.setAlert("this is android alert"); + android.setTitle("this is android title"); + + NotificationMessage.IOS iOS = new NotificationMessage.IOS(); + Map iOSAlert = new HashMap<>(); + iOSAlert.put("title", "this is iOS title"); + iOSAlert.put("subtitle", "this is iOS subtitle"); + iOS.setAlert(iOSAlert); + + Map extrasMap = new HashMap<>(); + Map extrasParamMap = new HashMap<>(); + extrasParamMap.put("key1", "value1"); + extrasParamMap.put("key2", "value2"); + extrasMap.put("params", extrasParamMap); + android.setExtras(extrasMap); + iOS.setExtras(extrasMap); + + NotificationMessage notificationMessage = new NotificationMessage(); + notificationMessage.setAlert("this is alert"); + notificationMessage.setAndroid(android); + notificationMessage.setIos(iOS); + param.setNotification(notificationMessage); + + // 目标人群 + Audience audience = new Audience(); + audience.setRegistrationIdList(Arrays.asList("1104a89793af2cfc030", "1104a89793af2cfc030")); + // 指定目标 + param.setAudience(audience); + + // 或者发送所有人 + // param.setAudience(ApiConstants.Audience.ALL); + + // 指定平台 + // param.setPlatform(Arrays.asList(Platform.android, Platform.ios)); + // 或者发送所有平台 + param.setPlatform(ApiConstants.Platform.ALL); + + // Android厂商 + // param.setThirdNotificationMessage(); + + // 短信补充 + // param.setSmsMessage(); + + // 回调 + // param.setCallback(); + + // options + Options options = new Options(); + Map thirdPartyMap = new HashMap<>(); + Map huaweiMap = new HashMap<>(); + huaweiMap.put("distribution", "first_ospush"); + huaweiMap.put("importance", "NORMAL"); + huaweiMap.put("category", "MARKETING"); + thirdPartyMap.put("huawei", huaweiMap); + options.setThirdPartyChannel(thirdPartyMap); + // param.setOptions(options); + + // 发送 + GroupPushSendResult result = groupPushApi.send(param); + log.info("result:{}", result); + } + +} diff --git a/example-for-spring/target/classes/application.yml b/example-for-spring/target/classes/application.yml index 2b949cf..ee8cf5d 100644 --- a/example-for-spring/target/classes/application.yml +++ b/example-for-spring/target/classes/application.yml @@ -16,5 +16,7 @@ jiguang: master-secret: bcf3b3327000abce4764f862 dev-key: c2dc75e97486529205528b23 dev-secret: d219e2f001df2fe4f08b6754 + group-key: 1ed1465b94aab3f03f6778e0 + group-master-secret: 4b69dce5f9dc0dabe70aa33a diff --git a/jiguang-sdk/pom.xml b/jiguang-sdk/pom.xml index 000dfe0..3744c18 100644 --- a/jiguang-sdk/pom.xml +++ b/jiguang-sdk/pom.xml @@ -7,13 +7,13 @@ io.github.jpush jiguang-sdk-java - 5.1.2 + 5.1.3 ../pom.xml io.github.jpush jiguang-sdk - 5.1.2 + 5.1.3 jar diff --git a/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/GroupPushApi.java b/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/GroupPushApi.java new file mode 100644 index 0000000..00b0b5f --- /dev/null +++ b/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/GroupPushApi.java @@ -0,0 +1,81 @@ +package cn.jiguang.sdk.api; + +import cn.jiguang.sdk.bean.push.GroupPushSendParam; +import cn.jiguang.sdk.bean.push.GroupPushSendResult; +import cn.jiguang.sdk.client.GroupPushClient; +import cn.jiguang.sdk.codec.ApiDecoder; +import cn.jiguang.sdk.codec.ApiEncoder; +import cn.jiguang.sdk.codec.ApiErrorDecoder; +import feign.Feign; +import feign.Logger; +import feign.auth.BasicAuthRequestInterceptor; +import feign.okhttp.OkHttpClient; +import feign.slf4j.Slf4jLogger; +import lombok.NonNull; + +import java.net.Proxy; + +public class GroupPushApi { + + private final GroupPushClient groupPushClient; + + protected GroupPushApi(@NonNull GroupPushClient groupPushClient) { + this.groupPushClient = groupPushClient; + } + + public GroupPushSendResult send(@NonNull GroupPushSendParam param) { + return groupPushClient.send(param); + } + + public static class Builder { + + private String host = "https://api.jpush.cn"; + private Proxy proxy; + private String groupKey; + private String groupMasterSecret; + private Logger.Level loggerLevel = Logger.Level.BASIC; + + public Builder setHost(@NonNull String host) { + this.host = host; + return this; + } + + public Builder setProxy(@NonNull Proxy proxy) { + this.proxy = proxy; + return this; + } + + public Builder setGroupKey(@NonNull String groupKey) { + this.groupKey = groupKey; + return this; + } + + public Builder setGroupMasterSecret(@NonNull String groupMasterSecret) { + this.groupMasterSecret = groupMasterSecret; + return this; + } + + public Builder setLoggerLevel(@NonNull Logger.Level loggerLevel) { + this.loggerLevel = loggerLevel; + return this; + } + + public GroupPushApi build() { + okhttp3.OkHttpClient.Builder delegateBuilder = new okhttp3.OkHttpClient().newBuilder(); + if (proxy != null) { + delegateBuilder.proxy(proxy); + } + GroupPushClient groupPushClient = Feign.builder() + .client(new OkHttpClient(delegateBuilder.build())) + .requestInterceptor(new BasicAuthRequestInterceptor("group-" + groupKey, groupMasterSecret)) + .encoder(new ApiEncoder()) + .decoder(new ApiDecoder()) + .errorDecoder(new ApiErrorDecoder()) + .logger(new Slf4jLogger()) + .logLevel(loggerLevel) + .target(GroupPushClient.class, host); + return new GroupPushApi(groupPushClient); + } + } + +} diff --git a/jiguang-sdk/src/main/java/cn/jiguang/sdk/bean/push/GroupPushSendParam.java b/jiguang-sdk/src/main/java/cn/jiguang/sdk/bean/push/GroupPushSendParam.java new file mode 100644 index 0000000..2b00c72 --- /dev/null +++ b/jiguang-sdk/src/main/java/cn/jiguang/sdk/bean/push/GroupPushSendParam.java @@ -0,0 +1,8 @@ +package cn.jiguang.sdk.bean.push; + +import lombok.Data; + +@Data +public class GroupPushSendParam extends PushSendParam{ + +} diff --git a/jiguang-sdk/src/main/java/cn/jiguang/sdk/bean/push/GroupPushSendResult.java b/jiguang-sdk/src/main/java/cn/jiguang/sdk/bean/push/GroupPushSendResult.java new file mode 100644 index 0000000..4dac85d --- /dev/null +++ b/jiguang-sdk/src/main/java/cn/jiguang/sdk/bean/push/GroupPushSendResult.java @@ -0,0 +1,42 @@ +package cn.jiguang.sdk.bean.push; + +import cn.jiguang.sdk.exception.ApiErrorException; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.JsonNode; +import lombok.Data; + +import java.util.HashMap; +import java.util.Map; + +@Data +public class GroupPushSendResult { + + @JsonInclude(JsonInclude.Include.NON_NULL) + private Map errors = new HashMap<>(); + + @JsonInclude(JsonInclude.Include.NON_NULL) + private Map successes = new HashMap<>(); + + @JsonProperty("group_msgid") + private String groupMessageId; + + @JsonAnySetter + public void handleUnknown(String key, JsonNode value) { + if (key.equals("group_msgid")) { + setGroupMessageId(value.asText()); + } else if (value.has("error")) { + ApiErrorException.ApiError.Error errorDetail = new ApiErrorException.ApiError.Error(); + errorDetail.setCode(value.get("error").get("code").asInt()); + errorDetail.setMessage(value.get("error").get("message").asText()); + errors.put(key, errorDetail); + } else if (value.has("msg_id")) { + PushSendResult successDetail = new PushSendResult(); + successDetail.setMessageId(value.get("msg_id").asText()); + successDetail.setSendNo(value.get("sendno").asText()); + successes.put(key, successDetail); + } + } + +} diff --git a/jiguang-sdk/src/main/java/cn/jiguang/sdk/client/GroupPushClient.java b/jiguang-sdk/src/main/java/cn/jiguang/sdk/client/GroupPushClient.java new file mode 100644 index 0000000..4f4e4df --- /dev/null +++ b/jiguang-sdk/src/main/java/cn/jiguang/sdk/client/GroupPushClient.java @@ -0,0 +1,17 @@ +package cn.jiguang.sdk.client; + +import cn.jiguang.sdk.bean.push.GroupPushSendParam; +import cn.jiguang.sdk.bean.push.GroupPushSendResult; +import feign.Headers; +import feign.RequestLine; + +/** + * (REST API - GroupPush) + */ +public interface GroupPushClient { + + @RequestLine("POST /v3/grouppush") + @Headers("Content-Type: application/json; charset=utf-8") + GroupPushSendResult send(GroupPushSendParam param); + +} diff --git a/jiguang-sdk/src/main/java/cn/jiguang/sdk/codec/ApiDecoder.java b/jiguang-sdk/src/main/java/cn/jiguang/sdk/codec/ApiDecoder.java index 59d9546..6bd7f3c 100644 --- a/jiguang-sdk/src/main/java/cn/jiguang/sdk/codec/ApiDecoder.java +++ b/jiguang-sdk/src/main/java/cn/jiguang/sdk/codec/ApiDecoder.java @@ -27,10 +27,10 @@ public Object decode(Response response, Type type) throws IOException, DecodeExc if (contentType == null) { return null; } - if (contentType.startsWith("application/json")) { - return jacksonDecoder.decode(response, type); + if (!contentType.startsWith("application/json")) { + return null; } - return null; + return jacksonDecoder.decode(response, type); } } diff --git a/jiguang-sdk/src/main/java/cn/jiguang/sdk/codec/ApiEncoder.java b/jiguang-sdk/src/main/java/cn/jiguang/sdk/codec/ApiEncoder.java index 57c8c9c..6582eba 100644 --- a/jiguang-sdk/src/main/java/cn/jiguang/sdk/codec/ApiEncoder.java +++ b/jiguang-sdk/src/main/java/cn/jiguang/sdk/codec/ApiEncoder.java @@ -28,13 +28,14 @@ public void encode(Object object, Type bodyType, RequestTemplate template) throw if (contentType == null) { return; } + if (contentType.startsWith("application/json")) { + jacksonEncoder.encode(object, bodyType, template); + return; + } if (contentType.startsWith("multipart/form-data")) { formEncoder.encode(object, bodyType, template); return; } - if (contentType.startsWith("application/json")) { - jacksonEncoder.encode(object, bodyType, template); - } } } diff --git a/pom.xml b/pom.xml index edac332..fe125c1 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.jpush jiguang-sdk-java - 5.1.2 + 5.1.3 pom Jiguang SDK For Rest Api