admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
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
274
275
276
277
package com.yeshi.fanli.util.push;
 
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
 
import com.vivo.push.sdk.notofication.Message;
import com.vivo.push.sdk.notofication.Result;
import com.vivo.push.sdk.notofication.TargetMessage;
import com.vivo.push.sdk.server.Sender;
import com.yeshi.fanli.dto.push.PushTypeEnum;
import com.yeshi.fanli.exception.push.PushException;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
 
/**
 * VIVO的推送帮助
 * 
 * @author Administrator
 *
 */
public class VIVOPushUtil {
 
    private static final int APP_ID = 15206;
    private static final String APP_KEY = "996c07d6-ec2b-41ef-b7c9-abb51ac63abf";
    private static final String APP_SECRET = "882548de-8183-4d29-9769-48b2bb4169c8";
    static String authToken = null;
    static long authTokenTime = 0L;
 
    /**
     * 获取授权码
     * 
     * @param appId
     * @param appKey
     * @param appSecret
     * @return
     * @throws Exception
     */
    private static String getAuthToken(int appId, String appKey, String appSecret) throws Exception {
        Sender sender = new Sender(appSecret);// 实例化Sender
        sender.initPool(20, 10);// 设置连接池参数,可选项
        Result result = sender.getToken(appId, appKey);// 发送鉴权请求
        if (result.getResult() == 0) {
            return result.getAuthToken();
        }
        throw new Exception("token获取失败");
    }
 
    private static String getAuthToken() {
        if (StringUtil.isNullOrEmpty(authToken) || (System.currentTimeMillis() - authTokenTime) > 5000 * 60 * 60L) {
            try {
                String token = getAuthToken(APP_ID, APP_KEY, APP_SECRET);
                authToken = token;
                authTokenTime = System.currentTimeMillis();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return authToken;
    }
 
    private static Message.Builder getBaseMessageBuidler(String title, String content,
            Map<String, String> customParams) {
        Message.Builder buidler = new Message.Builder();
 
        buidler = buidler.title(title).content(content).skipType(4).skipContent("todo").notifyType(3)
                .requestId(UUID.randomUUID().toString());
        buidler = buidler.clientCustomMap(customParams);
        return buidler;
    }
 
    private static Message.Builder getMessageBuidler(PushTypeEnum type, String title, String content, String activity,
            String url, String webUrl, Long id) {
        Map<String, String> data = new HashMap<>();
        if (type != null)
            data.put("type", type.name());
        if (activity != null)
            data.put("activity", activity);
        if (url != null)
            data.put("url", url);
        if (webUrl != null)
            data.put("webUrl", webUrl);
        if (id != null)
            data.put("id", id + "");
        return getBaseMessageBuidler(title, content, data);
    }
 
    // 推送所有设备
    private static void pushAll(Message.Builder builder) {
        String authToken = getAuthToken();
        if (StringUtil.isNullOrEmpty(authToken))
            return;
        try {
            Sender sender = new Sender(APP_SECRET);
            sender.initPool(20, 10);// 设置连接池参数,可选项
            sender.setAuthToken(authToken);// 设置推送的必要参数authToken(调用鉴权方法获得)
            Message allSendMessage = builder.build();// 构建要全量推送的消息体
            Result result = sender.sendToAll(allSendMessage);
            if (result.getResult() == 0)// 成功
            {
 
            }
        } catch (Exception e) {
 
        }
    }
 
    // 推送部分设备
    private static boolean push(List<String> regIdList, Message.Builder builder) {
        String authToken = getAuthToken();
        if (StringUtil.isNullOrEmpty(authToken))
            return false;
        try {
            Sender sender = new Sender(APP_SECRET);
            sender.initPool(20, 10);// 设置连接池参数,可选项
            sender.setAuthToken(authToken);// 设置推送的必要参数authToken(调用鉴权方法获得)
            Message saveList = builder.timeToLive(3600).build();// 构建要保存的批量推送消息体
            Result result = null;
            if (regIdList.size() > 1) {
                result = sender.saveListPayLoad(saveList);// 发送保存群推消息请求
                String taskId = result.getTaskId();
                Set<String> regIds = new HashSet<>();// 构建批量推送用户群
                for (String regId : regIdList)
                    regIds.add(regId);
                TargetMessage targetMessage = new TargetMessage.Builder().requestId(UUID.randomUUID().toString())
                        .regIds(regIds).taskId(taskId).build();// 构建批量推送的消息体
                result = sender.sendToList(targetMessage);// 批量推送给用户
            } else
            {
                result = sender.sendSingle(builder.regId(regIdList.get(0)).timeToLive(3600).build());
            }
            if (result.getResult() == 0)// 成功
            {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    /**
     * 推送商品
     * 
     * @param registerIds
     *            -最大值1000
     * @param title
     * @param content
     * @param goodsType
     * @param goodsId
     * @throws PushException
     */
    public static void pushGoods(List<String> registerIds, String title, String content, int goodsType, Long goodsId)
            throws PushException {
        if (registerIds != null && registerIds.size() > 500)
            throw new PushException(1, "设备数不能超过500个");
 
        String activity = Constant.systemCommonConfig.getAndroidBaseactivityName()
                + ".ui.recommend.GoodsBrowserActivity";
        Message.Builder builder = getMessageBuidler(PushTypeEnum.goodsdetail, title, content, activity, null, null,
                goodsId);
        try {
            boolean success = push(registerIds, builder);
            if (!success)
                throw new PushException(2, "推送失败");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 网页推送
     * 
     * @param registerIds
     *            最大值1000
     * @param title
     * @param content
     * @param url
     *            -网页链接
     * @throws PushException
     */
    public static void pushUrl(List<String> registerIds, String title, String content, String url)
            throws PushException {
        if (registerIds != null && registerIds.size() > 500)
            throw new PushException(1, "设备数不能超过500个");
        String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.invite.ShareBrowserActivity";
        Message.Builder builder = getMessageBuidler(PushTypeEnum.url, title, content, activity, null, url, null);
        try {
            boolean success = push(registerIds, builder);
            if (!success)
                throw new PushException(2, "推送失败");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 站内信推送
     * 
     * @param registerIds
     *            最大值1000
     * @param title
     * @param content
     * @param url
     *            -网页链接
     * @throws PushException
     */
    public static void pushZNX(List<String> registerIds, String title, String content) throws PushException {
        if (registerIds != null && registerIds.size() > 500)
            throw new PushException(1, "设备数不能超过500个");
        Message.Builder builder = getMessageBuidler(PushTypeEnum.ZNX, title, content, null, null, null, null);
        try {
            boolean success = push(registerIds, builder);
            if (!success)
                throw new PushException(2, "推送失败");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void pushWEEX(List<String> registerIds, String title, String content, String weexUrl)
            throws PushException {
        String activity = Constant.systemCommonConfig.getAndroidBaseactivityName()
                + ".ui.mine.weex.WeexApplicationActivity";
        Message.Builder builder = getMessageBuidler(PushTypeEnum.weex, title, content, activity, weexUrl, weexUrl,
                null);
        try {
            boolean success = push(registerIds, builder);
            if (!success)
                throw new PushException(2, "推送失败");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void pushBaiChuanUrl(List<String> registerIds, String title, String content, String url)
            throws PushException {
        Message.Builder builder = getMessageBuidler(PushTypeEnum.baichuan, title, content, null, url, null, null);
 
        try {
            boolean success = push(registerIds, builder);
            if (!success)
                throw new PushException(2, "推送失败");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void pushWelfareCenter(List<String> registerIds, String title, String content) throws PushException {
        String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.mine.WelfareCenterActivity";
        Message.Builder builder = getMessageBuidler(PushTypeEnum.welfare, title, content, activity, null, null, null);
        try {
            boolean success = push(registerIds, builder);
            if (!success)
                throw new PushException(2, "推送失败");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void pushUserSignInNotification(List<String> registerIds, String title, String content)
            throws PushException {
        String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.goldtask.GoldTaskActivity";
        Message.Builder builder = getMessageBuidler(PushTypeEnum.signin, title, content, activity, null, null, null);
        try {
            boolean success = push(registerIds, builder);
            if (!success)
                throw new PushException(2, "推送失败");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}