puzhibing
2023-10-08 22199bbdda579861736420fe26c2873ab0f5d21c
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.sinata.common.api.jPush;
 
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.PushPayload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * <p>
 * 极光推送
 * </p>
 * @author sl
 * @since 2020/10/6 11:14
 */
public class JpushUtils {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(JpushUtils.class);
 
    private static String APP_KEY_USER = "";
    private static String MASTER_SECRET_USER = "";
 
    public static final String PREFIX = "";
 
    private static JPushClient jPushClientUser = new JPushClient(MASTER_SECRET_USER,APP_KEY_USER);
 
    /**
     * 推送给设备标识参数的用户
     * @param aliasList 别名或别名组
     * @param notification_title 通知内容标题
     * @param msg_title 消息内容标题
     * @param msg_content 消息内容
     * @param extrasparam 扩展字段
     * @return 0推送失败,1推送成功
     */
    public static int sendToAliasListUser(List<String> aliasList, String notification_title, String msg_title, String msg_content, String extrasparam) {
        int result = 0;
        try {
            PushPayload pushPayload= PushPayloadUtils.buildPushObject_all_aliasList_alertWithTitle(aliasList,notification_title,msg_title,msg_content,extrasparam);
            LOGGER.info("推送给设备标识参数的用户"+pushPayload);
            PushResult pushResult=jPushClientUser.sendPush(pushPayload);
            LOGGER.info("推送结果"+pushResult);
            if(pushResult.getResponseCode() == 200){
                result=1;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return result;
    }
 
    /**
     * 推送给Tag参数的用户
     * @param tagsList Tag或Tag组
     * @param notification_title 通知内容标题
     * @param msg_title 消息内容标题
     * @param msg_content 消息内容
     * @param extrasparam 扩展字段
     * @return 0推送失败,1推送成功
     */
    public static int sendToTagListUser(List<String> tagsList,String notification_title, String msg_title, String msg_content, String extrasparam) {
        int result = 0;
        try {
            PushPayload pushPayload= PushPayloadUtils.buildPushObject_all_tagList_alertWithTitle(tagsList,notification_title,msg_title,msg_content,extrasparam);
            LOGGER.info(""+pushPayload);
            PushResult pushResult=jPushClientUser.sendPush(pushPayload);
            LOGGER.info(""+pushResult);
            if(pushResult.getResponseCode()==200){
                result=1;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return result;
    }
 
    /**
     * 发送给所有安卓用户
     * @param notification_title 通知内容标题
     * @param msg_title 消息内容标题
     * @param msg_content 消息内容
     * @param extrasparam 扩展字段
     * @return 0推送失败,1推送成功
     */
    public static int sendToAllAndroidUser( String notification_title, String msg_title, String msg_content, String extrasparam) {
        int result = 0;
        try {
            PushPayload pushPayload= PushPayloadUtils.buildPushObject_android_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
            LOGGER.info(""+pushPayload);
            PushResult pushResult=jPushClientUser.sendPush(pushPayload);
            LOGGER.info(""+pushResult);
            if(pushResult.getResponseCode()==200){
                result=1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    /**
     * 发送给所有IOS用户
     * @param notification_title 通知内容标题
     * @param msg_title 消息内容标题
     * @param msg_content 消息内容
     * @param extrasparam 扩展字段
     * @return 0推送失败,1推送成功
     */
    public static int sendToAllIosUser(String notification_title, String msg_title, String msg_content, String extrasparam) {
        int result = 0;
        try {
            PushPayload pushPayload= PushPayloadUtils.buildPushObject_ios_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
            LOGGER.info(""+pushPayload);
            PushResult pushResult=jPushClientUser.sendPush(pushPayload);
            LOGGER.info(""+pushResult);
            if(pushResult.getResponseCode()==200){
                result=1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    /**
     * 发送给所有用户
     * @param notification_title 通知内容标题
     * @param msg_title 消息内容标题
     * @param msg_content 消息内容
     * @param extrasparam 扩展字段
     * @return 0推送失败,1推送成功
     */
    public static int sendToAllUser(String notification_title, String msg_title, String msg_content, String extrasparam) {
        int result = 0;
        try {
            PushPayload pushPayload= PushPayloadUtils.buildPushObject_android_and_ios(notification_title,msg_title,msg_content,extrasparam);
            LOGGER.info(""+pushPayload);
            PushResult pushResult=jPushClientUser.sendPush(pushPayload);
            LOGGER.info(""+pushResult);
            if(pushResult.getResponseCode()==200){
                result=1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    public static void main(String[] args) {
        List<String> ids = new ArrayList<>();
        ids.add(PREFIX + "26113");
        JpushUtils.sendToAliasListUser(ids, "内容","1","1","3");
    }
 
}