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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package cn.jpush.api.push.model.notification;
 
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
 
import java.util.Map;
 
/**
 * <p><b>Android 通知类</b></p>
 * <br>
 * 具体使用方法请参考官方文档
 * 支持 Android Notification 的参数:
 * <ul>
 * <li>alert: 继承自父类 PlatformNotification 的 alert 属性;本类设置则覆盖。</li>
 * <li>title: 支持 setTitle(string) 方法来设置;可替换展示App名称的地方。 </li>
 * <li>builder_id: 支持 setBuilderId(int) 方法来设置。android 8.0 开始建议采用NotificationChannel配置。 </li>
 * <li>channel_id: 支持 setChannelId(string) 方法来设置;不超过1000字节。</li>
 * <li>priority: 支持 setPriority(int) 方法来设置。可改变通知栏展示优先级</li>
 * <li>category: 支持 setCategory(string) 方法来设置。</li>
 * <li>style: 支持 setStyle(int) 方法来设置;可改变通知栏样式类型。</li>
 * <li>alert_type: 支持 setAlertType(int) 方法来设置;可改变通知方式。</li>
 * <li>big_text: 支持 setBigText(string) 方法来设置;可改变大文本通知栏样式。 </li>
 * <li>inbox: 支持 setInbox(JSONObject) 方法来设置;可改变文本条目通知栏样式。</li>
 * <li>big_pic_path: 支持 setBigPicPath(string) 方法来设置;可改变大图片通知栏样式。 </li>
 * <li>extras: 继承自父类 PlatformNotification 的 extras 属性;支持通过 addExtra(key, value) 来添加自定义字段,具体看代码。 </li>
 * <li>large_icon: 支持 setLargeIcon(string) 方法来设置;可设置通知栏大图标。</li>
 * <li>small_icon_uri: 支持 setSmallIconUri(string) 方法来设置;可设置通知栏小图标。</li>
 * <li>intent: 支持 setIntent(JSONObject) 方法来设置; 可指定跳转页面。</li>
 * <li>uri_activity: 支持 setUriActivity(string) 方法来设置; 可指定跳转页面。</li>
 * <li>uri_action: 支持 setUriAction(string) 方法来设置;可指定跳转页面。</li>
 * <li>badge_add_num: 支持 setBadgeAddNum(int) 方法来设置;可指定角标数字增加</li>
 * <li>badge_class: 支持 setBadgeClass(string) 方法来设置;配合badge_add_num使用,二者需要共存,缺一不可。</li>
 * <li>sound: 支持 setSound(string) 方法来设置声音文件;填写文件名称即可,无需文件名后缀。</li>
 * <li>show_begin_time: 支持 setShowBeginTime(string) 方法来设置;可定时展示开始时间。</li>
 * <li>show_end_time: 支持 setShowEndTime(string) 方法来设置;可定时展示结束时间。</li>
 * <li>display_foreground: 支持 setDisplayForeground(string) 方法来设置; 可设置app在前台,通知是否展示。</li>
 ** </ul>
 * <br>
 */
public class AndroidNotification extends PlatformNotification {
    public static final String NOTIFICATION_ANDROID = "android";
 
    private static final String TITLE = "title";
    private static final String BUILDER_ID = "builder_id";
    private static final String CHANNEL_ID = "channel_id";
    private static final String PRIORITY = "priority";
    private static final String CATEGORY = "category";
    private static final String STYLE = "style";
    private static final String ALERT_TYPE = "alert_type";
    private static final String BIG_TEXT = "big_text";
    private static final String INBOX = "inbox";
    private static final String BIG_PIC_PATH = "big_pic_path";
    private static final String LARGE_ICON = "large_icon";
    private static final String SMALL_ICON_URI = "small_icon_uri";
    private static final String INTENT = "intent";
    private static final String URI_ACTIVITY = "uri_activity";
    private static final String URI_ACTION = "uri_action";
    private static final String BADGE_ADD_NUM = "badge_add_num";
    private static final String BADGE_CLASS = "badge_class";
    private static final String SOUND = "sound";
    private static final String SHOW_BEGIN_TIME = "show_begin_time";
    private static final String SHOW_END_TIME = "show_end_time";
    private static final String DISPLAY_FOREGROUND = "display_foreground";
 
    private final String title;
    private final int builderId;
    private String channelId;
    // range from [-2 ~ 2], default is 0
    private int priority;
    private String category;
    // range from [0 ~ 3], default is 0. bigText=1, Inbox=2, bigPicture=3.
    private int style = 0;
    // range from [-1 ~ 7], default is -1
    private int alert_type;
    private String big_text;
    private Object inbox;
    private String big_pic_path;
    private String large_icon;
    private String small_icon_uri;
    private JsonObject intent;
    private String uri_activity;
    private String uri_action;
    // range from [1 ~ 99], suggest set to 1
    private int badge_add_num;
    private String badge_class;
    private String sound;
    private String show_begin_time;
    private String show_end_time;
    private String display_foreground;
 
    private AndroidNotification(Object alert,
                                String title,
                                int builderId,
                                String channelId,
                                int priority,
                                String category,
                                int style,
                                int alertType,
                                String bigText,
                                Object inbox,
                                String bigPicPath,
                                String large_icon,
                                String small_icon_uri,
                                JsonObject intent,
                                String uri_activity,
                                String uri_action,
                                int badge_add_num,
                                String badge_class,
                                String sound,
                                String show_begin_time,
                                String show_end_time,
                                String display_foreground,
                                Map<String, String> extras,
                                Map<String, Number> numberExtras,
                                Map<String, Boolean> booleanExtras,
                                Map<String, JsonObject> jsonExtras,
                                Map<String, JsonPrimitive> customData) {
        super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData);
 
        this.title = title;
        this.builderId = builderId;
        this.channelId = channelId;
        this.priority = priority;
        this.category = category;
        this.style = style;
        this.alert_type = alertType;
        this.big_text = bigText;
        this.inbox = inbox;
        this.big_pic_path = bigPicPath;
        this.large_icon = large_icon;
        this.small_icon_uri = small_icon_uri;
        this.intent = intent;
        this.uri_activity = uri_activity;
        this.uri_action = uri_action;
        this.badge_add_num = badge_add_num;
        this.badge_class = badge_class;
        this.sound = sound;
        this.show_begin_time = show_begin_time;
        this.show_end_time = show_end_time;
        this.display_foreground = display_foreground;
 
    }
 
    public static Builder newBuilder() {
        return new Builder();
    }
 
    public static AndroidNotification alert(String alert) {
        return newBuilder().setAlert(alert).build();
    }
 
    @Override
    public String getPlatform() {
        return NOTIFICATION_ANDROID;
    }
 
    protected Object getInbox() {
        return this.inbox;
    }
 
    protected void setInbox(Object inbox) {
        this.inbox = inbox;
    }
 
    @Override
    public JsonElement toJSON() {
        JsonObject json = super.toJSON().getAsJsonObject();
 
        if (null != title) {
            json.add(TITLE, new JsonPrimitive(title));
        }
 
        if (builderId > 0) {
            json.add(BUILDER_ID, new JsonPrimitive(this.builderId));
        }
 
        if (null != channelId) {
            json.add(CHANNEL_ID, new JsonPrimitive(channelId));
        }
 
        // 默认为 0
        if (0 != priority) {
            json.add(PRIORITY, new JsonPrimitive(priority));
        }
 
        if (null != category) {
            json.add(CATEGORY, new JsonPrimitive(category));
        }
 
        // 默认是 0
        if (0 != style) {
            json.add(STYLE, new JsonPrimitive(this.style));
        }
 
        if (-1 != alert_type && alert_type <= 7) {
            json.add(ALERT_TYPE, new JsonPrimitive(this.alert_type));
        }
 
        if (null != big_text) {
            json.add(BIG_TEXT, new JsonPrimitive(this.big_text));
        }
 
        if (null != inbox) {
            if (inbox instanceof JsonObject) {
                json.add(INBOX, (JsonObject) inbox);
            }
        }
 
        if (null != big_pic_path) {
            json.add(BIG_PIC_PATH, new JsonPrimitive(this.big_pic_path));
        }
 
        if (null != large_icon) {
            json.add(LARGE_ICON, new JsonPrimitive(this.large_icon));
        }
 
        if (null != small_icon_uri) {
            json.add(SMALL_ICON_URI, new JsonPrimitive(this.small_icon_uri));
        }
 
        if (null != intent) {
            json.add(INTENT, intent);
        }
 
        if (null != uri_activity) {
            json.add(URI_ACTIVITY, new JsonPrimitive(this.uri_activity));
        }
 
        if (null != uri_action) {
            json.add(URI_ACTION, new JsonPrimitive(this.uri_action));
        }
 
        // 如果不填写,表示不改变角标数字
        if (0 != badge_add_num) {
            json.add(BADGE_ADD_NUM, new JsonPrimitive(this.badge_add_num));
        }
 
        if (null != badge_class) {
            json.add(BADGE_CLASS, new JsonPrimitive(this.badge_class));
        }
 
        if (null != sound) {
            json.add(SOUND, new JsonPrimitive(this.sound));
        }
 
        if (null != show_begin_time) {
            json.add(SHOW_BEGIN_TIME, new JsonPrimitive(this.show_begin_time));
        }
 
        if (null != show_end_time) {
            json.add(SHOW_END_TIME, new JsonPrimitive(this.show_end_time));
        }
 
        if (null != display_foreground) {
            json.add(DISPLAY_FOREGROUND, new JsonPrimitive(this.display_foreground));
        }
 
 
        return json;
    }
 
    public static class Builder extends PlatformNotification.Builder<AndroidNotification, Builder> {
        private String title;
        private int builderId;
        private String channelId;
        private int priority;
        private String category;
        private int style = 0;
        private int alert_type = -1;
        private String big_text;
        private Object inbox;
        private String big_pic_path;
        private String large_icon;
        private String small_icon_uri;
        private JsonObject intent;
        private String uri_activity;
        private String uri_action;
        private int badge_add_num;
        private String badge_class;
        private String sound;
        private String show_begin_time;
        private String show_end_time;
        private String display_foreground;
 
        @Override
        protected Builder getThis() {
            return this;
        }
 
        @Override
        public Builder setAlert(Object alert) {
            this.alert = alert;
            return this;
        }
 
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }
 
        public Builder setBuilderId(int builderId) {
            this.builderId = builderId;
            return this;
        }
 
        public String getChannelId() {
            return channelId;
        }
 
        public Builder setChannelId(String channelId) {
            this.channelId = channelId;
            return this;
        }
 
        public Builder setPriority(int priority) {
            this.priority = priority;
            return this;
        }
 
        public Builder setCategory(String category) {
            this.category = category;
            return this;
        }
 
        public Builder setStyle(int style) {
            this.style = style;
            return this;
        }
 
        public Builder setAlertType(int alertType) {
            this.alert_type = alertType;
            return this;
        }
 
        public Builder setBigText(String bigText) {
            this.big_text = bigText;
            return this;
        }
 
        public Builder setInbox(Object inbox) {
            if (null == inbox) {
                LOG.warn("Null inbox. Throw away it.");
                return this;
            }
            this.inbox = inbox;
            return this;
        }
 
        public Builder setBigPicPath(String bigPicPath) {
            this.big_pic_path = bigPicPath;
            return this;
        }
 
        public Builder setLargeIcon(String largeIcon) {
            this.large_icon = largeIcon;
            return this;
        }
 
        public Builder setSmallIconUri(String smallIconUri) {
            this.small_icon_uri = smallIconUri;
            return this;
        }
 
        public Builder setIntent(JsonObject intent) {
            if (null == intent) {
                LOG.warn("Null intent. Throw away it.");
                return this;
            }
            this.intent = intent;
            return this;
        }
 
        public Builder setUriActivity(String uriActivity) {
            this.uri_activity = uriActivity;
            return this;
        }
 
        public Builder setUriAction(String uriAction) {
            this.uri_action = uriAction;
            return this;
        }
 
        public Builder setBadgeAddNum(int badgeAddNum) {
            this.badge_add_num = badgeAddNum;
            return this;
        }
 
        public Builder setBadgeClass(String badgeClass) {
            this.badge_class = badgeClass;
            return this;
        }
 
        public Builder setSound(String sound) {
            this.sound = sound;
            return this;
        }
 
        public Builder setShowBeginTime(String showBeginTime) {
            this.show_begin_time = showBeginTime;
            return this;
        }
 
        public Builder setShowEndTime(String showEndTime) {
            this.show_end_time = showEndTime;
            return this;
        }
 
        public Builder setDisplayForeground(String displayForeground) {
            this.display_foreground = displayForeground;
            return this;
        }
 
 
        @Override
        public AndroidNotification build() {
            return new AndroidNotification(
                    alert,
                    title,
                    builderId,
                    channelId,
                    priority,
                    category,
                    style,
                    alert_type,
                    big_text,
                    inbox,
                    big_pic_path,
                    large_icon,
                    small_icon_uri,
                    intent,
                    uri_activity,
                    uri_action,
                    badge_add_num,
                    badge_class,
                    sound,
                    show_begin_time,
                    show_end_time,
                    display_foreground,
                    extrasBuilder,
                    numberExtrasBuilder,
                    booleanExtrasBuilder,
                    jsonExtrasBuilder,
                    super.customData
            );
        }
    }
}