admin
2020-05-06 24a8d17e007545f7426c48352109aa1a9c6587ee
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package com.yeshi.fanli.util.push;
 
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
 
import javax.annotation.PostConstruct;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.yeshi.utils.annotation.MapUtil;
 
import com.alibaba.fastjson.JSONObject;
import com.oppo.push.server.Notification;
import com.oppo.push.server.Result;
import com.oppo.push.server.Sender;
import com.oppo.push.server.Target;
import com.yeshi.fanli.entity.config.push.HWPushConfig;
import com.yeshi.fanli.exception.push.PushException;
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
 
@Component
public class OPPOPushUtil {
 
    private static OPPOPushUtil hWPushUtil;
 
    @Autowired
    private BusinessSystemService businessSystemService;
 
    @PostConstruct
    public void init() {
        hWPushUtil = this;
        hWPushUtil.businessSystemService = this.businessSystemService;
    }
 
    private static String appkey = "";
    private static String masterSecret = "";
 
    static {
        Properties ps = org.yeshi.utils.PropertiesUtil
                .getProperties(TaoBaoUtil.class.getClassLoader().getResourceAsStream("push_hw.properties"));
        HWPushConfig config = (HWPushConfig) MapUtil.parseMap(HWPushConfig.class, ps);
        appkey = config.getAppId();
        masterSecret = config.getAppSecret();
    }
 
    private static Map<String, String> getParams(String type, String activity, String url, String webUrl, Long id) {
        Map<String, String> data = new HashMap<>();
        if (type != null)
            data.put("type", type);
        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 data;
    }
 
    /**
     * 推送商品
     * 
     * @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() > 100)
            throw new PushException(1, "设备数不能超过100个");
 
        String activity = Constant.systemCommonConfig.getAndroidBaseactivityName()
                + ".ui.recommend.GoodsBrowserActivity";
        Map<String, String> params = getParams("goodsdetail", activity, null, null, goodsId);
 
        try {
            boolean success = push(registerIds, title, content, params);
            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() > 100)
            throw new PushException(1, "设备数不能超过1000个");
 
        String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.invite.ShareBrowserActivity";
        Map<String, String> params = getParams("url", activity, null, url, null);
        try {
            boolean success = push(registerIds, title, content, params);
            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() > 100)
            throw new PushException(1, "设备数不能超过100个");
        Map<String, String> params = getParams("ZNX", null, null, null, null);
        if (params == null)
            return;
 
        try {
            boolean success = push(registerIds, title, content, params);
            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";
        Map<String, String> params = getParams("weex", activity, weexUrl, weexUrl, null);
        if (params == null)
            return;
 
        try {
            boolean success = push(registerIds, title, content, params);
            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 {
        Map<String, String> params = getParams("baichuan", null, url, null, null);
        if (params == null)
            return;
 
        try {
            boolean success = push(registerIds, title, content, params);
            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";
        Map<String, String> params = getParams("welfare", activity, null, null, null);
        if (params == null)
            return;
 
        try {
            boolean success = push(registerIds, title, content, params);
            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";
        Map<String, String> params = getParams("signin", activity, null, null, null);
        if (params == null)
            return;
 
        try {
            boolean success = push(registerIds, title, content, params);
            if (!success)
                throw new PushException(2, "推送失败");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 
     * @param list
     *            最大为1000
     */
    public static boolean push(List<String> list, String title, String content, Map<String, String> dataParams) {
        Map<String, String> params = null;
        if (dataParams != null) {
            params = new HashMap<>();
            JSONObject json = new JSONObject();
            for (Iterator<String> its = dataParams.keySet().iterator(); its.hasNext();) {
                String key = its.next();
                json.put(key, dataParams.get(key));
            }
            params.put("data", json.toJSONString());
        }
 
        try {
            Sender sender = new Sender("f14S6P4sCrW4gs0WCO80w80cW", "E7976fe3d1758bf0c3480d8DcE4236e2");
            Notification notifyCation = getNotification(title, content,
                    "com.yeshi.ec.rebate.myapplication.ui.OPPOPushMessageActivity", params);
            if (list.size() == 1) {
                Result result = sender.unicastNotification(notifyCation, Target.build(list.get(0)));
                result.getStatusCode(); // 获取http请求状态码
                result.getReturnCode(); // 获取平台返回码
                System.out.println(result.getStatusCode() + "-" + result.getReturnCode());
                if (result.getReturnCode().getCode() == 0)
                    return true;
                else
                    return false;
            } else {
                Map<Target, Notification> batch = new HashMap<Target, Notification>(); // batch最大为1000
                for (String registerId : list) {
                    batch.put(Target.build(registerId), notifyCation);
                }
                Result result = sender.unicastBatchNotification(batch); // 发送批量单推消息
                result.getStatusCode(); // 获取http请求状态码
                result.getReturnCode(); // 获取平台返回码
                System.out.println(result.getStatusCode() + "-" + result.getReturnCode());
                if (result.getReturnCode().getCode() == 0)
                    return true;
                else
                    return false;
            }
 
            // List<Result.UnicastBatchResult> batchResult =
            // result.getUnicastBatchResults(); // 批量单推结果
            // for (Result.UnicastBatchResult record : batchResult) {
            // record.getErrorCode();
            // record.getErrorMessage();
            // record.getMessageId();
            // record.getTargetValue();
            // }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    private static Notification getNotification(String title, String content, String activity,
            Map<String, String> params) {
 
        Notification notification = new Notification();
 
        /**
         * 
         * 以下参数必填项
         * 
         */
 
        notification.setTitle(title);
        notification.setSubTitle(title);
        notification.setContent(content);
 
        /**
         * 
         * 以下参数非必填项, 如果需要使用可以参考OPPO push服务端api文档进行设置
         * 
         */
 
        // App开发者自定义消息Id,OPPO推送平台根据此ID做去重处理,对于广播推送相同appMessageId只会保存一次,对于单推相同appMessageId只会推送一次
        notification.setAppMessageId(UUID.randomUUID().toString());
 
        // 应用接收消息到达回执的回调URL,字数限制200以内,中英文均以一个计算
        // notification.setCallBackUrl("http://www.test.com");
        // App开发者自定义回执参数,字数限制50以内,中英文均以一个计算
        // notification.setCallBackParameter("");
 
        // 点击动作类型0,启动应用;1,打开应用内页(activity的intent
        // action);2,打开网页;4,打开应用内页(activity);【非必填,默认值为0】;5,Intent scheme URL
 
        notification.setClickActionType(4);
 
        // 应用内页地址【click_action_type为1或4时必填,长度500】
        notification.setClickActionActivity(activity);
 
        // 网页地址【click_action_type为2必填,长度500】
        // notification.setClickActionUrl("http://www.test.com");
 
        // 动作参数,打开应用内页或网页时传递给应用或网页【JSON格式,非必填】,字符数不能超过4K,示例:{"key1":"value1","key2":"value2"}
 
        JSONObject paramsData = new JSONObject();
        if (params != null)
            for (Iterator<String> its = params.keySet().iterator(); its.hasNext();) {
                String key = its.next();
                paramsData.put(key, params.get(key));
            }
 
        notification.setActionParameters(paramsData.toJSONString());
        // 展示类型 (0, “即时”),(1, “定时”)
        notification.setShowTimeType(0);
 
        // 定时展示开始时间(根据time_zone转换成当地时间),时间的毫秒数
        // notification.setShowStartTime(System.currentTimeMillis() + 1000 * 60
        // * 3);
 
        // 定时展示结束时间(根据time_zone转换成当地时间),时间的毫秒数
        // notification.setShowEndTime(System.currentTimeMillis() + 1000 * 60 *
        // 5);
 
        // 是否进离线消息,【非必填,默认为True】
 
        notification.setOffLine(true);
 
        // 离线消息的存活时间(time_to_live) (单位:秒), 【off_line值为true时,必填,最长3天】
        notification.setOffLineTtl(24 * 3600);
 
        // 时区,默认值:(GMT+08:00)北京,香港,新加坡
        notification.setTimeZone("GMT+08:00");
        // 0:不限联网方式, 1:仅wifi推送
        notification.setNetworkType(0);
        return notification;
    }
 
}