admin
2021-06-29 0a03971cf8b1ca89f171946ecce8e8e6435b9ec5
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
package org.yeshi.utils.push;
 
import com.alibaba.fastjson.JSONObject;
import com.oppo.push.server.*;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.push.entity.PushAppInfo;
import org.yeshi.utils.push.entity.PushMessage;
import org.yeshi.utils.push.exception.MeiZuPushException;
import org.yeshi.utils.push.exception.OppoPushException;
 
import java.io.IOException;
import java.util.*;
 
/**
 * OPPO推送
 * 推送文档:https://open.oppomobile.com/wiki/doc#id=10203
 */
public class OppoPushUtil {
 
    private static Notification getNotification(PushMessage message) {
        Notification notification = new Notification();
        String title = message.getTitle();
        String content = message.getContent();
        if (title.length() > 50) {
            title = title.substring(0, 50 - 3) + "...";
        }
        if (content.length() > 50) {
            content = content.substring(0, 50 - 3) + "...";
        }
/**
 * 以下参数必填项
 */
        //字数限制1~32
        notification.setTitle(title);
        //字数限制200以内,中英文均以一个计算
        notification.setContent(content);
/**
 * 以下参数非必填项, 如果需要使用可以参考OPPO push服务端api文档进行设置
 */
        //通知栏样式 1. 标准样式  2. 长文本样式  3. 大图样式 【非必填,默认1-标准样式】
        notification.setStyle(1);
// 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(message.getActivity());
// 网页地址【click_action_type为2必填,长度500】
//        notification.setClickActionUrl("http://www.test.com");
 
        if (message.getActivityParams() != null && message.getActivityParams().size() > 0) {
            JSONObject params = new JSONObject();
            for (Iterator<String> its = message.getActivityParams().keySet().iterator(); its.hasNext(); ) {
                String key = its.next();
                params.put(key, message.getActivityParams().get(key));
            }
            // 动作参数,打开应用内页或网页时传递给应用或网页【JSON格式,非必填】,字符数不能超过4K,示例:{"key1":"value1","key2":"value2"}
            notification.setActionParameters(params.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);
        //若推送消息时不带通道推送, Android 8及以上的手机将收不到消息
        notification.setChannelId("channel_android");
        return notification;
    }
 
    /**
     * 推送通知
     *
     * @param appInfo
     * @param message
     * @param regIds  不能超过1000个
     * @return
     * @throws IOException
     * @throws MeiZuPushException
     */
    public static String pushNotificationByRegIds(PushAppInfo appInfo, PushMessage message, List<String> regIds) throws Exception, OppoPushException {
        //推送标题限制字数1-32,推送内容限制字数1-100
        Sender sender = new Sender(appInfo.getAppKey(), appInfo.getAppSecret());
        Result result = null;
        if (regIds.size() > 1) {
            Map batch = new HashMap();  // batch最大为1000
            for (String regId : regIds) {
                batch.put(Target.build(regId), getNotification(message));
            }
            result = sender.unicastBatchNotification(batch); //发送批量单推消息
        } else {
            Target target = Target.build(regIds.get(0)); //创建发送对象
            result = sender.unicastNotification(getNotification(message), target);  //发送单推消息
        }
        if (result.getStatusCode() == 200) {
            if (result.getReturnCode().getCode() == 0) {
                return result.getMessageId();
            } else {
                throw new OppoPushException(result.getReturnCode().getCode() + "", result.getReturnCode().getMessage());
            }
        } else {
            throw new OppoPushException("网络请求出错");
        }
    }
 
    /**
     * 推送所有
     *
     * @param appInfo
     * @param message
     * @return
     * @throws IOException
     * @throws MeiZuPushException
     */
    public static String pushNotificationAll(PushAppInfo appInfo, PushMessage message, List<String> regIds) throws Exception, OppoPushException {
        Sender sender = new Sender(appInfo.getAppKey(), appInfo.getAppSecret());
        Notification broadNotification = getNotification(message);// 创建通知栏消息体
        Result saveResult = sender.saveNotification(broadNotification); // 发送保存消息体请求
        saveResult.getStatusCode(); // 获取http请求状态码
        saveResult.getReturnCode(); // 获取平台返回码
        String messageId = saveResult.getMessageId(); //获取messageId
        Target target = new Target(); // 创建广播目标
        target.setTargetValue(StringUtil.concat(regIds, ";"));
        Result broadResult = sender.broadcastNotification(messageId, target); // 发送广播消息
        broadResult.getTaskId(); // 获取广播taskId
        List<Result.BroadcastErrorResult> errorList = broadResult.getBroadcastErrorResults();
        if (errorList.size() > 0) { // 如果大小为0,代表所有目标发送成功
            for (Result.BroadcastErrorResult error : errorList) {
                error.getErrorCode(); // 错误码
                error.getTargetValue(); // 目标
            }
            return broadResult.getTaskId();
        } else {
            return broadResult.getTaskId();
        }
    }
 
 
    public static void main(String[] args) {
        PushAppInfo appInfo = new PushAppInfo("30514447", "78b911aa08e1468baf24ddea6c7ca23e", "fda37b51cc7f4d4d8b3d326455ec58c0");
        Map<String, String> activityParams = new HashMap<>();
        activityParams.put("activity", "test");
        JSONObject jumpParams = new JSONObject();
        jumpParams.put("id", 123123);
        activityParams.put("params", jumpParams.toJSONString());
 
        PushMessage message = new PushMessage("你好啊", "下午好,hello world", "com.weikou.beibeivideo.ui.push.OpenClickActivity", null,null,activityParams);
 
        String[] ids = new String[]{
                "OPPO_CN_fbaf9e7431c18d4ac4a08089d124c6ca"
        };
 
        try {
            OppoPushUtil.pushNotificationAll(appInfo, message, Arrays.asList(ids));
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
 
}