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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package cn.jpush.api.push.model;
 
import cn.jiguang.common.utils.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
 
import cn.jiguang.common.utils.Preconditions;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.push.model.notification.PlatformNotification;
 
import java.util.LinkedHashMap;
import java.util.Map;
 
/**
 * The object you should build for sending a push.
 * 
 * Basically start with newBuilder() method to build a PushPayload object.
 * 
 * alertAll() is a shortcut for quickly build payload of alert to all platform and all audience;
 * mesageAll() is a shortcut for quickly build payload of message to all platform and all audience.
 * 
 */
public class PushPayload implements PushModel {
 
    private static final String PLATFORM = "platform";
    private static final String AUDIENCE = "audience";
    private static final String NOTIFICATION = "notification";
    private static final String INAPPMESSAGE = "inapp_message";
    private static final String MESSAGE = "message";
    private static final String OPTIONS = "options";
    private static final String SMS = "sms_message";
    private static final String NOTIFICATION3RD = "notification_3rd";
    private static final String CID = "cid";
    private static final String TARGET = "target";
 
    /**
     * Definition acording to JPush Docs
     */
    private static final int MAX_GLOBAL_ENTITY_LENGTH = 4000;
 
    /**
     * Definition acording to JPush Docs
     */
    private static final int MAX_IOS_PAYLOAD_LENGTH = 2000;
    
    private static Gson _gson = new GsonBuilder().disableHtmlEscaping().create();
    
    private final Platform platform;
    private final Audience audience;
    private final Notification notification;
    private final InappMessage inappMessage;
    private final Message message;
    private Options options;
    private SMS sms;
    private final Notification3rd notification3rd;
    private String cid;
    private String target;
    protected Map<String, JsonObject> custom;
    
    private PushPayload(Platform platform, Audience audience, 
            Notification notification, InappMessage inappMessage, Message message, Options options,
                        SMS sms, Notification3rd notification3rd, String cid, String target, Map<String, JsonObject> custom) {
        this.platform = platform;
        this.audience = audience;
        this.notification = notification;
        this.inappMessage = inappMessage;
        this.message = message;
        this.options = options;
        this.sms = sms;
        this.notification3rd = notification3rd;
        this.cid = cid;
        this.target = target;
        this.custom = custom;
    }
 
    public PushPayload setCid(String cid) {
        this.cid = cid;
        return this;
    }
 
    public Platform getPlatform() {
        return platform;
    }
 
    public String getTarget() {
        return target;
    }
 
    public String getCid() {
        return cid;
    }
 
    /**
     * The entrance for building a PushPayload object.
     * @return PushPayload builder
     */
    public static Builder newBuilder() {
        return new Builder();
    }
    
    /**
     * The shortcut of building a simple alert notification object to all platforms and all audiences
     * @param alert The alert message.
     * @return PushPayload
     */
    public static PushPayload alertAll(String alert) {
        return new Builder()
            .setPlatform(Platform.all())
            .setAudience(Audience.all())
            .setNotification(Notification.alert(alert)).build();
    }
 
    public static PushPayload alertAll(String alert, SMS sms) {
        return new Builder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(Notification.alert(alert))
                .setSMS(sms)
                .build();
    }
    
    /**
     * The shortcut of building a simple message object to all platforms and all audiences
     * @param msgContent The message content.
     * @return PushPayload
     */
    public static PushPayload messageAll(String msgContent) {
        return new Builder()
            .setPlatform(Platform.all())
            .setAudience(Audience.all())
            .setMessage(Message.content(msgContent)).build();
    }
 
    public static PushPayload messageAll(String msgContent, SMS sms) {
        return new Builder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setMessage(Message.content(msgContent))
                .setSMS(sms)
                .build();
    }
    
    public static PushPayload fromJSON(String payloadString) {
        return _gson.fromJson(payloadString, PushPayload.class);
    }
    
    public void resetOptionsApnsProduction(boolean apnsProduction) {
        if (null == options) {
            options = Options.newBuilder().setApnsProduction(apnsProduction).build();
        } else {
            options.setApnsProduction(apnsProduction);
        }
    }
    
    public void resetOptionsTimeToLive(long timeToLive) {
        if (null == options) {
            options = Options.newBuilder().setTimeToLive(timeToLive).build();
        } else {
            options.setTimeToLive(timeToLive);
        }
    }
    
    public int getSendno() {
        if (null != options) {
            return options.getSendno();
        }
        return 0;
    }
 
    public Audience getAudience() {
        return audience;
    }
 
    public Options getOptions() {
        return options;
    }
    
    @Override
    public JsonElement toJSON() {
        JsonObject json = new JsonObject();
        if (null != platform) {
            json.add(PLATFORM, platform.toJSON());
        }
        if (null != audience) {
            json.add(AUDIENCE, audience.toJSON());
        }
        if (null != notification) {
            json.add(NOTIFICATION, notification.toJSON());
        }
        if (null != inappMessage) {
            json.add(INAPPMESSAGE, inappMessage.toJSON());
        }
        if (null != message) {
            json.add(MESSAGE, message.toJSON());
        }
        if (null != options) {
            json.add(OPTIONS, options.toJSON());
        }
        if (null != sms) {
            json.add(SMS, sms.toJSON());
        }
        if (null != notification3rd) {
            json.add(NOTIFICATION3RD, notification3rd.toJSON());
        }
        if (null != cid) {
            json.addProperty(CID, cid);
        }
        if (null != target) {
            json.addProperty(TARGET, target);
        }
        if (null != custom) {
            for (Map.Entry<String, JsonObject> entry : custom.entrySet()) {
                json.add(entry.getKey(), entry.getValue());
            }
        }
                
        return json;
    }
 
    public boolean isGlobalExceedLength() {
        int messageLength = 0;
        JsonObject payload = (JsonObject) this.toJSON();
        if (payload.has(MESSAGE)) {
            JsonObject message = payload.getAsJsonObject(MESSAGE);
            messageLength = message.toString().getBytes().length;
        }
        if (!payload.has(NOTIFICATION)) {
            // only mesage
            return messageLength > MAX_GLOBAL_ENTITY_LENGTH;
        } else {
            JsonObject notification = payload.getAsJsonObject(NOTIFICATION);
            if (notification.has(AndroidNotification.NOTIFICATION_ANDROID)) {
                JsonObject android = notification.getAsJsonObject(AndroidNotification.NOTIFICATION_ANDROID);
                int androidLength = android.toString().getBytes().length;
                return (androidLength + messageLength) > MAX_GLOBAL_ENTITY_LENGTH;
            }
        }
        return false;
    }
    
    public boolean isIosExceedLength() {
        JsonObject payload = (JsonObject) this.toJSON();
        if (payload.has(NOTIFICATION)) {
            JsonObject notification = payload.getAsJsonObject(NOTIFICATION);
            if (notification.has(IosNotification.NOTIFICATION_IOS)) {
                JsonObject ios = notification.getAsJsonObject(IosNotification.NOTIFICATION_IOS);
                return ios.toString().getBytes().length > MAX_IOS_PAYLOAD_LENGTH;
            } else {
                if (notification.has(PlatformNotification.ALERT)) {
                    String alert = notification.get(PlatformNotification.ALERT).getAsString();
                    JsonObject ios = new JsonObject();
                    ios.add("alert", new JsonPrimitive(alert));
                    return ios.toString().getBytes().length > MAX_IOS_PAYLOAD_LENGTH;
                } else {
                    // No iOS Payload
                }
            }
        }
        return false;
    }
    
    @Override
    public String toString() {
        return _gson.toJson(toJSON());
    }
    
    public static class Builder {
        private Platform platform = null;
        private Audience audience = null;
        private Notification notification = null;
        private InappMessage inappMessage = null;
        private Message message = null;
        private Options options = null;
        private SMS sms = null;
        private Notification3rd notification3rd = null;
        private String cid;
        private String target;
        private Map<String, JsonObject> custom;
 
        public Builder() {
            this.custom = new LinkedHashMap<>();
        }
 
        public Builder setTarget(String target) {
            this.target = target;
            return this;
        }
 
        public Builder setPlatform(Platform platform) {
            this.platform = platform;
            return this;
        }
        
        public Builder setAudience(Audience audience) {
            this.audience = audience;
            return this;
        }
        
        public Builder setNotification(Notification notification) {
            this.notification = notification;
            return this;
        }
 
        public Builder setInappMessage(InappMessage inappMessage) {
            this.inappMessage = inappMessage;
            return this;
        }
        
        public Builder setMessage(Message message) {
            this.message = message;
            return this;
        }
        
        public Builder setOptions(Options options) {
            this.options = options;
            return this;
        }
 
        public Builder setSMS(SMS sms) {
            this.sms = sms;
            return this;
        }
 
        public Builder setNotification3rd(Notification3rd notification3rd) {
            this.notification3rd = notification3rd;
            return this;
        }
 
        public Builder setCid(String cid) {
            this.cid = cid;
            return this;
        }
 
        public Builder addCustom(String field, JsonObject jsonObject) {
            this.custom.put(field, jsonObject);
            return this;
        }
 
        public PushPayload build() {
 
            if (StringUtils.isTrimedEmpty(target)) {
                Preconditions.checkArgument(!(null == audience || null == platform),
                        "audience and platform both should be set.");
            }
            if (!StringUtils.isTrimedEmpty(target)) {
                Preconditions.checkArgument(!StringUtils.isTrimedEmpty(target) && null != platform,
                        "target and platform should be set.");
            }
 
            Preconditions.checkArgument(! (null == notification && null == message),
                    "notification or message should be set at least one.");
            
            // if options is not set, a sendno will be generated for tracing easily
            if (null == options) {
                options = Options.sendno();
            }
            
            return new PushPayload(platform, audience, notification, inappMessage, message, options, sms, notification3rd, cid, target, custom);
        }
    }
}