JPushServiceImpl.java
4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.diligrp.rider.service.impl;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.diligrp.rider.config.JPushProperties;
import com.diligrp.rider.service.JPushService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
@RequiredArgsConstructor
public class JPushServiceImpl implements JPushService {
private final JPushClient jPushClient;
private final JPushProperties properties;
@Override
public void pushToRider(Long riderId, String title, String content, String bizType, Long bizId) {
if (!properties.isEnabled()) {
log.debug("JPush 未启用,跳过推送 riderId={} title={}", riderId, title);
return;
}
if (riderId == null || riderId <= 0) {
log.warn("riderId 无效,跳过推送 riderId={}", riderId);
return;
}
try {
PushPayload payload = buildPayload(
Audience.alias(String.valueOf(riderId)),
title, content, bizType, bizId);
PushResult result = jPushClient.sendPush(payload);
log.info("JPush 推送成功 riderId={} msgId={} sendno={}",
riderId, result.msg_id, result.sendno);
} catch (APIConnectionException e) {
log.error("JPush 网络异常 riderId={}", riderId, e);
} catch (APIRequestException e) {
log.error("JPush 业务异常 riderId={} status={} errorCode={} msg={}",
riderId, e.getStatus(), e.getErrorCode(), e.getErrorMessage());
} catch (Exception e) {
log.error("JPush 推送未知异常 riderId={}", riderId, e);
}
}
@Override
public void pushToCity(Long cityId, String title, String content, String bizType) {
if (!properties.isEnabled()) {
log.debug("JPush 未启用,跳过广播 cityId={} title={}", cityId, title);
return;
}
if (cityId == null || cityId <= 0) {
log.warn("cityId 无效,跳过广播 cityId={}", cityId);
return;
}
try {
PushPayload payload = buildPayload(
Audience.tag("city_" + cityId),
title, content, bizType, 0L);
PushResult result = jPushClient.sendPush(payload);
log.info("JPush 城市广播成功 cityId={} msgId={}", cityId, result.msg_id);
} catch (APIConnectionException e) {
log.error("JPush 城市广播网络异常 cityId={}", cityId, e);
} catch (APIRequestException e) {
log.error("JPush 城市广播业务异常 cityId={} status={} errorCode={} msg={}",
cityId, e.getStatus(), e.getErrorCode(), e.getErrorMessage());
} catch (Exception e) {
log.error("JPush 城市广播未知异常 cityId={}", cityId, e);
}
}
private PushPayload buildPayload(Audience audience, String title, String content,
String bizType, Long bizId) {
AndroidNotification.Builder androidBuilder = AndroidNotification.newBuilder()
.setAlert(content)
.setTitle(title);
IosNotification.Builder iosBuilder = IosNotification.newBuilder()
.setAlert(content)
.incrBadge(1)
.setSound("default");
if (bizType != null && !bizType.isEmpty()) {
androidBuilder.addExtra("bizType", bizType);
iosBuilder.addExtra("bizType", bizType);
}
if (bizId != null) {
androidBuilder.addExtra("bizId", String.valueOf(bizId));
iosBuilder.addExtra("bizId", String.valueOf(bizId));
}
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(audience)
.setNotification(Notification.newBuilder()
.addPlatformNotification(androidBuilder.build())
.addPlatformNotification(iosBuilder.build())
.build())
.setOptions(Options.newBuilder()
.setApnsProduction(properties.isApnsProduction())
.setTimeToLive(properties.getTimeToLive())
.build())
.build();
}
}