Skip to content

feat(fcm): Add 12 new android notification params support #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
285 changes: 285 additions & 0 deletions src/main/java/com/google/firebase/messaging/AndroidNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
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.concurrent.TimeUnit;

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

@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;

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;
Expand Down Expand Up @@ -101,6 +151,49 @@ private AndroidNotification(Builder builder) {
}
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;
}
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,
}

/**
Expand All @@ -127,6 +220,18 @@ public static class Builder {
private List<String> titleLocArgs = new ArrayList<>();
private String channelId;
private String image;
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 Integer notificationCount;

private Builder() {}

Expand Down Expand Up @@ -309,6 +414,186 @@ public Builder setImage(String 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 be formated 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) {
this.eventTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'")
.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. 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 The notification count
* @return This builder.
*/
public Builder setNotificationCount(int notificationCount) {
this.notificationCount = notificationCount;
return this;
}

/**
* Creates a new {@link AndroidNotification} instance from the parameters set on this builder.
*
Expand Down
Loading