Bläddra i källkod

极光推送模块

shengyang000 3 år sedan
förälder
incheckning
24b0a4d6b1

+ 40 - 0
pom.xml

@@ -167,6 +167,46 @@
167 167
             <version>5.4.4</version>
168 168
         </dependency>
169 169
 
170
+        <!-- 极光推送-->
171
+        <dependency>
172
+            <groupId>cn.jpush.api</groupId>
173
+            <artifactId>jpush-client</artifactId>
174
+            <version>3.4.8</version>
175
+        </dependency>
176
+<!--        <dependency>-->
177
+<!--            <groupId>cn.jpush.api</groupId>-->
178
+<!--            <artifactId>jiguang-common</artifactId>-->
179
+<!--            <version>1.1.10</version>-->
180
+<!--        </dependency>-->
181
+<!--        <dependency>-->
182
+<!--            <groupId>io.netty</groupId>-->
183
+<!--            <artifactId>netty-all</artifactId>-->
184
+<!--            <version>4.1.6.Final</version>-->
185
+<!--            <scope>compile</scope>-->
186
+<!--        </dependency>-->
187
+<!--        <dependency>-->
188
+<!--            <groupId>com.google.code.gson</groupId>-->
189
+<!--            <artifactId>gson</artifactId>-->
190
+<!--            <version>2.3</version>-->
191
+<!--        </dependency>-->
192
+<!--        <dependency>-->
193
+<!--            <groupId>org.slf4j</groupId>-->
194
+<!--            <artifactId>slf4j-api</artifactId>-->
195
+<!--            <version>1.7.7</version>-->
196
+<!--        </dependency>-->
197
+
198
+<!--        &lt;!&ndash; For log4j &ndash;&gt;-->
199
+<!--        <dependency>-->
200
+<!--            <groupId>org.slf4j</groupId>-->
201
+<!--            <artifactId>slf4j-log4j12</artifactId>-->
202
+<!--            <version>1.7.7</version>-->
203
+<!--        </dependency>-->
204
+<!--        <dependency>-->
205
+<!--            <groupId>log4j</groupId>-->
206
+<!--            <artifactId>log4j</artifactId>-->
207
+<!--            <version>1.2.17</version>-->
208
+<!--        </dependency>-->
209
+
170 210
     </dependencies>
171 211
 
172 212
     <build>

+ 22 - 0
src/main/java/com/unissoft/config/JpushConfig.java

@@ -0,0 +1,22 @@
1
+package com.unissoft.config;
2
+
3
+import lombok.Getter;
4
+import lombok.Setter;
5
+import org.springframework.beans.factory.annotation.Value;
6
+import org.springframework.stereotype.Component;
7
+
8
+@Setter
9
+@Getter
10
+@Component("jpushConfig")
11
+public class JpushConfig {
12
+
13
+    @Value("${jpush.appKey}")
14
+    private String appkey;
15
+
16
+    @Value("${jpush.masterSecret}")
17
+    private String masterSecret;
18
+
19
+    @Value("${jpush.liveTime}")
20
+    private String liveTime;
21
+
22
+}

+ 35 - 0
src/main/java/com/unissoft/controller/JgPushController.java

@@ -0,0 +1,35 @@
1
+package com.unissoft.controller;
2
+
3
+import com.unissoft.result.ResultView;
4
+import com.unissoft.service.JpushService;
5
+import com.unissoft.vo.JpushVO;
6
+import io.swagger.annotations.ApiOperation;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.web.bind.annotation.PostMapping;
9
+import org.springframework.web.bind.annotation.RequestBody;
10
+import org.springframework.web.bind.annotation.RequestMapping;
11
+import org.springframework.web.bind.annotation.RestController;
12
+
13
+/**
14
+ * <p>
15
+ * 极光推送测试
16
+ * </p>
17
+ *
18
+ * @author root
19
+ * @since 2020-12-10
20
+ */
21
+@RestController
22
+@RequestMapping("/jpush")
23
+public class JgPushController {
24
+
25
+    @Autowired
26
+    private JpushService jpushService;
27
+
28
+    @ApiOperation(value = "根据id查询", notes = "")
29
+    @PostMapping("/authority/jpush")
30
+    public ResultView getById(@RequestBody JpushVO jpushVO) {
31
+        jpushService.sendNotificationPush(jpushVO.getTitle(), jpushVO.getContent(), jpushVO.getExtras(), jpushVO.getRegistrationIds());
32
+        return ResultView.success();
33
+    }
34
+
35
+}

+ 17 - 0
src/main/java/com/unissoft/service/JpushService.java

@@ -0,0 +1,17 @@
1
+package com.unissoft.service;
2
+
3
+
4
+import cn.jpush.api.push.PushResult;
5
+
6
+import java.util.List;
7
+import java.util.Map;
8
+
9
+public interface JpushService {
10
+
11
+    PushResult sendMessagePush(String title, String content, Map<String, String> extras, List<String> registrationIds);
12
+
13
+    PushResult sendNotificationPush(String title, String content, Map<String, String> extras, List<String> registrationIds);
14
+
15
+    void sendNotificationPushWithCallback(String title, String content, Map<String, String> extras, List<String> registrationIds);
16
+
17
+}

+ 207 - 0
src/main/java/com/unissoft/service/impl/JpushServiceImpl.java

@@ -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
+}

+ 5 - 2
src/main/java/com/unissoft/service/impl/QuartzServiceImpl.java

@@ -39,8 +39,11 @@ public class QuartzServiceImpl implements QuartzService {
39 39
         try {
40 40
             scheduler.start();
41 41
             //
42
-            WeeklyCalendar weeklyCalendar = new WeeklyCalendar();
43
-            scheduler.addCalendar("weekly", weeklyCalendar, false, false);
42
+            Calendar weekly = scheduler.getCalendar("weekly");
43
+            if(null == weekly) {
44
+                WeeklyCalendar weeklyCalendar = new WeeklyCalendar();
45
+                scheduler.addCalendar("weekly", weeklyCalendar, false, false);
46
+            }
44 47
         } catch (SchedulerException e) {
45 48
             e.printStackTrace();
46 49
         }

+ 2 - 2
src/main/java/com/unissoft/service/impl/SysEarlyWarningServiceImpl.java

@@ -98,8 +98,8 @@ public class SysEarlyWarningServiceImpl extends ServiceImpl<SysEarlyWarningMappe
98 98
         validateParam(model.getWarningTime(), "预警时间");
99 99
 
100 100
         model.setWarnStatus(true);//新增,默认开启
101
-        model.setCreateBy(userPO.getuId());
102
-        model.setUpdateBy(userPO.getuId());
101
+//        model.setCreateBy(userPO.getuId());
102
+//        model.setUpdateBy(userPO.getuId());
103 103
         //
104 104
         Date date = new Date();
105 105
         model.setCreateTime(date);

+ 19 - 0
src/main/java/com/unissoft/vo/JpushVO.java

@@ -0,0 +1,19 @@
1
+package com.unissoft.vo;
2
+
3
+import lombok.Data;
4
+
5
+import java.util.List;
6
+import java.util.Map;
7
+
8
+@Data
9
+public class JpushVO {
10
+
11
+    private String title;
12
+
13
+    private String content;
14
+
15
+    private Map<String, String> extras;
16
+
17
+    private List<String> registrationIds;
18
+
19
+}

+ 5 - 0
src/main/resources/bootstrap.yml

@@ -26,3 +26,8 @@ spring:
26 26
   devtools:
27 27
     restart:
28 28
       log-condition-evaluation-delta: false
29
+
30
+jpush:
31
+  appKey: 364961d3e7f43b10b3555e51
32
+  masterSecret: 0ce36f05ee8be23b1b84cbd8
33
+  liveTime: 300000