|
@@ -0,0 +1,207 @@
|
|
1
|
+package com.unissoft.service.impl;
|
|
2
|
+
|
|
3
|
+import cn.jiguang.common.ClientConfig;
|
|
4
|
+import cn.jiguang.common.ServiceHelper;
|
|
5
|
+import cn.jiguang.common.connection.NettyHttpClient;
|
|
6
|
+import cn.jiguang.common.resp.APIConnectionException;
|
|
7
|
+import cn.jiguang.common.resp.APIRequestException;
|
|
8
|
+import cn.jiguang.common.resp.ResponseWrapper;
|
|
9
|
+import cn.jpush.api.JPushClient;
|
|
10
|
+import cn.jpush.api.push.PushResult;
|
|
11
|
+import cn.jpush.api.push.model.Message;
|
|
12
|
+import cn.jpush.api.push.model.Options;
|
|
13
|
+import cn.jpush.api.push.model.Platform;
|
|
14
|
+import cn.jpush.api.push.model.PushPayload;
|
|
15
|
+import cn.jpush.api.push.model.audience.Audience;
|
|
16
|
+import cn.jpush.api.push.model.notification.AndroidNotification;
|
|
17
|
+import cn.jpush.api.push.model.notification.IosNotification;
|
|
18
|
+import cn.jpush.api.push.model.notification.Notification;
|
|
19
|
+import cn.jpush.api.report.ReceivedsResult;
|
|
20
|
+import com.unissoft.config.JpushConfig;
|
|
21
|
+import com.unissoft.service.JpushService;
|
|
22
|
+import io.netty.handler.codec.http.HttpMethod;
|
|
23
|
+import lombok.extern.slf4j.Slf4j;
|
|
24
|
+import org.springframework.stereotype.Service;
|
|
25
|
+
|
|
26
|
+import javax.annotation.Resource;
|
|
27
|
+import java.net.URI;
|
|
28
|
+import java.net.URISyntaxException;
|
|
29
|
+import java.util.HashMap;
|
|
30
|
+import java.util.List;
|
|
31
|
+import java.util.Map;
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+@Service
|
|
35
|
+@Slf4j
|
|
36
|
+public class JpushServiceImpl implements JpushService {
|
|
37
|
+
|
|
38
|
+ @Resource
|
|
39
|
+ JpushConfig jpushConfig;// 注入配置信息
|
|
40
|
+
|
|
41
|
+ /**
|
|
42
|
+ * 推送自定义消息,不创建通知栏提醒,由APP端拦截信息后再决定是否创建通知
|
|
43
|
+ *
|
|
44
|
+ * @param title App通知栏标题
|
|
45
|
+ * @param content App通知栏内容(为了单行显示全,尽量保持在22个汉字以下)
|
|
46
|
+ * @param extras 额外推送信息(不会显示在通知栏,传递数据用)
|
|
47
|
+ * @param registrationIds 手机注册ID,设定哪些用户手机能接收信息
|
|
48
|
+ */
|
|
49
|
+ @Override
|
|
50
|
+ public PushResult sendMessagePush(String title, String content, Map<String, String> extras, List<String> registrationIds) {
|
|
51
|
+ ClientConfig clientConfig = getClientConfig();
|
|
52
|
+
|
|
53
|
+ // 使用NativeHttpClient网络客户端,连接网络的方式,不提供回调函数
|
|
54
|
+ JPushClient jpushClient = new JPushClient(jpushConfig.getMasterSecret(), jpushConfig.getAppkey(), null, clientConfig);
|
|
55
|
+ //
|
|
56
|
+ PushPayload payload = buildMessagePushPayload(title, content, extras, registrationIds);
|
|
57
|
+ PushResult result = null;
|
|
58
|
+ try {
|
|
59
|
+ result = jpushClient.sendPush(payload);
|
|
60
|
+ log.info("极光推送结果 - " + result+",接收推送的注册ID列表:" + String.join(",", registrationIds));
|
|
61
|
+ } catch (APIConnectionException e) {
|
|
62
|
+ log.error("极光推送连接错误,请稍后重试 ", e);
|
|
63
|
+ log.error("Sendno: " + payload.getSendno());
|
|
64
|
+ } catch (APIRequestException e) {
|
|
65
|
+ log.error("极光服务器响应出错,请修复! ", e);
|
|
66
|
+ log.info("HTTP Status: " + e.getStatus());
|
|
67
|
+ log.info("Error Code: " + e.getErrorCode());
|
|
68
|
+ log.info("Error Message: " + e.getErrorMessage());
|
|
69
|
+ log.info("Msg ID: " + e.getMsgId());
|
|
70
|
+ log.info("以下存在不能识别的注册ID: " + String.join(",", registrationIds));
|
|
71
|
+ log.error("Sendno: " + payload.getSendno());
|
|
72
|
+ }
|
|
73
|
+ return result;
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ /**
|
|
77
|
+ * 原生方式推送
|
|
78
|
+ *
|
|
79
|
+ * @param title App通知栏标题
|
|
80
|
+ * @param content App通知栏内容(为了单行显示全,尽量保持在22个汉字以下)
|
|
81
|
+ * @param extras 额外推送信息(不会显示在通知栏,传递数据用)
|
|
82
|
+ * @param registrationIds 手机注册ID,设定哪些用户手机能接收通知
|
|
83
|
+ *
|
|
84
|
+ */
|
|
85
|
+ @Override
|
|
86
|
+ public PushResult sendNotificationPush(String title, String content, Map<String, String> extras, List<String> registrationIds) {
|
|
87
|
+ ClientConfig clientConfig = getClientConfig();
|
|
88
|
+
|
|
89
|
+ // 使用NativeHttpClient网络客户端,连接网络的方式,不提供回调函数
|
|
90
|
+ JPushClient jpushClient = new JPushClient(jpushConfig.getMasterSecret(), jpushConfig.getAppkey(), null, clientConfig);
|
|
91
|
+ // 设置推送方式
|
|
92
|
+ PushPayload payload = buildNotificationPushPayload(title, content, extras, registrationIds);
|
|
93
|
+ PushResult result = null;
|
|
94
|
+ try {
|
|
95
|
+ result = jpushClient.sendPush(payload);
|
|
96
|
+ log.info("极光推送结果 - " + result);
|
|
97
|
+ } catch (APIConnectionException e) {
|
|
98
|
+ log.error("极光推送连接错误,请稍后重试 ", e);
|
|
99
|
+ log.error("Sendno: " + payload.getSendno());
|
|
100
|
+ } catch (APIRequestException e) {
|
|
101
|
+ log.error("极光服务器响应出错,请修复! ", e);
|
|
102
|
+ log.info("HTTP Status: " + e.getStatus());
|
|
103
|
+ log.info("Error Code: " + e.getErrorCode());
|
|
104
|
+ log.info("Error Message: " + e.getErrorMessage());
|
|
105
|
+ log.info("Msg ID: " + e.getMsgId());
|
|
106
|
+ log.info("以下存在不能识别的注册ID: " + String.join(",", registrationIds));
|
|
107
|
+ log.error("Sendno: " + payload.getSendno());
|
|
108
|
+ }
|
|
109
|
+ return result;
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ /**
|
|
113
|
+ * 异步请求推送方式,有回调
|
|
114
|
+ *
|
|
115
|
+ * @param title 通知栏标题
|
|
116
|
+ * @param content 通知栏内容(为了单行显示全,尽量保持在22个汉字以下)
|
|
117
|
+ * @param extras 额外推送信息(不会显示在通知栏,传递数据用)
|
|
118
|
+ * @param registrationIds 手机注册ID,设定哪些用户手机能接收通知
|
|
119
|
+ *
|
|
120
|
+ * @see "使用NettyHttpClient,异步接口发送请求",通过回调函数可以获取推送成功与否情况
|
|
121
|
+ */
|
|
122
|
+ @Override
|
|
123
|
+ public void sendNotificationPushWithCallback(String title, String content, Map<String, String> extras, List<String> registrationIds) {
|
|
124
|
+ ClientConfig clientConfig = getClientConfig();
|
|
125
|
+ String host = (String) clientConfig.get(ClientConfig.PUSH_HOST_NAME);
|
|
126
|
+ final NettyHttpClient client = new NettyHttpClient(
|
|
127
|
+ ServiceHelper.getBasicAuthorization(jpushConfig.getAppkey(), jpushConfig.getMasterSecret()), null,
|
|
128
|
+ clientConfig);
|
|
129
|
+ try {
|
|
130
|
+ URI uri = new URI(host + clientConfig.get(ClientConfig.PUSH_PATH));
|
|
131
|
+ PushPayload payload = buildNotificationPushPayload(title, content, extras, registrationIds);
|
|
132
|
+ client.sendRequest(HttpMethod.POST, payload.toString(), uri, new NettyHttpClient.BaseCallback() {
|
|
133
|
+ @Override
|
|
134
|
+ public void onSucceed(ResponseWrapper responseWrapper) {
|
|
135
|
+ if (200 == responseWrapper.responseCode) {
|
|
136
|
+ log.info("极光推送成功");
|
|
137
|
+ } else {
|
|
138
|
+ log.info("极光推送失败,返回结果: " + responseWrapper.responseContent);
|
|
139
|
+ }
|
|
140
|
+ }
|
|
141
|
+ });
|
|
142
|
+ } catch (URISyntaxException e) {
|
|
143
|
+ log.error(e.getMessage());
|
|
144
|
+ } finally {
|
|
145
|
+ // 需要手动关闭Netty请求进程,否则会一直保留
|
|
146
|
+ client.close();
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ private ClientConfig getClientConfig() {
|
|
152
|
+ ClientConfig clientConfig = ClientConfig.getInstance();
|
|
153
|
+ clientConfig.setTimeToLive(Long.valueOf(jpushConfig.getLiveTime()));
|
|
154
|
+ return clientConfig;
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+ /**
|
|
159
|
+ * 构建Android和IOS的推送通知对象
|
|
160
|
+ */
|
|
161
|
+ private PushPayload buildNotificationPushPayload(String title, String content, Map<String, String> extras, List<String> registrationIds) {
|
|
162
|
+ if (extras == null || extras.isEmpty()) {
|
|
163
|
+ extras = new HashMap<>();
|
|
164
|
+ }
|
|
165
|
+ return PushPayload.newBuilder().setPlatform(Platform.android_ios())
|
|
166
|
+ .setAudience(Audience.registrationId(registrationIds))
|
|
167
|
+ .setNotification(Notification.newBuilder().setAlert(content)
|
|
168
|
+ .addPlatformNotification(AndroidNotification.newBuilder().setTitle(title).addExtras(extras).build())
|
|
169
|
+ .addPlatformNotification(IosNotification.newBuilder().incrBadge(1).addExtras(extras).build())
|
|
170
|
+ .build())
|
|
171
|
+ .setOptions(Options.newBuilder().setApnsProduction(true).build())
|
|
172
|
+ .build();
|
|
173
|
+ }
|
|
174
|
+
|
|
175
|
+ /**
|
|
176
|
+ * 构建Android和IOS的自定义消息的推送消息对象
|
|
177
|
+ */
|
|
178
|
+ private PushPayload buildMessagePushPayload(String title, String content, Map<String, String> extras, List<String> registrationIds) {
|
|
179
|
+ return PushPayload.newBuilder().setPlatform(Platform.android_ios())
|
|
180
|
+ .setAudience(Audience.registrationId(registrationIds))
|
|
181
|
+ .setMessage(Message.newBuilder().setTitle(title).setMsgContent(content).addExtras(extras).build())
|
|
182
|
+ .setOptions(Options.newBuilder().setApnsProduction(true).build())
|
|
183
|
+ .build();
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+ /**
|
|
187
|
+ * 查询记录推送成功条数(暂未使用)
|
|
188
|
+ * @param msgId 在推送返回结果PushResult中保存
|
|
189
|
+ */
|
|
190
|
+ public void countPush(String msgId) {
|
|
191
|
+ JPushClient jpushClient = new JPushClient(jpushConfig.getMasterSecret(), jpushConfig.getAppkey());
|
|
192
|
+ try {
|
|
193
|
+ ReceivedsResult result = jpushClient.getReportReceiveds(msgId);
|
|
194
|
+ ReceivedsResult.Received received = result.received_list.get(0);
|
|
195
|
+ log.debug("Android接受信息:" + received.android_received + "\n IOS端接受信息:" + received.ios_apns_sent);
|
|
196
|
+ log.debug("极光推送返回结果 - " + result);
|
|
197
|
+ } catch (APIConnectionException e) {
|
|
198
|
+ log.error("极光推送连接错误,请稍后重试", e);
|
|
199
|
+ } catch (APIRequestException e) {
|
|
200
|
+ log.error("检查错误,并修复推送请求", e);
|
|
201
|
+ log.info("HTTP Status: " + e.getStatus());
|
|
202
|
+ log.info("Error Code: " + e.getErrorCode());
|
|
203
|
+ log.info("Error Message: " + e.getErrorMessage());
|
|
204
|
+ }
|
|
205
|
+ }
|
|
206
|
+
|
|
207
|
+}
|