puzhibing
2023-02-15 2811bab657aab4145b65a45a824fb63e93b58e30
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
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;
 
/**
 * 极光推送工具类
 */
@Component
public class JGPushUtil {
 
    @Value("${jiguang.masterSecret}")
    private String masterSecret;
 
    @Value("${jiguang.appKey}")
    private String appKey;
 
 
    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();
    }
 
 
    /**
     * 推送
     * @param tags
     * @param content
     */
    public void push(String content, String...tags){
        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 static void main(String[] args) {
        JGPushUtil jgPushUtil = new JGPushUtil();
        jgPushUtil.push("owner", "消息内容", "owner_id");
    }
}