86183
2022-09-09 0d999e33085c0a25c5525242748f6aa62a401159
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
package com.dsh.utils;
 
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.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.util.List;
 
/**
 * @author 张鹏
 * @date 2020/09/02 15:20
 * @description  极光推送工具
 */
@Component
public class JPushClientUtils {
 
    private static final Logger logger = LoggerFactory.getLogger(JPushClientUtils.class);
 
    /**
     * 极光推送 api key
     */
    private static String appKey;
    /**
     * 极光推送 masterSecret
     */
    private static String masterSecret;
 
    @Value("${jPush.appKey}")
    public void setAppKey(String appKey) {
        JPushClientUtils.appKey = appKey;
    }
 
    @Value("${jPush.masterSecret}")
    public void setMasterSecret(String masterSecret) {
        JPushClientUtils.masterSecret = masterSecret;
    }
 
    private static JPushClient jPushClient;
 
 
    /**
     * 根据设备id推送
     * @param regeSterIds 设备号
     * @param msgContent 内容
     * @return
     */
    public static boolean senPushByRegesterId(List<String> regeSterIds, String msgContent) {
        jPushClient = new JPushClient(masterSecret, appKey);
        boolean flag = false;
        try {
            PushPayload payload = JPushClientUtils.buildPushObject_all_all_regesterIds(regeSterIds, msgContent);
            System.out.println("处理后的结果:" + payload);
            PushResult result = jPushClient.sendPush(payload);
            System.out.println("结果:" + result);
            if (result.getResponseCode() == 200) {
                logger.info("Get result ----" + result);
                flag = true;
            }
        } catch (APIConnectionException e) {
            logger.error("Connection error. Should retry later. ", e);
            flag = false;
        } catch (APIRequestException e) {
            logger.error("Error response from JPush server. Should review and fix it. ", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
            logger.info("Msg ID: " + e.getMsgId());
            flag = false;
        }
        return flag;
    }
 
 
    public static PushPayload buildPushObject_all_all_regesterIds(List<String> regesterIds, String content) {
        System.out.println("----------------开始处理推送消息----------------");
        return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.registrationId(regesterIds))
                .setNotification(Notification.alert(content)).build();
 
    }
}