|
@@ -0,0 +1,91 @@
|
|
|
+package com.fjhx.jpush.code;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.extra.spring.SpringUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fjhx.jpush.entity.jpush.po.JpushInfo;
|
|
|
+import com.fjhx.jpush.service.jpush.JpushInfoService;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.DataOutputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Base64;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class JPushUtils {
|
|
|
+
|
|
|
+
|
|
|
+ private static final JpushInfoService jPushInfoService = SpringUtil.getBean(JpushInfoService.class);
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据用户id列表 向极光推送消息
|
|
|
+ */
|
|
|
+ public static void jPushSendMsg(String msg, List<Long> userIds) {
|
|
|
+ //如果未配置极光推送相关信息直接跳过
|
|
|
+ if (ObjectUtil.isEmpty(JPushConfig.appKey) || ObjectUtil.isEmpty(JPushConfig.masterSecret)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据用户id获取要推送的设备列表
|
|
|
+ List<String> registrationIds = jPushInfoService.listObject(JpushInfo::getRegistrationId, q -> q
|
|
|
+ .eq(JpushInfo::getUserId, userIds)
|
|
|
+ .isNotNull(JpushInfo::getRegistrationId)
|
|
|
+ );
|
|
|
+
|
|
|
+ //如果推送设备id列表为空直接跳过
|
|
|
+ if (ObjectUtil.isEmpty(registrationIds)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //创建消息体
|
|
|
+ JpushMsgEntity.Audience audience = new JpushMsgEntity.Audience(registrationIds);
|
|
|
+ JpushMsgEntity.Notification notification = new JpushMsgEntity.Notification(msg);
|
|
|
+ JpushMsgEntity jpushMsgEntity = new JpushMsgEntity("all", audience, notification);
|
|
|
+
|
|
|
+ //推送消息
|
|
|
+ jPushSend(JSONObject.toJSONString(jpushMsgEntity));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 向极光推送消息
|
|
|
+ */
|
|
|
+ private static void jPushSend(String requestDate) {
|
|
|
+ try {
|
|
|
+ //通过base64编码认证信息
|
|
|
+ String token = JPushConfig.appKey + ":" + JPushConfig.masterSecret;
|
|
|
+ token = Base64.getEncoder().encodeToString(token.getBytes());
|
|
|
+
|
|
|
+ URL url = new URL("https://api.jpush.cn/v3/push");
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+ conn.setConnectTimeout(3000);
|
|
|
+ conn.setReadTimeout(3000);
|
|
|
+ conn.setDoInput(true);
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ // 身份鉴权
|
|
|
+ conn.setRequestProperty("Content-Type", "application/json");
|
|
|
+ conn.setRequestProperty("Authorization", "Basic " + token);
|
|
|
+ // 发送消息
|
|
|
+ DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
|
|
|
+ dos.write(requestDate.getBytes(StandardCharsets.UTF_8));
|
|
|
+ dos.flush();
|
|
|
+ // 根据状态码获取流
|
|
|
+ int responseCode = conn.getResponseCode();
|
|
|
+ System.out.println(responseCode);
|
|
|
+ if (responseCode != 200) {
|
|
|
+ throw new ServiceException(IOUtils.toString(conn.getErrorStream(), StandardCharsets.UTF_8));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ throw new ServiceException("极光消息推送异常:{}" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|