admin
2022-01-12 4a7367a869ef12375ea6678ca44e102b8919c624
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
package cn.jpush.api.push.model;
 
 
import com.google.gson.*;
 
import cn.jiguang.common.ServiceHelper;
import cn.jiguang.common.utils.Preconditions;
 
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
 
public class Options implements PushModel {
 
    private static final String SENDNO = "sendno";
    private static final String OVERRIDE_MSG_ID = "override_msg_id";
    private static final String TIME_TO_LIVE = "time_to_live";
    private static final String APNS_PRODUCTION = "apns_production";
    private static final String BIG_PUSH_DURATION = "big_push_duration";
    private static final String APNS_COLLAPSE_ID = "apns_collapse_id";
    private static final String THIRD_PARTH_CHANNEl = "third_party_channel";
 
    private static final long NONE_TIME_TO_LIVE = -1;
 
    private final int sendno;
    private final long overrideMsgId;
    private long timeToLive;
    private boolean apnsProduction;
    // minutes
    private int bigPushDuration;
    private String apnsCollapseId;
    private final Map<String, JsonPrimitive> customData;
 
 
    /**
     * {
     *     "third_party_channel":{
     *         "xiaomi":{
     *             "distribution":"ospush",
     *             "channel_id":"*******"
     *         },
     *         "huawei":{
     *             "distribution":"jpush"
     *         },
     *         "meizu":{
     *             "distribution":"jpush"
     *         },
     *         "fcm":{
     *             "distribution":"ospush"
     *         },
     *         "oppo":{
     *             "distribution":"ospush",
     *             "channel_id":"*******"
     *         },
     *         "vivo":{
     *             "distribution":"ospush",
     *             "classification":0 // 2020/06 新增,和vivo官方字段含义一致 0 代表运营消息,1 代表系统消息,不填vivo官方默认为0
     *                                // 使用此字段时,需使用setThirdPartyChannelV2方法,因为此值只能为整数形式
     *         }
     *     }
     * }
     */
    private Map<String, JsonObject> thirdPartyChannel;
 
    private Options(int sendno,
                    long overrideMsgId,
                    long timeToLive,
                    boolean apnsProduction,
                    int bigPushDuration,
                    String apnsCollapseId,
                    Map<String, JsonObject> thirdPartyChannel,
                    Map<String, JsonPrimitive> customData) {
        this.sendno = sendno;
        this.overrideMsgId = overrideMsgId;
        this.timeToLive = timeToLive;
        this.apnsProduction = apnsProduction;
        this.bigPushDuration = bigPushDuration;
        this.apnsCollapseId = apnsCollapseId;
        this.thirdPartyChannel = thirdPartyChannel;
        this.customData = customData;
    }
 
    public static Builder newBuilder() {
        return new Builder();
    }
 
    public static Options sendno() {
        return newBuilder().setSendno(ServiceHelper.generateSendno()).build();
    }
 
    public static Options sendno(int sendno) {
        return newBuilder().setSendno(sendno).build();
    }
 
    public void setApnsProduction(boolean apnsProduction) {
        this.apnsProduction = apnsProduction;
    }
 
    public void setTimeToLive(long timeToLive) {
        this.timeToLive = timeToLive;
    }
 
    public void setBigPushDuration(int bigPushDuration) {
        this.bigPushDuration = bigPushDuration;
    }
 
    public int getSendno() {
        return this.sendno;
    }
 
    @Override
    public JsonElement toJSON() {
        JsonObject json = new JsonObject();
        if (sendno > 0) {
            json.add(SENDNO, new JsonPrimitive(sendno));
        }
        if (overrideMsgId > 0) {
            json.add(OVERRIDE_MSG_ID, new JsonPrimitive(overrideMsgId));
        }
        if (timeToLive >= 0) {
            json.add(TIME_TO_LIVE, new JsonPrimitive(timeToLive));
        }
 
        json.add(APNS_PRODUCTION, new JsonPrimitive(apnsProduction));
 
        if (bigPushDuration > 0) {
            json.add(BIG_PUSH_DURATION, new JsonPrimitive(bigPushDuration));
        }
 
        if (apnsCollapseId != null) {
            json.add(APNS_COLLAPSE_ID, new JsonPrimitive(apnsCollapseId));
        }
 
        if (null != thirdPartyChannel && thirdPartyChannel.size() > 0) {
            JsonObject partyChannel = new JsonObject();
            for (Map.Entry<String, JsonObject> entry : thirdPartyChannel.entrySet()) {
                JsonObject channel = entry.getValue();
                partyChannel.add(entry.getKey(), channel);
            }
            json.add(THIRD_PARTH_CHANNEl, partyChannel);
        }
 
        if (null != customData) {
            for (Map.Entry<String, JsonPrimitive> entry : customData.entrySet()) {
                json.add(entry.getKey(), entry.getValue());
            }
        }
 
        return json;
    }
 
    public static class Builder {
 
        private int sendno = 0;
        private long overrideMsgId = 0;
        private long timeToLive = NONE_TIME_TO_LIVE;
        private boolean apnsProduction = false;
        private int bigPushDuration = 0;
        private String apnsCollapseId;
        private Map<String, JsonObject> thirdPartyChannel;
        private Map<String, JsonPrimitive> customData;
 
        public Builder setSendno(int sendno) {
            this.sendno = sendno;
            return this;
        }
 
        public Builder setOverrideMsgId(long overrideMsgId) {
            this.overrideMsgId = overrideMsgId;
            return this;
        }
 
        public Builder setTimeToLive(long timeToLive) {
            this.timeToLive = timeToLive;
            return this;
        }
 
        public Builder setApnsProduction(boolean apnsProduction) {
            this.apnsProduction = apnsProduction;
            return this;
        }
 
        public Builder setApnsCollapseId(String id) {
            this.apnsCollapseId = id;
            return this;
        }
 
        public Builder setBigPushDuration(int bigPushDuration) {
            this.bigPushDuration = bigPushDuration;
            return this;
        }
 
        @Deprecated
        public Map<String, Map<String, String>> getThirdPartyChannel() {
            if (null != thirdPartyChannel) {
                Map<String, Map<String, String>> thirdPartyChannelRsp = new HashMap<>();
                Set<Map.Entry<String, JsonObject>> entrySet = thirdPartyChannel.entrySet();
                for (Map.Entry<String, JsonObject> entry : entrySet) {
                    JsonObject entryValue = entry.getValue();
                    Set<Map.Entry<String, JsonElement>> valueEntrySet = entryValue.entrySet();
                    Map<String, String> valueMap = new HashMap<>();
                    for (Map.Entry<String, JsonElement> valueEntry : valueEntrySet) {
                        valueMap.put(valueEntry.getKey(), null == valueEntry.getValue() ? null : valueEntry.getValue().getAsString());
                    }
                    thirdPartyChannelRsp.put(entry.getKey(), valueMap);
                }
                return thirdPartyChannelRsp;
            }
            return null;
        }
 
        @Deprecated
        public Builder setThirdPartyChannel(Map<String, Map<String, String>> thirdPartyChannel) {
            this.thirdPartyChannel = new HashMap<>();
            if (null != thirdPartyChannel) {
                Set<Map.Entry<String, Map<String, String>>> entrySet = thirdPartyChannel.entrySet();
                for (Map.Entry<String, Map<String, String>> entry : entrySet) {
                    String key = entry.getKey();
                    Map<String, String> valueMap = entry.getValue();
                    JsonObject valueObj = new JsonObject();
                    if (null != valueMap) {
                        Set<Map.Entry<String, String>> valueEntrySet = valueMap.entrySet();
                        for (Map.Entry<String, String> valueEntry : valueEntrySet) {
                            valueObj.addProperty(valueEntry.getKey(), valueEntry.getValue());
                        }
                        this.thirdPartyChannel.put(key, valueObj);
                    }
                }
 
            }
            return this;
        }
 
        public Builder setThirdPartyChannelV2(Map<String, JsonObject> thirdPartyChannel) {
            this.thirdPartyChannel = thirdPartyChannel;
            return this;
        }
 
        public Builder addCustom(Map<String, String> extras) {
            if (customData == null) {
                customData = new LinkedHashMap<>();
            }
            for (Map.Entry<String, String> entry : extras.entrySet()) {
                customData.put(entry.getKey(), new JsonPrimitive(entry.getValue()));
            }
            return this;
        }
 
        public Builder addCustom(String key, Number value) {
            Preconditions.checkArgument(! (null == key), "Key should not be null.");
            if (customData == null) {
                customData = new LinkedHashMap<>();
            }
            customData.put(key, new JsonPrimitive(value));
            return this;
        }
 
        public Builder addCustom(String key, String value) {
            Preconditions.checkArgument(! (null == key), "Key should not be null.");
            if (customData == null) {
                customData = new LinkedHashMap<>();
            }
            customData.put(key, new JsonPrimitive(value));
            return this;
        }
 
        public Builder addCustom(String key, Boolean value) {
            Preconditions.checkArgument(! (null == key), "Key should not be null.");
            if (customData == null) {
                customData = new LinkedHashMap<>();
            }
            customData.put(key, new JsonPrimitive(value));
            return this;
        }
 
        public Options build() {
            Preconditions.checkArgument(sendno >= 0, "sendno should be greater than 0.");
            Preconditions.checkArgument(overrideMsgId >= 0, "override_msg_id should be greater than 0.");
            Preconditions.checkArgument(timeToLive >= NONE_TIME_TO_LIVE, "time_to_live should be greater than 0.");
            Preconditions.checkArgument(bigPushDuration >= 0, "bigPushDuration should be greater than 0.");
 
            if (sendno <= 0) {
                sendno = ServiceHelper.generateSendno();
            }
 
            return new Options(sendno, overrideMsgId, timeToLive, apnsProduction, bigPushDuration, apnsCollapseId, thirdPartyChannel, customData);
        }
    }
 
}