Skip to content

Commit a8f5ba8

Browse files
authored
feat(fcm): Add 12 new android notification params support (#320)
* Add 12 new parameter support for Android Notifications * Add LightSettings class * Add LightSettingsColor class
1 parent c33579d commit a8f5ba8

File tree

4 files changed

+568
-0
lines changed

4 files changed

+568
-0
lines changed

src/main/java/com/google/firebase/messaging/AndroidNotification.java

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
import com.google.api.client.util.Key;
2222
import com.google.common.base.Strings;
2323
import com.google.common.collect.ImmutableList;
24+
import com.google.common.collect.ImmutableMap;
2425
import com.google.firebase.internal.NonNull;
26+
import java.text.SimpleDateFormat;
2527
import java.util.ArrayList;
28+
import java.util.Date;
2629
import java.util.List;
30+
import java.util.Map;
31+
import java.util.concurrent.TimeUnit;
2732

2833
/**
2934
* Represents the Android-specific notification options that can be included in a {@link Message}.
@@ -69,6 +74,51 @@ public class AndroidNotification {
6974

7075
@Key("image")
7176
private final String image;
77+
78+
@Key("ticker")
79+
private final String ticker;
80+
81+
@Key("sticky")
82+
private final Boolean sticky;
83+
84+
@Key("event_time")
85+
private final String eventTime;
86+
87+
@Key("local_only")
88+
private final Boolean localOnly;
89+
90+
@Key("notification_priority")
91+
private final String priority;
92+
93+
@Key("vibrate_timings")
94+
private final List<String> vibrateTimings;
95+
96+
@Key("default_vibrate_timings")
97+
private final Boolean defaultVibrateTimings;
98+
99+
@Key("default_sound")
100+
private final Boolean defaultSound;
101+
102+
@Key("light_settings")
103+
private final LightSettings lightSettings;
104+
105+
@Key("default_light_settings")
106+
private final Boolean defaultLightSettings;
107+
108+
@Key("visibility")
109+
private final String visibility;
110+
111+
@Key("notification_count")
112+
private final Integer notificationCount;
113+
114+
private static final Map<Priority, String> PRIORITY_MAP =
115+
ImmutableMap.<Priority, String>builder()
116+
.put(Priority.MIN, "PRIORITY_MIN")
117+
.put(Priority.LOW, "PRIORITY_LOW")
118+
.put(Priority.DEFAULT, "PRIORITY_DEFAULT")
119+
.put(Priority.HIGH, "PRIORITY_HIGH")
120+
.put(Priority.MAX, "PRIORITY_MAX")
121+
.build();
72122

73123
private AndroidNotification(Builder builder) {
74124
this.title = builder.title;
@@ -101,6 +151,49 @@ private AndroidNotification(Builder builder) {
101151
}
102152
this.channelId = builder.channelId;
103153
this.image = builder.image;
154+
this.ticker = builder.ticker;
155+
this.sticky = builder.sticky;
156+
this.eventTime = builder.eventTime;
157+
this.localOnly = builder.localOnly;
158+
if (builder.priority != null) {
159+
this.priority = builder.priority.toString();
160+
} else {
161+
this.priority = null;
162+
}
163+
if (!builder.vibrateTimings.isEmpty()) {
164+
this.vibrateTimings = ImmutableList.copyOf(builder.vibrateTimings);
165+
} else {
166+
this.vibrateTimings = null;
167+
}
168+
this.defaultVibrateTimings = builder.defaultVibrateTimings;
169+
this.defaultSound = builder.defaultSound;
170+
this.lightSettings = builder.lightSettings;
171+
this.defaultLightSettings = builder.defaultLightSettings;
172+
if (builder.visibility != null) {
173+
this.visibility = builder.visibility.name().toLowerCase();
174+
} else {
175+
this.visibility = null;
176+
}
177+
this.notificationCount = builder.notificationCount;
178+
}
179+
180+
public enum Priority {
181+
MIN,
182+
LOW,
183+
DEFAULT,
184+
HIGH,
185+
MAX;
186+
187+
@Override
188+
public String toString() {
189+
return PRIORITY_MAP.get(this);
190+
}
191+
}
192+
193+
public enum Visibility {
194+
PRIVATE,
195+
PUBLIC,
196+
SECRET,
104197
}
105198

106199
/**
@@ -127,6 +220,18 @@ public static class Builder {
127220
private List<String> titleLocArgs = new ArrayList<>();
128221
private String channelId;
129222
private String image;
223+
private String ticker;
224+
private Boolean sticky;
225+
private String eventTime;
226+
private Boolean localOnly;
227+
private Priority priority;
228+
private List<String> vibrateTimings = new ArrayList<>();
229+
private Boolean defaultVibrateTimings;
230+
private Boolean defaultSound;
231+
private LightSettings lightSettings;
232+
private Boolean defaultLightSettings;
233+
private Visibility visibility;
234+
private Integer notificationCount;
130235

131236
private Builder() {}
132237

@@ -309,6 +414,186 @@ public Builder setImage(String imageUrl) {
309414
return this;
310415
}
311416

417+
/**
418+
* Sets the "ticker" text, which is sent to accessibility services. Prior to API level 21
419+
* (Lollipop), sets the text that is displayed in the status bar when the notification
420+
* first arrives.
421+
*
422+
* @param ticker Ticker name.
423+
* @return This builder.
424+
*/
425+
public Builder setTicker(String ticker) {
426+
this.ticker = ticker;
427+
return this;
428+
}
429+
430+
/**
431+
* Sets the sticky flag. When set to false or unset, the notification is automatically
432+
* dismissed when the user clicks it in the panel. When set to true, the notification
433+
* persists even when the user clicks it.
434+
*
435+
* @param sticky The sticky flag
436+
* @return This builder.
437+
*/
438+
public Builder setSticky(boolean sticky) {
439+
this.sticky = sticky;
440+
return this;
441+
}
442+
443+
/**
444+
* For notifications that inform users about events with an absolute time reference, sets
445+
* the time that the event in the notification occurred in milliseconds. Notifications
446+
* in the panel are sorted by this time. The time is be formated in RFC3339 UTC "Zulu"
447+
* format, accurate to nanoseconds. Example: "2014-10-02T15:01:23.045123456Z". Note that
448+
* since the time is in milliseconds, the last section of the time representation always
449+
* has 6 leading zeros.
450+
*
451+
* @param eventTimeInMillis The event time in milliseconds
452+
* @return This builder.
453+
*/
454+
public Builder setEventTimeInMillis(long eventTimeInMillis) {
455+
this.eventTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'")
456+
.format(new Date(eventTimeInMillis));
457+
return this;
458+
}
459+
460+
/**
461+
* Sets whether or not this notification is relevant only to the current device. Some
462+
* notifications can be bridged to other devices for remote display, such as a Wear
463+
* OS watch. This hint can be set to recommend this notification not be bridged.
464+
*
465+
* @param localOnly The "local only" flag
466+
* @return This builder.
467+
*/
468+
public Builder setLocalOnly(boolean localOnly) {
469+
this.localOnly = localOnly;
470+
return this;
471+
}
472+
473+
/**
474+
* Sets the relative priority for this notification. Priority is an indication of how much of
475+
* the user's attention should be consumed by this notification. Low-priority notifications
476+
* may be hidden from the user in certain situations, while the user might be interrupted
477+
* for a higher-priority notification.
478+
*
479+
* @param priority The priority value, one of the values in {MIN, LOW, DEFAULT, HIGH, MAX}
480+
* @return This builder.
481+
*/
482+
public Builder setPriority(Priority priority) {
483+
this.priority = priority;
484+
return this;
485+
}
486+
487+
/**
488+
* Sets a list of vibration timings in milliseconds in the array to use. The first value in the
489+
* array indicates the duration to wait before turning the vibrator on. The next value
490+
* indicates the duration to keep the vibrator on. Subsequent values alternate between
491+
* duration to turn the vibrator off and to turn the vibrator on. If {@code vibrate_timings}
492+
* is set and {@code default_vibrate_timings} is set to true, the default value is used instead
493+
* of the user-specified {@code vibrate_timings}.
494+
* A duration in seconds with up to nine fractional digits, terminated by 's'. Example: "3.5s".
495+
*
496+
* @param vibrateTimingsInMillis List of vibration time in milliseconds
497+
* @return This builder.
498+
*/
499+
public Builder setVibrateTimingsInMillis(long[] vibrateTimingsInMillis) {
500+
List<String> list = new ArrayList<>();
501+
for (long value : vibrateTimingsInMillis) {
502+
checkArgument(value >= 0, "elements in vibrateTimingsInMillis must not be negative");
503+
long seconds = TimeUnit.MILLISECONDS.toSeconds(value);
504+
long subsecondNanos = TimeUnit.MILLISECONDS.toNanos(value - seconds * 1000L);
505+
if (subsecondNanos > 0) {
506+
list.add(String.format("%d.%09ds", seconds, subsecondNanos));
507+
} else {
508+
list.add(String.format("%ds", seconds));
509+
}
510+
}
511+
this.vibrateTimings = ImmutableList.copyOf(list);
512+
return this;
513+
}
514+
515+
/**
516+
* Sets the whether to use the default vibration timings. If set to true, use the Android
517+
* framework's default vibrate pattern for the notification. Default values are specified
518+
* in {@code config.xml}. If {@code default_vibrate_timings} is set to true and
519+
* {@code vibrate_timings} is also set, the default value is used instead of the
520+
* user-specified {@code vibrate_timings}.
521+
*
522+
* @param defaultVibrateTimings The flag indicating whether to use the default vibration timings
523+
* @return This builder.
524+
*/
525+
public Builder setDefaultVibrateTimings(boolean defaultVibrateTimings) {
526+
this.defaultVibrateTimings = defaultVibrateTimings;
527+
return this;
528+
}
529+
530+
/**
531+
* Sets the whether to use the default sound. If set to true, use the Android framework's
532+
* default sound for the notification. Default values are specified in config.xml.
533+
*
534+
* @param defaultSound The flag indicating whether to use the default sound
535+
* @return This builder.
536+
*/
537+
public Builder setDefaultSound(boolean defaultSound) {
538+
this.defaultSound = defaultSound;
539+
return this;
540+
}
541+
542+
/**
543+
* Sets the settings to control the notification's LED blinking rate and color if LED is
544+
* available on the device. The total blinking time is controlled by the OS.
545+
*
546+
* @param lightSettings The light settings to use
547+
* @return This builder.
548+
*/
549+
public Builder setLightSettings(LightSettings lightSettings) {
550+
this.lightSettings = lightSettings;
551+
return this;
552+
}
553+
554+
/**
555+
* Sets the whether to use the default light settings. If set to true, use the Android
556+
* framework's default LED light settings for the notification. Default values are
557+
* specified in config.xml. If {@code default_light_settings} is set to true and
558+
* {@code light_settings} is also set, the user-specified {@code light_settings} is used
559+
* instead of the default value.
560+
*
561+
* @param defaultLightSettings The flag indicating whether to use the default light
562+
* settings
563+
* @return This builder.
564+
*/
565+
public Builder setDefaultLightSettings(boolean defaultLightSettings) {
566+
this.defaultLightSettings = defaultLightSettings;
567+
return this;
568+
}
569+
570+
/**
571+
* Sets the visibility of this notification.
572+
*
573+
* @param visibility The visibility value. one of the values in {PRIVATE, PUBLIC, SECRET}
574+
* @return This builder.
575+
*/
576+
public Builder setVisibility(Visibility visibility) {
577+
this.visibility = visibility;
578+
return this;
579+
}
580+
581+
/**
582+
* Sets the number of items this notification represents. May be displayed as a badge
583+
* count for launchers that support badging. For example, this might be useful if you're
584+
* using just one notification to represent multiple new messages but you want the count
585+
* here to represent the number of total new messages. If zero or unspecified, systems
586+
* that support badging use the default, which is to increment a number displayed on
587+
* the long-press menu each time a new notification arrives.
588+
*
589+
* @param notificationCount The notification count
590+
* @return This builder.
591+
*/
592+
public Builder setNotificationCount(int notificationCount) {
593+
this.notificationCount = notificationCount;
594+
return this;
595+
}
596+
312597
/**
313598
* Creates a new {@link AndroidNotification} instance from the parameters set on this builder.
314599
*

0 commit comments

Comments
 (0)