“mitao”
2026-05-01 74232f3f2a69a99c40a1e54854f0ae419a73a3bc
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
90
91
92
93
package com.tcm.training.admin.utils;
 
import cn.jiguang.common.ClientConfig;
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.Map;
 
/**
 * 极光推送工具类
 */
@Component
public class JGPushUtil {
 
    private static final Logger log = LoggerFactory.getLogger(JGPushUtil.class);
 
    @Value("${jpush.appKey}")
    private String appKey;
 
    @Value("${jpush.masterSecret}")
    private String masterSecret;
 
    /**
     * 构建简单文本推送载荷。
     */
    private PushPayload buildPushObject_all_all_alert(String content, String...tags) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())//设置推送平台为全部平台
                .setAudience(Audience.alias(tags))//通过别名的方式发送(用户owner_id,员工user_id)
                .setNotification(Notification.alert(content))
                .build();
    }
 
    /**
     * 构建带标题和扩展参数的推送载荷。
     */
//TODO ios 鸿蒙配置好了之后,需要将下面的注释解除
    private PushPayload buildPushObject_all_all_alert(String title, String content, Map<String, String> map, String...tags) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())//设置推送平台为全部平台
                .setAudience(Audience.alias(tags))//通过别名的方式发送(user_id)
                .setNotification(Notification.android(content, title, map))
                // .setNotification(Notification.ios(content, map))
                // .setNotification(Notification.winphone(content, map))
                .build();
    }
 
 
    /**
     * 推送
     * @param tags
     * @param content
     */
    public void push(String content, String...tags){
        try {
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
        PushPayload payload = this.buildPushObject_all_all_alert(content, tags);
            PushResult result = jpushClient.sendPush(payload);
            log.info("jpush simple push finished, statusCode={}, aliases={}", result.statusCode, tags);
        } catch (APIConnectionException e) {
            throw new IllegalStateException("极光推送连接失败", e);
        } catch (APIRequestException e) {
            throw new IllegalStateException("极光推送请求失败", e);
        }
    }
 
    /**
     * 推送带业务参数的通知消息。
     */
    public void push(String content, String title, Map<String, String> map, String...tags){
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
        PushPayload payload = this.buildPushObject_all_all_alert(title, content, map, tags);
        try {
            PushResult result = jpushClient.sendPush(payload);
            log.info("jpush biz push finished, statusCode={}, title={}, aliases={}", result.statusCode, title, tags);
        } catch (APIConnectionException e) {
            throw new IllegalStateException("极光推送连接失败", e);
        } catch (APIRequestException e) {
            throw new IllegalStateException("极光推送请求失败", e);
        }
    }
 
}