Skip to content

Commit

Permalink
support group push, update version to 5.1.3.
Browse files Browse the repository at this point in the history
  • Loading branch information
wicked-tc130 committed Aug 1, 2024
1 parent 2e7ff3b commit c3edecb
Show file tree
Hide file tree
Showing 14 changed files with 279 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
## 1. 集成
引入sdk包
```xml
<!--以5.1.2版本为例-->
<!--以5.1.3版本为例-->
<dependencies>
<!-- jiguang-sdk -->
<dependency>
<groupId>io.github.jpush</groupId>
<artifactId>jiguang-sdk</artifactId>
<version>5.1.2</version>
<version>5.1.3</version>
</dependency>
</dependencies>
```
Expand Down
4 changes: 2 additions & 2 deletions example-for-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<groupId>io.github.jpush</groupId>
<artifactId>example-for-spring</artifactId>
<version>5.1.2</version>
<version>5.1.3</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand All @@ -26,7 +26,7 @@
<dependency>
<groupId>io.github.jpush</groupId>
<artifactId>jiguang-sdk</artifactId>
<version>5.1.2</version>
<version>5.1.3</version>
</dependency>
<!-- lombok -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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();
}

}
2 changes: 2 additions & 0 deletions example-for-spring/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ jiguang:
master-secret: bcf3b3327000abce4764f862
dev-key: c2dc75e97486529205528b23
dev-secret: d219e2f001df2fe4f08b6754
group-key: 2ed1465b94aab3f03f6778e0
group-master-secret: 5b69dce5f9dc0dabe70aa33a


Original file line number Diff line number Diff line change
@@ -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<String, String> iOSAlert = new HashMap<>();
iOSAlert.put("title", "this is iOS title");
iOSAlert.put("subtitle", "this is iOS subtitle");
iOS.setAlert(iOSAlert);

Map<String, Object> extrasMap = new HashMap<>();
Map<String, Object> 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<String, Object> thirdPartyMap = new HashMap<>();
Map<String, Object> 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);
}

}
2 changes: 2 additions & 0 deletions example-for-spring/target/classes/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ jiguang:
master-secret: bcf3b3327000abce4764f862
dev-key: c2dc75e97486529205528b23
dev-secret: d219e2f001df2fe4f08b6754
group-key: 1ed1465b94aab3f03f6778e0
group-master-secret: 4b69dce5f9dc0dabe70aa33a


4 changes: 2 additions & 2 deletions jiguang-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<parent>
<groupId>io.github.jpush</groupId>
<artifactId>jiguang-sdk-java</artifactId>
<version>5.1.2</version>
<version>5.1.3</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>io.github.jpush</groupId>
<artifactId>jiguang-sdk</artifactId>
<version>5.1.2</version>
<version>5.1.3</version>
<packaging>jar</packaging>

<properties>
Expand Down
81 changes: 81 additions & 0 deletions jiguang-sdk/src/main/java/cn/jiguang/sdk/api/GroupPushApi.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cn.jiguang.sdk.bean.push;

import lombok.Data;

@Data
public class GroupPushSendParam extends PushSendParam{

}
Original file line number Diff line number Diff line change
@@ -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<String, ApiErrorException.ApiError.Error> errors = new HashMap<>();

@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, PushSendResult> 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);
}
}

}
Original file line number Diff line number Diff line change
@@ -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;

/**
* (<a href="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push_grouppush">REST API - GroupPush</a>)
*/
public interface GroupPushClient {

@RequestLine("POST /v3/grouppush")
@Headers("Content-Type: application/json; charset=utf-8")
GroupPushSendResult send(GroupPushSendParam param);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.jpush</groupId>
<artifactId>jiguang-sdk-java</artifactId>
<version>5.1.2</version>
<version>5.1.3</version>
<packaging>pom</packaging>

<name>Jiguang SDK For Rest Api</name>
Expand Down

0 comments on commit c3edecb

Please sign in to comment.