Forráskód Böngészése

[chg] 消息推送账号类型判断

wcz 5 éve
szülő
commit
5f84756415

+ 23 - 17
lift-common/src/main/java/cn.com.ty.lift.common/model/PushMessage.java

@@ -2,7 +2,6 @@ package cn.com.ty.lift.common.model;
 
 import cn.com.ty.lift.common.utils.ValuePool;
 import cn.hutool.core.collection.IterUtil;
-import cn.hutool.core.util.StrUtil;
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.jms.core.JmsMessagingTemplate;
@@ -27,7 +26,7 @@ public class PushMessage implements Serializable {
 
     private String       title;
     private String       content;
-    private List<String> toList;
+    private List<String> toList = new ArrayList<>();
     //the count of try again. Increase 1 for each failure, maximum times 100
     private int          tryCount = 0;
 
@@ -39,6 +38,11 @@ public class PushMessage implements Serializable {
         this.content = content;
     }
 
+    public PushMessage add(String to){
+        this.toList.add(to);
+        return this;
+    }
+
     private static PushMessage create(String title, String content) {
         return new PushMessage(title, content);
     }
@@ -503,10 +507,16 @@ public class PushMessage implements Serializable {
         return create("发布新闻", title);
     }
 
+    /**
+     * 检查推送列表总数是否需要拆分,信鸽推送一次最多1000
+     */
     public boolean needSplit() {
-        return toList != null && toList.size() > ValuePool.PUSH_MAX_SIZE;
+        return Objects.nonNull(toList) && toList.size() > ValuePool.PUSH_MAX_SIZE;
     }
 
+    /**
+     * 拆分推送设备列表
+     */
     public List<ArrayList<String>> doSplit(final int length) {
         List<ArrayList<String>> splits = new ArrayList<>();
         int size = toList.size();
@@ -524,7 +534,7 @@ public class PushMessage implements Serializable {
     }
 
     /**
-     * 同时推送到指定的android和ios设备token
+     * 同时推送到多个设备token
      */
     public boolean sendTokenOnPlatform(JmsMessagingTemplate jmsMessagingTemplate, List<PushUserInfo> pushUserInfos) {
         try {
@@ -534,13 +544,11 @@ public class PushMessage implements Serializable {
             Set<String> toAndroid = new HashSet<>();
             Set<String> toIos = new HashSet<>();
             for (PushUserInfo pushUserInfo : pushUserInfos) {
-                Integer deviceModel = pushUserInfo.getDeviceModel();
-                String deviceFlag = pushUserInfo.getDeviceFlag();
-                if (Objects.nonNull(deviceModel) && StrUtil.isNotBlank(deviceFlag)) {
-                    if (1 == deviceModel) {
-                        toAndroid.add(deviceFlag);
-                    } else if (2 == deviceModel) {
-                        toIos.add(deviceFlag);
+                if (pushUserInfo.available()) {
+                    if (pushUserInfo.isAndroid()) {
+                        toAndroid.add(pushUserInfo.getDeviceFlag());
+                    } else if (pushUserInfo.isIos()) {
+                        toIos.add(pushUserInfo.getDeviceFlag());
                     }
                 }
             }
@@ -568,15 +576,13 @@ public class PushMessage implements Serializable {
             if (Objects.isNull(pushUserInfo)) {
                 return false;
             }
-            Integer deviceModel = pushUserInfo.getDeviceModel();
-            String deviceFlag = pushUserInfo.getDeviceFlag();
-            if(Objects.isNull(deviceModel) || StrUtil.isBlank(deviceFlag)){
+            if(!pushUserInfo.available()){
                 return false;
             }
-            this.setToList(Arrays.asList(deviceFlag));
-            if (1 == deviceModel) {
+            this.add(pushUserInfo.getDeviceFlag());
+            if (pushUserInfo.isAndroid()) {
                 jmsMessagingTemplate.send(ValuePool.PUSH_QUEUE_ANDROID_TOKEN, new GenericMessage<>(this));
-            } else if (2 == deviceModel) {
+            } else if (pushUserInfo.isIos()) {
                 jmsMessagingTemplate.send(ValuePool.PUSH_QUEUE_IOS_TOKEN, new GenericMessage<>(this));
             } else {
                 return false;

+ 14 - 0
lift-common/src/main/java/cn.com.ty.lift.common/model/PushUserInfo.java

@@ -1,7 +1,10 @@
 package cn.com.ty.lift.common.model;
 
+import cn.com.ty.lift.common.utils.ValuePool;
 import lombok.Data;
 
+import java.util.Objects;
+
 /**
  * @author huangyuan
  * @date 2020/4/17
@@ -40,4 +43,15 @@ public class PushUserInfo {
      */
     private String roleName;
 
+    public boolean available(){
+        return Objects.nonNull(this.deviceModel) && Objects.nonNull(this.deviceFlag);
+    }
+
+    public boolean isAndroid(){
+        return ValuePool.nullable(this.getDeviceModel(), 0) == ValuePool.PUSH_DEVICE_MODEL_ANDROID;
+    }
+
+    public boolean isIos(){
+        return ValuePool.nullable(this.getDeviceModel(), 0) == ValuePool.PUSH_DEVICE_MODEL_IOS;
+    }
 }

+ 2 - 0
lift-common/src/main/java/cn.com.ty.lift.common/utils/ValuePool.java

@@ -120,6 +120,8 @@ public interface ValuePool {
     int      PUSH_MAX_SIZE                            = 1000;
     //消息推送失败后重新尝试的最多次数
     int      PUSH_TRY_MAX_TIMES                       = 100;
+    int PUSH_DEVICE_MODEL_ANDROID = 1;
+    int PUSH_DEVICE_MODEL_IOS = 2;
     String   PATTERN_NAME                             = "^[\u4e00-\u9fa5a-zA-Z0-9·.。;&\\s]{1,20}$";
     String   PATTERN_TELEPHONE                        = "^1[345789]\\d{9}|0[1-9](\\d{1,2}\\-?)[1-9]\\d{6,7}$";
     //======================年检相关状态值和判断方法======================================