forked from firebase/firebase-admin-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndroidNotification.java
More file actions
644 lines (574 loc) · 20.3 KB
/
AndroidNotification.java
File metadata and controls
644 lines (574 loc) · 20.3 KB
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.firebase.messaging;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.api.client.util.Key;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.firebase.internal.NonNull;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
/**
* Represents the Android-specific notification options that can be included in a {@link Message}.
* Instances of this class are thread-safe and immutable.
*/
public class AndroidNotification {
@Key("title")
private final String title;
@Key("body")
private final String body;
@Key("icon")
private final String icon;
@Key("color")
private final String color;
@Key("sound")
private final String sound;
@Key("tag")
private final String tag;
@Key("click_action")
private final String clickAction;
@Key("body_loc_key")
private final String bodyLocKey;
@Key("body_loc_args")
private final List<String> bodyLocArgs;
@Key("title_loc_key")
private final String titleLocKey;
@Key("title_loc_args")
private final List<String> titleLocArgs;
@Key("channel_id")
private final String channelId;
@Key("image")
private final String image;
@Key("ticker")
private final String ticker;
@Key("sticky")
private final Boolean sticky;
@Key("event_time")
private final String eventTime;
@Key("local_only")
private final Boolean localOnly;
@Key("notification_priority")
private final String priority;
@Key("vibrate_timings")
private final List<String> vibrateTimings;
@Key("default_vibrate_timings")
private final Boolean defaultVibrateTimings;
@Key("default_sound")
private final Boolean defaultSound;
@Key("light_settings")
private final LightSettings lightSettings;
@Key("default_light_settings")
private final Boolean defaultLightSettings;
@Key("visibility")
private final String visibility;
@Key("notification_count")
private final Integer notificationCount;
@Key("proxy")
private final String proxy;
private static final Map<Priority, String> PRIORITY_MAP =
ImmutableMap.<Priority, String>builder()
.put(Priority.MIN, "PRIORITY_MIN")
.put(Priority.LOW, "PRIORITY_LOW")
.put(Priority.DEFAULT, "PRIORITY_DEFAULT")
.put(Priority.HIGH, "PRIORITY_HIGH")
.put(Priority.MAX, "PRIORITY_MAX")
.build();
private AndroidNotification(Builder builder) {
this.title = builder.title;
this.body = builder.body;
this.icon = builder.icon;
if (builder.color != null) {
checkArgument(builder.color.matches("^#[0-9a-fA-F]{6}$"),
"color must be in the form #RRGGBB");
}
this.color = builder.color;
this.sound = builder.sound;
this.tag = builder.tag;
this.clickAction = builder.clickAction;
this.bodyLocKey = builder.bodyLocKey;
if (!builder.bodyLocArgs.isEmpty()) {
checkArgument(!Strings.isNullOrEmpty(builder.bodyLocKey),
"bodyLocKey is required when specifying bodyLocArgs");
this.bodyLocArgs = ImmutableList.copyOf(builder.bodyLocArgs);
} else {
this.bodyLocArgs = null;
}
this.titleLocKey = builder.titleLocKey;
if (!builder.titleLocArgs.isEmpty()) {
checkArgument(!Strings.isNullOrEmpty(builder.titleLocKey),
"titleLocKey is required when specifying titleLocArgs");
this.titleLocArgs = ImmutableList.copyOf(builder.titleLocArgs);
} else {
this.titleLocArgs = null;
}
this.channelId = builder.channelId;
this.image = builder.image;
this.ticker = builder.ticker;
this.sticky = builder.sticky;
this.eventTime = builder.eventTime;
this.localOnly = builder.localOnly;
if (builder.priority != null) {
this.priority = builder.priority.toString();
} else {
this.priority = null;
}
if (!builder.vibrateTimings.isEmpty()) {
this.vibrateTimings = ImmutableList.copyOf(builder.vibrateTimings);
} else {
this.vibrateTimings = null;
}
this.defaultVibrateTimings = builder.defaultVibrateTimings;
this.defaultSound = builder.defaultSound;
this.lightSettings = builder.lightSettings;
this.defaultLightSettings = builder.defaultLightSettings;
if (builder.visibility != null) {
this.visibility = builder.visibility.name().toLowerCase();
} else {
this.visibility = null;
}
if (builder.notificationCount != null) {
checkArgument(builder.notificationCount >= 0,
"notificationCount if specified must be zero or positive valued");
}
if (builder.proxy != null) {
this.proxy = builder.proxy.name();
} else {
this.proxy = null;
}
this.notificationCount = builder.notificationCount;
}
public enum Priority {
MIN,
LOW,
DEFAULT,
HIGH,
MAX;
@Override
public String toString() {
return PRIORITY_MAP.get(this);
}
}
public enum Visibility {
PRIVATE,
PUBLIC,
SECRET,
}
public enum Proxy {
PROXY_UNSPECIFIED,
ALLOW,
DENY,
IF_PRIORITY_LOWERED
}
/**
* Creates a new {@link AndroidNotification.Builder}.
*
* @return A {@link AndroidNotification.Builder} instance.
*/
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String title;
private String body;
private String icon;
private String color;
private String sound;
private String tag;
private String clickAction;
private String bodyLocKey;
private List<String> bodyLocArgs = new ArrayList<>();
private String titleLocKey;
private List<String> titleLocArgs = new ArrayList<>();
private String channelId;
private String image;
private Integer notificationCount;
private String ticker;
private Boolean sticky;
private String eventTime;
private Boolean localOnly;
private Priority priority;
private List<String> vibrateTimings = new ArrayList<>();
private Boolean defaultVibrateTimings;
private Boolean defaultSound;
private LightSettings lightSettings;
private Boolean defaultLightSettings;
private Visibility visibility;
private Proxy proxy;
private Builder() {}
/**
* Sets the title of the Android notification. When provided, overrides the title set
* via {@link Notification}.
*
* @param title Title of the notification.
* @return This builder.
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
* Sets the body of the Android notification. When provided, overrides the body set
* via {@link Notification}.
*
* @param body Body of the notification.
* @return This builder.
*/
public Builder setBody(String body) {
this.body = body;
return this;
}
/**
* Sets the icon of the Android notification.
*
* @param icon Icon resource for the notification.
* @return This builder.
*/
public Builder setIcon(String icon) {
this.icon = icon;
return this;
}
/**
* Sets the notification icon color.
*
* @param color Color specified in the {@code #rrggbb} format.
* @return This builder.
*/
public Builder setColor(String color) {
this.color = color;
return this;
}
/**
* Sets the sound to be played when the device receives the notification.
*
* @param sound File name of the sound resource or "default".
* @return This builder.
*/
public Builder setSound(String sound) {
this.sound = sound;
return this;
}
/**
* Sets the notification tag. This is an identifier used to replace existing notifications in
* the notification drawer. If not specified, each request creates a new notification.
*
* @param tag Notification tag.
* @return This builder.
*/
public Builder setTag(String tag) {
this.tag = tag;
return this;
}
/**
* Sets the action associated with a user click on the notification. If specified, an activity
* with a matching Intent Filter is launched when a user clicks on the notification.
*
* @param clickAction Click action name.
* @return This builder.
*/
public Builder setClickAction(String clickAction) {
this.clickAction = clickAction;
return this;
}
/**
* Sets the key of the body string in the app's string resources to use to localize the body
* text.
*
* @param bodyLocKey Resource key string.
* @return This builder.
*/
public Builder setBodyLocalizationKey(String bodyLocKey) {
this.bodyLocKey = bodyLocKey;
return this;
}
/**
* Adds a resource key string that will be used in place of the format specifiers in
* {@code bodyLocKey}.
*
* @param arg Resource key string.
* @return This builder.
*/
public Builder addBodyLocalizationArg(@NonNull String arg) {
this.bodyLocArgs.add(arg);
return this;
}
/**
* Adds a list of resource keys that will be used in place of the format specifiers in
* {@code bodyLocKey}.
*
* @param args List of resource key strings.
* @return This builder.
*/
public Builder addAllBodyLocalizationArgs(@NonNull List<String> args) {
this.bodyLocArgs.addAll(args);
return this;
}
/**
* Sets the key of the title string in the app's string resources to use to localize the title
* text.
*
* @param titleLocKey Resource key string.
* @return This builder.
*/
public Builder setTitleLocalizationKey(String titleLocKey) {
this.titleLocKey = titleLocKey;
return this;
}
/**
* Adds a resource key string that will be used in place of the format specifiers in
* {@code titleLocKey}.
*
* @param arg Resource key string.
* @return This builder.
*/
public Builder addTitleLocalizationArg(@NonNull String arg) {
this.titleLocArgs.add(arg);
return this;
}
/**
* Adds a list of resource keys that will be used in place of the format specifiers in
* {@code titleLocKey}.
*
* @param args List of resource key strings.
* @return This builder.
*/
public Builder addAllTitleLocalizationArgs(@NonNull List<String> args) {
this.titleLocArgs.addAll(args);
return this;
}
/**
* Sets the Android notification channel ID (new in Android O). The app must create a channel
* with this channel ID before any notification with this channel ID is received. If you
* don't send this channel ID in the request, or if the channel ID provided has not yet been
* created by the app, FCM uses the channel ID specified in the app manifest.
*
* @param channelId The notification's channel ID.
* @return This builder.
*/
public Builder setChannelId(String channelId) {
this.channelId = channelId;
return this;
}
/**
* Sets the URL of the image that is going to be displayed in the notification. When provided,
* overrides the imageUrl set via {@link Notification}.
*
* @param imageUrl URL of the image that is going to be displayed in the notification.
* @return This builder.
*/
public Builder setImage(String imageUrl) {
this.image = imageUrl;
return this;
}
/**
* Sets the "ticker" text, which is sent to accessibility services. Prior to API level 21
* (Lollipop), sets the text that is displayed in the status bar when the notification
* first arrives.
*
* @param ticker Ticker name.
* @return This builder.
*/
public Builder setTicker(String ticker) {
this.ticker = ticker;
return this;
}
/**
* Sets the sticky flag. When set to false or unset, the notification is automatically
* dismissed when the user clicks it in the panel. When set to true, the notification
* persists even when the user clicks it.
*
* @param sticky The sticky flag
* @return This builder.
*/
public Builder setSticky(boolean sticky) {
this.sticky = sticky;
return this;
}
/**
* For notifications that inform users about events with an absolute time reference, sets
* the time that the event in the notification occurred in milliseconds. Notifications
* in the panel are sorted by this time. The time is formatted in RFC3339 UTC "Zulu"
* format, accurate to nanoseconds. Example: "2014-10-02T15:01:23.045123456Z". Note that
* since the time is in milliseconds, the last section of the time representation always
* has 6 leading zeros.
*
* @param eventTimeInMillis The event time in milliseconds
* @return This builder.
*/
public Builder setEventTimeInMillis(long eventTimeInMillis) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS000000'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
this.eventTime = dateFormat.format(new Date(eventTimeInMillis));
return this;
}
/**
* Sets whether or not this notification is relevant only to the current device. Some
* notifications can be bridged to other devices for remote display, such as a Wear
* OS watch. This hint can be set to recommend this notification not be bridged.
*
* @param localOnly The "local only" flag
* @return This builder.
*/
public Builder setLocalOnly(boolean localOnly) {
this.localOnly = localOnly;
return this;
}
/**
* Sets the relative priority for this notification. Priority is an indication of how much of
* the user's attention should be consumed by this notification. Low-priority notifications
* may be hidden from the user in certain situations, while the user might be interrupted
* for a higher-priority notification.
*
* @param priority The priority value, one of the values in {MIN, LOW, DEFAULT, HIGH, MAX}
* @return This builder.
*/
public Builder setPriority(Priority priority) {
this.priority = priority;
return this;
}
/**
* Sets a list of vibration timings in milliseconds in the array to use. The first value in the
* array indicates the duration to wait before turning the vibrator on. The next value
* indicates the duration to keep the vibrator on. Subsequent values alternate between
* duration to turn the vibrator off and to turn the vibrator on. If {@code vibrate_timings}
* is set and {@code default_vibrate_timings} is set to true, the default value is used instead
* of the user-specified {@code vibrate_timings}.
* A duration in seconds with up to nine fractional digits, terminated by 's'. Example: "3.5s".
*
* @param vibrateTimingsInMillis List of vibration time in milliseconds
* @return This builder.
*/
public Builder setVibrateTimingsInMillis(long[] vibrateTimingsInMillis) {
List<String> list = new ArrayList<>();
for (long value : vibrateTimingsInMillis) {
checkArgument(value >= 0, "elements in vibrateTimingsInMillis must not be negative");
long seconds = TimeUnit.MILLISECONDS.toSeconds(value);
long subsecondNanos = TimeUnit.MILLISECONDS.toNanos(value - seconds * 1000L);
if (subsecondNanos > 0) {
list.add(String.format("%d.%09ds", seconds, subsecondNanos));
} else {
list.add(String.format("%ds", seconds));
}
}
this.vibrateTimings = ImmutableList.copyOf(list);
return this;
}
/**
* Sets the whether to use the default vibration timings. If set to true, use the Android
* framework's default vibrate pattern for the notification. Default values are specified
* in {@code config.xml}. If {@code default_vibrate_timings} is set to true and
* {@code vibrate_timings} is also set, the default value is used instead of the
* user-specified {@code vibrate_timings}.
*
* @param defaultVibrateTimings The flag indicating whether to use the default vibration timings
* @return This builder.
*/
public Builder setDefaultVibrateTimings(boolean defaultVibrateTimings) {
this.defaultVibrateTimings = defaultVibrateTimings;
return this;
}
/**
* Sets the whether to use the default sound. If set to true, use the Android framework's
* default sound for the notification. Default values are specified in config.xml.
*
* @param defaultSound The flag indicating whether to use the default sound
* @return This builder.
*/
public Builder setDefaultSound(boolean defaultSound) {
this.defaultSound = defaultSound;
return this;
}
/**
* Sets the settings to control the notification's LED blinking rate and color if LED is
* available on the device. The total blinking time is controlled by the OS.
*
* @param lightSettings The light settings to use
* @return This builder.
*/
public Builder setLightSettings(LightSettings lightSettings) {
this.lightSettings = lightSettings;
return this;
}
/**
* Sets the whether to use the default light settings. If set to true, use the Android
* framework's default LED light settings for the notification. Default values are
* specified in config.xml. If {@code default_light_settings} is set to true and
* {@code light_settings} is also set, the user-specified {@code light_settings} is used
* instead of the default value.
*
* @param defaultLightSettings The flag indicating whether to use the default light
* settings
* @return This builder.
*/
public Builder setDefaultLightSettings(boolean defaultLightSettings) {
this.defaultLightSettings = defaultLightSettings;
return this;
}
/**
* Sets the visibility of this notification.
*
* @param visibility The visibility value. one of the values in {PRIVATE, PUBLIC, SECRET}
* @return This builder.
*/
public Builder setVisibility(Visibility visibility) {
this.visibility = visibility;
return this;
}
/**
* Sets the number of items this notification represents. May be displayed as a badge
* count for launchers that support badging.
* If not invoked then notification count is left unchanged.
* For example, this might be useful if you're using just one notification to represent
* multiple new messages but you want the count here to represent the number of total
* new messages. If zero or unspecified, systems that support badging use the default,
* which is to increment a number displayed on
* the long-press menu each time a new notification arrives.
*
* @param notificationCount Zero or positive value. Zero indicates leave unchanged.
* @return This builder.
*/
public Builder setNotificationCount(int notificationCount) {
this.notificationCount = notificationCount;
return this;
}
/**
* Sets the proxy of this notification.
*
* @param proxy The proxy value. one of the values in
* {PROXY_UNSPECIFIED, ALLOW, DENY, IF_PRIORITY_LOWERED}
*
* @return This builder.
*/
public Builder setProxy(Proxy proxy) {
this.proxy = proxy;
return this;
}
/**
* Creates a new {@link AndroidNotification} instance from the parameters set on this builder.
*
* @return A new {@link AndroidNotification} instance.
* @throws IllegalArgumentException If any of the parameters set on the builder are invalid.
*/
public AndroidNotification build() {
return new AndroidNotification(this);
}
}
}