xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package com.nuvole.util;
 
import com.gexin.rp.sdk.base.IAliasResult;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.notify.Notify;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.AbstractTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.nuvole.common.domain.emnu.CommonResultEmnu;
import com.nuvole.common.domain.result.CommonResult;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
 
/**
 * 推送工具类(个推)
 *
 * @Author: lc
 * @Date: 2020/4/26 13:41
 */
 
public class GeTuiPush {
 
    private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
//    private static String appId = "0TrCn76GcW8T4y3wediOn9";
    private static String appId = "g1FYuSXD2x5HY2KfWswP19";
//    private static String appKey = "FUPAiYeLSo5hbPIZrRDT21";
    private static String appKey = "kkMqLLBGLd7wx0mhIbG9l5";
//    private static String masterSecret = "ne5FG9hfS77RNyGAnQF5p1";
    private static String masterSecret = "AEuOKMlvTY9aLrdjtPhzy7";
    private static IGtPush igtPush = new IGtPush(url, appKey, masterSecret);
 
 
    /**
     * 为用户绑定别名
     *
     * @param clientId 客户端身份id
     * @param alias    别名
     * @Author: lc
     * @Date: 2020/4/26 13:36
     */
    public static CommonResult bindAlias(String clientId, String alias) {
 
        IAliasResult ret = null;
        try {
            ret = igtPush.bindAlias(appId, alias, clientId);
            System.out.println("is bindAlias res:" + ret.getResponse());
        } catch (RequestException e) {
            e.printStackTrace();
        }
        if (ret != null) {
            Map res = ret.getResponse();
            if ("ok".equals(res.get("result").toString())) {
                return new CommonResult(res);
            } else {
                return new CommonResult(CommonResultEmnu.ERROR, res.get("error_msg").toString());
            }
        } else {
            return new CommonResult(CommonResultEmnu.ERROR, "绑定个推别名时出现异常!");
        }
    }
 
 
    //测试
    public static void main(String[] args) {
 
        bindAlias("0482fa489fe285cbb8b96054f81a0b4f", "123456789");
        pushMessageToSingle("测试demo", "测试demo内容", 2, "123456789");
    }
 
    /**
     * 单推(单个clientId/别名)
     *
     * @param title   标题
     * @param content 内容
     * @param type    类型【1.设备id(clientId) 2.别名】
     * @param val     类型值(clientId/别名)
     * @Author: lc
     * @Date: 2020/4/26 13:31
     */
    public static Map pushMessageToSingle(String title, String content, int type, String val) {
 
        /*透传消息模版*/
        AbstractTemplate template = getTransmissionTemplate(title, content);
 
        // 单推消息类型
        SingleMessage message = getSingleMessage(template);
        Target target = new Target();
        target.setAppId(appId);
        if (type == 1) {
            target.setClientId(val);
        } else {
            target.setAlias(val);//别名需要提前绑定
        }
        IPushResult ret = null;
        try {
            ret = igtPush.pushMessageToSingle(message, target);
            System.out.println("is pushMessageToSingle res:" + ret.getResponse());
        } catch (RequestException e) {
            e.printStackTrace();
            ret = igtPush.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            return ret.getResponse();
        } else {
            System.out.println("个推单条推送服务器响应异常");
            return null;
        }
    }
 
 
    /**
     * 推送一个列表
     *
     * @param title   标题
     * @param content 内容
     * @parma type    类型【1.设备id(clientId) 2.别名】
     * @parma jsonArray    类型值(clientId/别名)
     * @Author: lc
     * @Date: 2020/4/26 13:37
     */
    public static Map pushMessageToList(String title, String content, int type, List<String> list) {
        /*透传消息模版*/
        AbstractTemplate template = getTransmissionTemplate(title, content);
 
        ListMessage message = new ListMessage();
        message.setData(template);
        // 设置消息离线,并设置离线时间
        message.setOffline(true);
        // 离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(24 * 1000 * 3600);
        message.setPriority(1);
        String taskId = igtPush.getContentId(message);
        // 配置推送目标
        List targets = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            String cid = list.get(i);
            Target target = new Target();
            target.setAppId(appId);
            if (type == 1) {
                target.setClientId(cid);
            } else {
                target.setAlias(cid);
            }
            targets.add(target);
        }
 
        IPushResult ret = null;
        try {
            ret = igtPush.pushMessageToList(taskId, targets);
            System.out.println("is pushMessageToList:" + ret.getResponse());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (ret != null) {
            return ret.getResponse();
        } else {
            System.out.println("个推推送一个列表服务器响应异常");
            return null;
        }
    }
 
 
    /**
     * 推送所有手机用户
     *
     * @param title   标题
     * @param content 内容
     * @Author: lc
     * @Date: 2020/4/26 13:36
     */
    public static Map pushMessageToApp(String title, String content) {
        /*透传消息模版*/
        AbstractTemplate template = getTransmissionTemplate(title, content);
 
        // 单推消息类型
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setOffline(true);
        message.setOfflineExpireTime(24 * 1000 * 3600);  //离线有效时间,单位为毫秒,可选
        //全量推送时希望能控制推送速度不要太快,缓减服务器连接压力,可设置定速推送。如果未设置则按默认推送速度发送
        message.setSpeed(100);   // 设置为100,含义为个推控制下发速度在100条/秒左右
        List<String> appIdList = new ArrayList<String>();
        appIdList.add(appId);
        message.setAppIdList(appIdList);
 
        IPushResult ret = null;
        try {
            ret = igtPush.pushMessageToApp(message);
            System.out.println("is pushMessageToApp res:" + ret.getResponse());
        } catch (RequestException e) {
            e.printStackTrace();
            ret = igtPush.pushMessageToApp(message, e.getRequestId());
        }
        if (ret != null) {
            return ret.getResponse();
        } else {
            System.out.println("个推推送所有手机用户服务器响应异常");
            return null;
        }
    }
 
    /**
     * 透传消息模版
     *
     * @param title   标题
     * @param content 内容
     * @Author: lc
     * @Date: 2020/4/26 13:46
     */
    private static TransmissionTemplate getTransmissionTemplate(String title, String content) {
        TransmissionTemplate template = new TransmissionTemplate();
        /*设置APPID与APPKEY*/
        template.setAppId(appId);
        template.setAppkey(appKey);
        /*透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动*/
        template.setTransmissionType(2);
        template.setTransmissionContent(content);
        /*ios消息推送*/
        template.setAPNInfo(getAPNPayload());
 
        Notify notify = new Notify();
//        notify.setIntent("intent:#intent;action=android.intent.action.oppopush;package=cn.com.pconline.android.browser;component=cn.com.android.browser/cn.com.android.browser.push.MultivendorPushActivity;S.protocol=pconlinebrowser://information-article/8270922;end");
        notify.setIntent("intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=com.liuqit.notebook/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=" + title + ";S.content=" + content + ";S.payload=" + content + ";end");
        notify.setTitle(title);
        notify.setContent(content);
        /*第三方*/
        template.set3rdNotifyInfo(notify);
        return template;
    }
 
    private static SingleMessage getSingleMessage(AbstractTemplate template) {
        SingleMessage message = new SingleMessage();
        message.setData(template);
        // 设置消息离线,并设置离线时间
        message.setOffline(true);
        // 离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(72 * 3600 * 1000);
        message.setPriority(1);
        // 判断客户端是否wifi环境下推送。1为仅在wifi环境下推送,0为不限制网络环境,默认不限
        message.setPushNetWorkType(0);
        return message;
    }
 
    private static APNPayload getAPNPayload() {
        APNPayload payload = new APNPayload();
        //在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
        payload.setAutoBadge("+1");
        payload.setContentAvailable(1);
        //ios 12.0 以上可以使用 Dictionary 类型的 sound
        payload.setSound("default");
        payload.setCategory("$由客户端定义");
        payload.addCustomMsg("由客户自定义消息key", "由客户自定义消息value");
 
        //简单模式APNPayload.SimpleMsg
        payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));
//        payload.setAlertMsg(getDictionaryAlertMsg());  //字典模式使用APNPayload.DictionaryAlertMsg
 
        //设置语音播报类型,int类型,0.不可用 1.播放body 2.播放自定义文本
        payload.setVoicePlayType(0);
 
        return payload;
    }
 
}