|
@@ -13,7 +13,6 @@ import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
-import java.util.function.Consumer;
|
|
|
import java.util.stream.Collectors;
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.crypto.Cipher;
|
|
@@ -85,14 +84,16 @@ public class DefaultJPushHandler implements JPushHandler {
|
|
|
* 获取历史消息地址
|
|
|
*/
|
|
|
private static final String GET_HISTORY_MESSAGE_URL = "https://report.im.jpush.cn/v2/users/%s/messages?cursor=%s";
|
|
|
+
|
|
|
private final RestTemplate restTemplate;
|
|
|
private final JPushProperties properties;
|
|
|
- @Value("${spring.profiles.active}")
|
|
|
- private String env;
|
|
|
private PrivateKey privateKey;
|
|
|
private JPushClient pushClient;
|
|
|
private JMessageClient messageClient;
|
|
|
|
|
|
+ @Value("${spring.profiles.active}")
|
|
|
+ private String env;
|
|
|
+
|
|
|
@PostConstruct
|
|
|
public void initialize() throws GeneralSecurityException {
|
|
|
// 初始化登录认证配置
|
|
@@ -185,7 +186,13 @@ public class DefaultJPushHandler implements JPushHandler {
|
|
|
headers.add("Authorization", ServiceHelper.getBasicAuthorization(appkey, secret));
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
HttpEntity<?> entity = new HttpEntity<>(headers);
|
|
|
- ChatMessage message = this.restTemplate.exchange(url, HttpMethod.GET, entity, ChatMessage.class).getBody();
|
|
|
+ ChatMessage message;
|
|
|
+ try {
|
|
|
+ message = this.restTemplate.exchange(url, HttpMethod.GET, entity, ChatMessage.class).getBody();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("JPush get chat message failed: {}", e.getMessage(), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
if (debug) {
|
|
|
log.debug("JPush response: {}", message);
|
|
|
}
|
|
@@ -207,14 +214,21 @@ public class DefaultJPushHandler implements JPushHandler {
|
|
|
headers.add("Authorization", authorization);
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
HttpEntity<?> entity = new HttpEntity<>(parameters, headers);
|
|
|
- JPushMobile response =
|
|
|
- this.restTemplate.exchange(TOKEN2MOBILE_URL, HttpMethod.POST, entity, JPushMobile.class).getBody();
|
|
|
+ JPushMobile response;
|
|
|
+ try {
|
|
|
+ response = this.restTemplate.exchange(
|
|
|
+ TOKEN2MOBILE_URL, HttpMethod.POST, entity, JPushMobile.class
|
|
|
+ ).getBody();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("JPush get mobile failed: {}", e.getMessage(), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
if (debug) {
|
|
|
log.debug("JPush response: {}", response);
|
|
|
}
|
|
|
- Objects.requireNonNull(response, "Invalid jpush server response");
|
|
|
- if (response.getCode() == null || response.getCode() != 8000) {
|
|
|
- throw new RuntimeException(String.format("[%d] %s", response.getCode(), response.getContent()));
|
|
|
+ if (response == null || response.getCode() == null || response.getCode() != 8000) {
|
|
|
+ log.warn("JPush get mobile failed: {}", response);
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
// 解码手机号
|
|
@@ -228,62 +242,39 @@ public class DefaultJPushHandler implements JPushHandler {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void register(@NonNull RegisterInfo... registrations) {
|
|
|
- if (registrations.length > 0) {
|
|
|
- boolean debug = log.isDebugEnabled();
|
|
|
- if (debug) {
|
|
|
- log.debug("JPush request: {}", Arrays.toString(registrations));
|
|
|
- }
|
|
|
- String result;
|
|
|
- try {
|
|
|
- result = this.getMessageClient().registerUsers(registrations);
|
|
|
- } catch (APIConnectionException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- } catch (APIRequestException e) {
|
|
|
- throw new RuntimeException(e.getErrorMessage(), e);
|
|
|
- }
|
|
|
- if (debug) {
|
|
|
- log.debug("JPush response: {}", result);
|
|
|
- }
|
|
|
+ public boolean register(@NonNull RegisterInfo... registrations) {
|
|
|
+ if (registrations.length == 0) {
|
|
|
+ return false;
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void register(@NonNull Collection<RegisterInfo> registrations) {
|
|
|
- if (!registrations.isEmpty()) {
|
|
|
- this.register(registrations.toArray(new RegisterInfo[0]));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<String> getPushIdentities(int count) {
|
|
|
boolean debug = log.isDebugEnabled();
|
|
|
if (debug) {
|
|
|
- log.debug("JPush request: {}", count);
|
|
|
+ log.debug("JPush request: {}", Arrays.toString(registrations));
|
|
|
}
|
|
|
- List<String> identities;
|
|
|
+ String result;
|
|
|
try {
|
|
|
- identities = this.getPushClient().getCidList(count, "push").cidlist;
|
|
|
- } catch (APIConnectionException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- } catch (APIRequestException e) {
|
|
|
- throw new RuntimeException(e.getErrorMessage(), e);
|
|
|
+ result = this.getMessageClient().registerUsers(registrations);
|
|
|
+ } catch (Exception e) {
|
|
|
+ int code;
|
|
|
+ if (e instanceof APIRequestException
|
|
|
+ && ((code = ((APIRequestException) e).getErrorCode()) == 1011 || code == 899001)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ log.warn("JPush register failed: {}", e.getMessage(), e);
|
|
|
+ return false;
|
|
|
}
|
|
|
if (debug) {
|
|
|
- log.debug("JPush response: {}", identities);
|
|
|
+ log.debug("JPush response: {}", result);
|
|
|
}
|
|
|
- return identities;
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public long push(@NonNull PushPayload payload) {
|
|
|
- return this.push(payload, e -> {
|
|
|
- throw new RuntimeException(e.getErrorMessage(), e);
|
|
|
- });
|
|
|
+ public boolean register(@NonNull Collection<RegisterInfo> registrations) {
|
|
|
+ return !registrations.isEmpty() && this.register(registrations.toArray(new RegisterInfo[0]));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public long push(@NonNull PushPayload payload, @NonNull Consumer<APIRequestException> failure) {
|
|
|
+ public long push(@NonNull PushPayload payload) {
|
|
|
// 针对生产环境IOS推送,需要设置apnsProduction为true
|
|
|
if (this.isProduction()) {
|
|
|
payload.resetOptionsApnsProduction(true);
|
|
@@ -292,23 +283,22 @@ public class DefaultJPushHandler implements JPushHandler {
|
|
|
if (debug) {
|
|
|
log.debug("JPush request: {}", payload);
|
|
|
}
|
|
|
- JPushClient client = this.getPushClient();
|
|
|
+ PushResult result;
|
|
|
try {
|
|
|
- PushResult result = client.sendPush(payload);
|
|
|
- if (debug) {
|
|
|
- log.debug("JPush response: {}", result);
|
|
|
- }
|
|
|
- if (result != null && result.statusCode == 0) {
|
|
|
- return result.msg_id;
|
|
|
- }
|
|
|
- Integer code = result == null || result.error == null ? null : result.error.getCode();
|
|
|
- String message = result == null || result.error == null ? null : result.error.getMessage();
|
|
|
- throw new RuntimeException(String.format("[%d] %s", code, message));
|
|
|
- } catch (APIConnectionException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- } catch (APIRequestException e) {
|
|
|
- failure.accept(e);
|
|
|
+ result = this.getPushClient().sendPush(payload);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("JPush push failed: {}", e.getMessage(), e);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (debug) {
|
|
|
+ log.debug("JPush response: {}", result);
|
|
|
+ }
|
|
|
+ if (result != null && result.statusCode == 0) {
|
|
|
+ return result.msg_id;
|
|
|
}
|
|
|
+ Integer code = result == null || result.error == null ? null : result.error.getCode();
|
|
|
+ String message = result == null || result.error == null ? null : result.error.getMessage();
|
|
|
+ log.warn("JPush push failed: [{}] {}", code, message);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
@@ -379,13 +369,11 @@ public class DefaultJPushHandler implements JPushHandler {
|
|
|
|
|
|
// 批量推送消息
|
|
|
BatchPushResult result;
|
|
|
- JPushClient client = this.getPushClient();
|
|
|
try {
|
|
|
- result = client.batchSendPushByAlias(payloads);
|
|
|
- } catch (APIConnectionException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- } catch (APIRequestException e) {
|
|
|
- throw new RuntimeException(e.getErrorMessage(), e);
|
|
|
+ result = this.getPushClient().batchSendPushByAlias(payloads);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("JPush push failed: {}", e.getMessage(), e);
|
|
|
+ return null;
|
|
|
}
|
|
|
if (debug) {
|
|
|
log.debug("JPush response: {}", JacksonContextHolder.serialize(result));
|
|
@@ -408,13 +396,11 @@ public class DefaultJPushHandler implements JPushHandler {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
ReceivedsResult result;
|
|
|
- JPushClient client = this.getPushClient();
|
|
|
try {
|
|
|
- result = client.getReceivedsDetail(ids);
|
|
|
- } catch (APIConnectionException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- } catch (APIRequestException e) {
|
|
|
- throw new RuntimeException(e.getErrorMessage(), e);
|
|
|
+ result = this.getPushClient().getReceivedsDetail(ids);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("JPush get deliveries failed: {}", e.getMessage(), e);
|
|
|
+ return Collections.emptyList();
|
|
|
}
|
|
|
if (debug) {
|
|
|
log.debug("JPush response: {}", JacksonContextHolder.serialize(result));
|
|
@@ -449,10 +435,9 @@ public class DefaultJPushHandler implements JPushHandler {
|
|
|
UserInfoResult user;
|
|
|
try {
|
|
|
user = this.getMessageClient().getUserInfo(username);
|
|
|
- } catch (APIConnectionException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- } catch (APIRequestException e) {
|
|
|
- throw new RuntimeException(e.getErrorMessage(), e);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("JPush get user info failed: {}", e.getMessage(), e);
|
|
|
+ return null;
|
|
|
}
|
|
|
if (debug) {
|
|
|
log.debug("JPush response: {}", user);
|
|
@@ -469,10 +454,9 @@ public class DefaultJPushHandler implements JPushHandler {
|
|
|
UserStateResult state;
|
|
|
try {
|
|
|
state = this.getMessageClient().getUserState(username);
|
|
|
- } catch (APIConnectionException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- } catch (APIRequestException e) {
|
|
|
- throw new RuntimeException(e.getErrorMessage(), e);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("JPush get user state failed: {}", e.getMessage(), e);
|
|
|
+ return null;
|
|
|
}
|
|
|
if (debug) {
|
|
|
log.debug("JPush response: {}", state);
|
|
@@ -492,10 +476,9 @@ public class DefaultJPushHandler implements JPushHandler {
|
|
|
UserStateListResult[] states;
|
|
|
try {
|
|
|
states = this.getMessageClient().getUsersState(usernames);
|
|
|
- } catch (APIConnectionException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- } catch (APIRequestException e) {
|
|
|
- throw new RuntimeException(e.getErrorMessage(), e);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("JPush get user state failed: {}", e.getMessage(), e);
|
|
|
+ return new UserStateListResult[0];
|
|
|
}
|
|
|
if (debug) {
|
|
|
log.debug("JPush response: {}", Arrays.toString(states));
|