xuhy
2023-08-11 3c1223bf3eb6df077b48637a7c93a93246a1a8ae
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
package com.stylefeng.guns.modular.system.util;
 
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.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.util.Map;
 
/**
 * 极光推送工具类
 */
@Component
public class JGPushUtil {
 
    @Value("${jiguang.appKey-driver}")
    private String appKeyDriver;
 
    @Value("${jiguang.masterSecret-driver}")
    private String masterSecretDriver;
 
    @Value("${jiguang.appKey-dispatch}")
    private String appKeyDispatch;
 
    @Value("${jiguang.masterSecret-dispatch}")
    private String masterSecretDispatch;
 
 
    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();
    }
 
 
    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))//通过别名的方式发送(用户owner_id,员工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(Integer type, String content, String...tags){
        String masterSecret = (type == 1 ? masterSecretDriver : masterSecretDispatch);
        String appKey = (type == 1 ? appKeyDriver : appKeyDispatch);
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
        PushPayload payload = this.buildPushObject_all_all_alert(content, tags);
        try {
            PushResult result = jpushClient.sendPush(payload);
            System.err.println(result.statusCode);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }
 
 
    public void push(Integer type, String content, String title, Map<String, String> map, String...tags){
        String masterSecret = (type == 1 ? masterSecretDriver : masterSecretDispatch);
        String appKey = (type == 1 ? appKeyDriver : appKeyDispatch);
        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);
            System.err.println(result.statusCode);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }
 
}