From 484bee6be6eb1fda54f88856793ebdc7410d304b Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Fri, 13 Jul 2018 15:33:03 -0700 Subject: [PATCH 1/7] Added webpush notification fields --- .../messaging/WebpushNotification.java | 211 +++++++++++++++++- .../messaging/FirebaseMessagingTest.java | 58 ++++- 2 files changed, 262 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/google/firebase/messaging/WebpushNotification.java b/src/main/java/com/google/firebase/messaging/WebpushNotification.java index 92876c562..e6091fc0f 100644 --- a/src/main/java/com/google/firebase/messaging/WebpushNotification.java +++ b/src/main/java/com/google/firebase/messaging/WebpushNotification.java @@ -16,24 +16,66 @@ 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.firebase.internal.NonNull; import com.google.firebase.internal.Nullable; +import java.util.ArrayList; +import java.util.List; /** * Represents the Webpush-specific notification options that can be included in a {@link Message}. - * Instances of this class are thread-safe and immutable. + * Instances of this class are thread-safe and immutable. Supports most standard options defined + * in the Web + * Notification specification. */ public class WebpushNotification { - @Key("title") - private final String title; + @Key("actions") + private final List actions; + + @Key("badge") + private final String badge; @Key("body") private final String body; + @Key("dir") + private final String direction; + @Key("icon") private final String icon; + @Key("image") + private final String image; + + @Key("lang") + private final String language; + + @Key("renotify") + private final Boolean renotify; + + @Key("requireInteraction") + private final Boolean requireInteraction; + + @Key("silent") + private final Boolean silent; + + @Key("tag") + private final String tag; + + @Key("timestamp") + private final Long timestamp; + + @Key("title") + private final String title; + + @Key("vibrate") + private final List vibrate; + /** * Creates a new notification with the given title and body. Overrides the options set via * {@link Notification}. @@ -54,8 +96,165 @@ public WebpushNotification(String title, String body) { * @param icon URL to the notifications icon. */ public WebpushNotification(String title, String body, @Nullable String icon) { - this.title = title; - this.body = body; - this.icon = icon; + this(builder().setTitle(title).setBody(body).setIcon(icon)); + } + + private WebpushNotification(Builder builder) { + this.actions = !builder.actions.isEmpty() ? ImmutableList.copyOf(builder.actions) : null; + this.badge = builder.badge; + this.body = builder.body; + this.direction = builder.direction != null ? builder.direction.value : null; + this.icon = builder.icon; + this.image = builder.image; + this.language = builder.language; + this.renotify = builder.renotify; + this.requireInteraction = builder.requireInteraction; + this.silent = builder.silent; + this.tag = builder.tag; + this.timestamp = builder.timestamp; + this.title = builder.title; + this.vibrate = builder.vibrate; + } + + public static Builder builder() { + return new Builder(); + } + + public enum Direction { + AUTO("auto"), + LEFT_TO_RIGHT("ltr"), + RIGHT_TO_LEFT("rtl"); + + final String value; + + Direction(String value) { + this.value = value; + } + } + + public static class Action { + @Key("action") + private final String action; + + @Key("title") + private final String title; + + @Key("icon") + private final String icon; + + public Action(String action, String title) { + this(action, title, null); + } + + public Action(String action, String title, @Nullable String icon) { + checkArgument(!Strings.isNullOrEmpty(action)); + checkArgument(!Strings.isNullOrEmpty(title)); + this.action = action; + this.title = title; + this.icon = icon; + } + } + + public static class Builder { + + private final List actions = new ArrayList<>(); + private String badge; + private String body; + private Direction direction; + private String icon; + private String image; + private String language; + private Boolean renotify; + private Boolean requireInteraction; + private Boolean silent; + private String tag; + private Long timestamp; + private String title; + private List vibrate; + + private Builder() {} + + public Builder setTitle(String title) { + this.title = title; + return this; + } + + public Builder setBody(String body) { + this.body = body; + return this; + } + + public Builder setIcon(String icon) { + this.icon = icon; + return this; + } + + public Builder setBadge(String badge) { + this.badge = badge; + return this; + } + + public Builder setImage(String image) { + this.image = image; + return this; + } + + public Builder setLanguage(String language) { + this.language = language; + return this; + } + + public Builder setRenotify(boolean renotify) { + this.renotify = renotify; + return this; + } + + public Builder setRequireInteraction(boolean requireInteraction) { + this.requireInteraction = requireInteraction; + return this; + } + + public Builder setSilent(boolean silent) { + this.silent = silent; + return this; + } + + public Builder setTag(String tag) { + this.tag = tag; + return this; + } + + public Builder setTimestamp(long timestamp) { + this.timestamp = timestamp; + return this; + } + + public Builder setVibrate(int[] pattern) { + List list = new ArrayList<>(); + for (int value : pattern) { + list.add(value); + } + this.vibrate = ImmutableList.copyOf(list); + return this; + } + + public Builder setDirection(Direction direction) { + this.direction = direction; + return this; + } + + public Builder addAction(@NonNull Action action) { + this.actions.add(action); + return this; + } + + public Builder addAllActions(@NonNull List actions) { + this.actions.addAll(actions); + return this; + } + + public WebpushNotification build() { + return new WebpushNotification(this); + } } } diff --git a/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java b/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java index d36db08c0..986d2982b 100644 --- a/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java +++ b/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java @@ -35,6 +35,8 @@ import com.google.firebase.FirebaseOptions; import com.google.firebase.TestOnlyImplFirebaseTrampolines; import com.google.firebase.auth.MockGoogleCredentials; +import com.google.firebase.messaging.WebpushNotification.Action; +import com.google.firebase.messaging.WebpushNotification.Direction; import com.google.firebase.testing.GenericFunction; import com.google.firebase.testing.TestResponseInterceptor; import java.io.ByteArrayOutputStream; @@ -657,7 +659,7 @@ private static Map> buildTestMessages() { "title", "test-title", "body", "test-body")))) )); - // Webpush message + // Webpush message (simple) builder.put( Message.builder() .setWebpushConfig(WebpushConfig.builder() @@ -678,6 +680,60 @@ private static Map> buildTestMessages() { "title", "test-title", "body", "test-body", "icon", "test-icon")) )); + // Webpush message (all fields) + builder.put( + Message.builder() + .setWebpushConfig(WebpushConfig.builder() + .putHeader("h1", "v1") + .putAllHeaders(ImmutableMap.of("h2", "v2", "h3", "v3")) + .putData("k1", "v1") + .putAllData(ImmutableMap.of("k2", "v2", "k3", "v3")) + .setNotification(WebpushNotification.builder() + .setTitle("test-title") + .setBody("test-body") + .setIcon("test-icon") + .setBadge("test-badge") + .setImage("test-image") + .setLanguage("test-lang") + .setTag("test-tag") + .setDirection(Direction.AUTO) + .setRenotify(true) + .setRequireInteraction(false) + .setSilent(true) + .setTimestamp(100L) + .setVibrate(new int[]{200, 100, 200}) + .addAction(new Action("action1", "title1")) + .addAllActions(ImmutableList.of(new Action("action2", "title2", "icon2"))) + .build()) + .build()) + .setTopic("test-topic") + .build(), + ImmutableMap.of( + "topic", "test-topic", + "webpush", ImmutableMap.of( + "headers", ImmutableMap.of("h1", "v1", "h2", "v2", "h3", "v3"), + "data", ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"), + "notification", ImmutableMap.builder() + .put("title", "test-title") + .put("body", "test-body") + .put("icon", "test-icon") + .put("badge", "test-badge") + .put("image", "test-image") + .put("lang", "test-lang") + .put("tag", "test-tag") + .put("renotify", true) + .put("requireInteraction", false) + .put("silent", true) + .put("dir", "auto") + .put("timestamp", new BigDecimal(100)) + .put("vibrate", ImmutableList.of( + new BigDecimal(200), new BigDecimal(100), new BigDecimal(200))) + .put("actions", ImmutableList.of( + ImmutableMap.of("action", "action1", "title", "title1"), + ImmutableMap.of("action", "action2", "title", "title2", "icon", "icon2"))) + .build()) + )); + return builder.build(); } } From efa81998aa5ca4df7b1d35243a41678915cdcbf5 Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Fri, 13 Jul 2018 16:13:08 -0700 Subject: [PATCH 2/7] Added documentation --- .../messaging/WebpushNotification.java | 180 ++++++++++++++++-- .../messaging/FirebaseMessagingTest.java | 2 + 2 files changed, 161 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/google/firebase/messaging/WebpushNotification.java b/src/main/java/com/google/firebase/messaging/WebpushNotification.java index e6091fc0f..69d94f35c 100644 --- a/src/main/java/com/google/firebase/messaging/WebpushNotification.java +++ b/src/main/java/com/google/firebase/messaging/WebpushNotification.java @@ -43,6 +43,9 @@ public class WebpushNotification { @Key("body") private final String body; + @Key("data") + private final Object data; + @Key("dir") private final String direction; @@ -103,6 +106,7 @@ private WebpushNotification(Builder builder) { this.actions = !builder.actions.isEmpty() ? ImmutableList.copyOf(builder.actions) : null; this.badge = builder.badge; this.body = builder.body; + this.data = builder.data; this.direction = builder.direction != null ? builder.direction.value : null; this.icon = builder.icon; this.image = builder.image; @@ -116,10 +120,18 @@ private WebpushNotification(Builder builder) { this.vibrate = builder.vibrate; } + /** + * Creates a new {@link WebpushNotification.Builder}. + * + * @return A {@link WebpushNotification.Builder} instance. + */ public static Builder builder() { return new Builder(); } + /** + * Different directions a notification can be displayed in. + */ public enum Direction { AUTO("auto"), LEFT_TO_RIGHT("ltr"), @@ -132,6 +144,9 @@ public enum Direction { } } + /** + * Represents an action available to the users when the notification is presented. + */ public static class Action { @Key("action") private final String action; @@ -142,10 +157,23 @@ public static class Action { @Key("icon") private final String icon; + /** + * Creates a new Action with the given action string and title. + * + * @param action Action string. + * @param title Title text. + */ public Action(String action, String title) { this(action, title, null); } + /** + * Creates a new Action with the given action string, title and icon URL. + * + * @param action Action string. + * @param title Title text. + * @param icon Icon URL or null. + */ public Action(String action, String title, @Nullable String icon) { checkArgument(!Strings.isNullOrEmpty(action)); checkArgument(!Strings.isNullOrEmpty(title)); @@ -160,6 +188,7 @@ public static class Builder { private final List actions = new ArrayList<>(); private String badge; private String body; + private Object data; private Direction direction; private String icon; private String image; @@ -174,61 +203,180 @@ public static class Builder { private Builder() {} - public Builder setTitle(String title) { - this.title = title; + /** + * Adds a notification action to the notification. + * + * @param action A non-null {@link Action}. + * @return This builder. + */ + public Builder addAction(@NonNull Action action) { + this.actions.add(action); return this; } + /** + * Adds all the actions in the given list to the notification. + * + * @param actions A non-null list of actions. + * @return This builder. + */ + public Builder addAllActions(@NonNull List actions) { + this.actions.addAll(actions); + return this; + } + + /** + * Sets the URL of the image used to represent the notification when there is + * not enough space to display the notification itself. + * + * @param badge Badge URL. + * @return This builder. + */ + public Builder setBadge(String badge) { + this.badge = badge; + return this; + } + + /** + * Sets the body text of the notification. + * + * @param body Body text. + * @return This builder. + */ public Builder setBody(String body) { this.body = body; return this; } - public Builder setIcon(String icon) { - this.icon = icon; + /** + * Sets any arbitrary data that should be associated with the notification. + * + * @param data A json-serializable object. + * @return This builder. + */ + public Builder setData(Object data) { + this.data = data; return this; } - public Builder setBadge(String badge) { - this.badge = badge; + /** + * Sets the direction in which to display the notification. + * + * @param direction Direction enum value. + * @return This builder. + */ + public Builder setDirection(Direction direction) { + this.direction = direction; return this; } + /** + * Sets the URL to the icon of the notification. + * + * @param icon Icon URL. + * @return This builder. + */ + public Builder setIcon(String icon) { + this.icon = icon; + return this; + } + + /** + * Sets the URL of an image to be displayed in the notification. + * + * @param image Image URL + * @return This builder. + */ public Builder setImage(String image) { this.image = image; return this; } + /** + * Sets the language of the notification. + * + * @param language Notification language. + * @return This builder. + */ public Builder setLanguage(String language) { this.language = language; return this; } + /** + * Sets whether the user should be notified after a new notification replaces an old one. + * + * @param renotify true to notify the user on replacement. + * @return This builder. + */ public Builder setRenotify(boolean renotify) { this.renotify = renotify; return this; } + /** + * Sets whether a notification should remain active until the user clicks or dismisses it, + * rather than closing automatically. + * + * @param requireInteraction true to keep the notification active until user interaction. + * @return This builder. + */ public Builder setRequireInteraction(boolean requireInteraction) { this.requireInteraction = requireInteraction; return this; } + /** + * Sets whether the notification should be silent. + * + * @param silent true to indicate that the notification should be silent. + * @return This builder. + */ public Builder setSilent(boolean silent) { this.silent = silent; return this; } + /** + * Sets an identifying tag on the notification. + * + * @param tag A tag to be associated with the notification. + * @return This builder. + */ public Builder setTag(String tag) { this.tag = tag; return this; } + /** + * Sets a timestamp value on the notification. + * + * @param timestamp A timestamp value as a number. + * @return This builder. + */ public Builder setTimestamp(long timestamp) { this.timestamp = timestamp; return this; } + /** + * Sets the title text of the notification. + * + * @param title Title text. + * @return This builder. + */ + public Builder setTitle(String title) { + this.title = title; + return this; + } + + /** + * Sets a vibration pattern for the device's vibration hardware to emit + * when the notification fires. + * + * @param pattern An integer array representing a vibration pattern. + * @return This builder. + */ public Builder setVibrate(int[] pattern) { List list = new ArrayList<>(); for (int value : pattern) { @@ -238,21 +386,11 @@ public Builder setVibrate(int[] pattern) { return this; } - public Builder setDirection(Direction direction) { - this.direction = direction; - return this; - } - - public Builder addAction(@NonNull Action action) { - this.actions.add(action); - return this; - } - - public Builder addAllActions(@NonNull List actions) { - this.actions.addAll(actions); - return this; - } - + /** + * Creates a new {@link WebpushNotification} from the parameters set on this builder. + * + * @return A new {@link WebpushNotification} instance. + */ public WebpushNotification build() { return new WebpushNotification(this); } diff --git a/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java b/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java index 986d2982b..5da847a38 100644 --- a/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java +++ b/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java @@ -696,6 +696,7 @@ private static Map> buildTestMessages() { .setImage("test-image") .setLanguage("test-lang") .setTag("test-tag") + .setData(ImmutableList.of("arbitrary", "data")) .setDirection(Direction.AUTO) .setRenotify(true) .setRequireInteraction(false) @@ -721,6 +722,7 @@ private static Map> buildTestMessages() { .put("image", "test-image") .put("lang", "test-lang") .put("tag", "test-tag") + .put("data", ImmutableList.of("arbitrary", "data")) .put("renotify", true) .put("requireInteraction", false) .put("silent", true) From 1e08ef7fa25de0ac297dfeeaff2d568452d3d3e4 Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Mon, 16 Jul 2018 14:41:43 -0700 Subject: [PATCH 3/7] Support for adding arbitrary key-value pairs to WebpushNotification --- .../firebase/messaging/WebpushConfig.java | 4 +- .../messaging/WebpushNotification.java | 139 ++++++++++-------- .../messaging/FirebaseMessagingTest.java | 25 +++- 3 files changed, 106 insertions(+), 62 deletions(-) diff --git a/src/main/java/com/google/firebase/messaging/WebpushConfig.java b/src/main/java/com/google/firebase/messaging/WebpushConfig.java index 6b5ac4ac2..7c99580c6 100644 --- a/src/main/java/com/google/firebase/messaging/WebpushConfig.java +++ b/src/main/java/com/google/firebase/messaging/WebpushConfig.java @@ -35,12 +35,12 @@ public class WebpushConfig { private final Map data; @Key("notification") - private final WebpushNotification notification; + private final Map notification; private WebpushConfig(Builder builder) { this.headers = builder.headers.isEmpty() ? null : ImmutableMap.copyOf(builder.headers); this.data = builder.data.isEmpty() ? null : ImmutableMap.copyOf(builder.data); - this.notification = builder.notification; + this.notification = builder.notification != null ? builder.notification.getFields() : null; } /** diff --git a/src/main/java/com/google/firebase/messaging/WebpushNotification.java b/src/main/java/com/google/firebase/messaging/WebpushNotification.java index 69d94f35c..b4812068c 100644 --- a/src/main/java/com/google/firebase/messaging/WebpushNotification.java +++ b/src/main/java/com/google/firebase/messaging/WebpushNotification.java @@ -21,10 +21,13 @@ 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 com.google.firebase.internal.Nullable; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Represents the Webpush-specific notification options that can be included in a {@link Message}. @@ -34,50 +37,7 @@ */ public class WebpushNotification { - @Key("actions") - private final List actions; - - @Key("badge") - private final String badge; - - @Key("body") - private final String body; - - @Key("data") - private final Object data; - - @Key("dir") - private final String direction; - - @Key("icon") - private final String icon; - - @Key("image") - private final String image; - - @Key("lang") - private final String language; - - @Key("renotify") - private final Boolean renotify; - - @Key("requireInteraction") - private final Boolean requireInteraction; - - @Key("silent") - private final Boolean silent; - - @Key("tag") - private final String tag; - - @Key("timestamp") - private final Long timestamp; - - @Key("title") - private final String title; - - @Key("vibrate") - private final List vibrate; + private final Map fields; /** * Creates a new notification with the given title and body. Overrides the options set via @@ -103,21 +63,58 @@ public WebpushNotification(String title, String body, @Nullable String icon) { } private WebpushNotification(Builder builder) { - this.actions = !builder.actions.isEmpty() ? ImmutableList.copyOf(builder.actions) : null; - this.badge = builder.badge; - this.body = builder.body; - this.data = builder.data; - this.direction = builder.direction != null ? builder.direction.value : null; - this.icon = builder.icon; - this.image = builder.image; - this.language = builder.language; - this.renotify = builder.renotify; - this.requireInteraction = builder.requireInteraction; - this.silent = builder.silent; - this.tag = builder.tag; - this.timestamp = builder.timestamp; - this.title = builder.title; - this.vibrate = builder.vibrate; + ImmutableMap.Builder fields = ImmutableMap.builder(); + if (!builder.actions.isEmpty()) { + fields.put("actions", ImmutableList.copyOf(builder.actions)); + } + if (!Strings.isNullOrEmpty(builder.badge)) { + fields.put("badge", builder.badge); + } + if (!Strings.isNullOrEmpty(builder.body)) { + fields.put("body", builder.body); + } + if (builder.data != null) { + fields.put("data", builder.data); + } + if (builder.direction != null) { + fields.put("dir", builder.direction.value); + } + if (!Strings.isNullOrEmpty(builder.icon)) { + fields.put("icon", builder.icon); + } + if (!Strings.isNullOrEmpty(builder.image)) { + fields.put("image", builder.image); + } + if (!Strings.isNullOrEmpty(builder.language)) { + fields.put("lang", builder.language); + } + if (builder.renotify != null) { + fields.put("renotify", builder.renotify); + } + if (builder.requireInteraction != null) { + fields.put("requireInteraction", builder.requireInteraction); + } + if (builder.silent != null) { + fields.put("silent", builder.silent); + } + if (!Strings.isNullOrEmpty(builder.tag)) { + fields.put("tag", builder.tag); + } + if (builder.timestamp != null) { + fields.put("timestamp", builder.timestamp); + } + if (!Strings.isNullOrEmpty(builder.title)) { + fields.put("title", builder.title); + } + if (builder.vibrate != null) { + fields.put("vibrate", builder.vibrate); + } + fields.putAll(builder.customData); + this.fields = fields.build(); + } + + Map getFields() { + return fields; } /** @@ -200,6 +197,7 @@ public static class Builder { private Long timestamp; private String title; private List vibrate; + private final Map customData = new HashMap<>(); private Builder() {} @@ -386,6 +384,29 @@ public Builder setVibrate(int[] pattern) { return this; } + /** + * Puts a custom key-value pair to the notification. + * + * @param key A non-null key. + * @param value A non-null, json-serializable value. + * @return This builder. + */ + public Builder putCustomData(@NonNull String key, @NonNull Object value) { + this.customData.put(key, value); + return this; + } + + /** + * Puts all the key-value pairs in the specified map to the notification. + * + * @param fields A non-null map. Map must not contain null keys or values. + * @return This builder. + */ + public Builder putAllCustomData(@NonNull Map fields) { + this.customData.putAll(fields); + return this; + } + /** * Creates a new {@link WebpushNotification} from the parameters set on this builder. * diff --git a/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java b/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java index 5da847a38..434256113 100644 --- a/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java +++ b/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java @@ -659,7 +659,25 @@ private static Map> buildTestMessages() { "title", "test-title", "body", "test-body")))) )); - // Webpush message (simple) + // Webpush message (no notification) + builder.put( + Message.builder() + .setWebpushConfig(WebpushConfig.builder() + .putHeader("h1", "v1") + .putAllHeaders(ImmutableMap.of("h2", "v2", "h3", "v3")) + .putData("k1", "v1") + .putAllData(ImmutableMap.of("k2", "v2", "k3", "v3")) + .build()) + .setTopic("test-topic") + .build(), + ImmutableMap.of( + "topic", "test-topic", + "webpush", ImmutableMap.of( + "headers", ImmutableMap.of("h1", "v1", "h2", "v2", "h3", "v3"), + "data", ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3")) + )); + + // Webpush message (simple notification) builder.put( Message.builder() .setWebpushConfig(WebpushConfig.builder() @@ -705,6 +723,8 @@ private static Map> buildTestMessages() { .setVibrate(new int[]{200, 100, 200}) .addAction(new Action("action1", "title1")) .addAllActions(ImmutableList.of(new Action("action2", "title2", "icon2"))) + .putCustomData("k4", "v4") + .putAllCustomData(ImmutableMap.of("k5", "v5", "k6", "v6")) .build()) .build()) .setTopic("test-topic") @@ -733,6 +753,9 @@ private static Map> buildTestMessages() { .put("actions", ImmutableList.of( ImmutableMap.of("action", "action1", "title", "title1"), ImmutableMap.of("action", "action2", "title", "title2", "icon", "icon2"))) + .put("k4", "v4") + .put("k5", "v5") + .put("k6", "v6") .build()) )); From ff562c6bfaf24a73d4acb3ab645c4391306d5ee6 Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Mon, 16 Jul 2018 14:42:49 -0700 Subject: [PATCH 4/7] Updated changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index da38156b4..a26343301 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +- [added] WebpushNotification type now supports arbitrary key-value + pairs in its payload. - [added] Implemented the ability to create custom tokens without service account credentials. - [added] Added the `setServiceAccount()` method to the From 2a5feed2f4115b5ab1c78e26963eab71937fcdd1 Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Mon, 16 Jul 2018 15:14:51 -0700 Subject: [PATCH 5/7] Added a couple of helper methods for clarity --- .../messaging/WebpushNotification.java | 70 ++++++++----------- 1 file changed, 28 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/google/firebase/messaging/WebpushNotification.java b/src/main/java/com/google/firebase/messaging/WebpushNotification.java index b4812068c..549e5f6cc 100644 --- a/src/main/java/com/google/firebase/messaging/WebpushNotification.java +++ b/src/main/java/com/google/firebase/messaging/WebpushNotification.java @@ -67,48 +67,20 @@ private WebpushNotification(Builder builder) { if (!builder.actions.isEmpty()) { fields.put("actions", ImmutableList.copyOf(builder.actions)); } - if (!Strings.isNullOrEmpty(builder.badge)) { - fields.put("badge", builder.badge); - } - if (!Strings.isNullOrEmpty(builder.body)) { - fields.put("body", builder.body); - } - if (builder.data != null) { - fields.put("data", builder.data); - } - if (builder.direction != null) { - fields.put("dir", builder.direction.value); - } - if (!Strings.isNullOrEmpty(builder.icon)) { - fields.put("icon", builder.icon); - } - if (!Strings.isNullOrEmpty(builder.image)) { - fields.put("image", builder.image); - } - if (!Strings.isNullOrEmpty(builder.language)) { - fields.put("lang", builder.language); - } - if (builder.renotify != null) { - fields.put("renotify", builder.renotify); - } - if (builder.requireInteraction != null) { - fields.put("requireInteraction", builder.requireInteraction); - } - if (builder.silent != null) { - fields.put("silent", builder.silent); - } - if (!Strings.isNullOrEmpty(builder.tag)) { - fields.put("tag", builder.tag); - } - if (builder.timestamp != null) { - fields.put("timestamp", builder.timestamp); - } - if (!Strings.isNullOrEmpty(builder.title)) { - fields.put("title", builder.title); - } - if (builder.vibrate != null) { - fields.put("vibrate", builder.vibrate); - } + addNonNullNonEmpty(fields, "badge", builder.badge); + addNonNullNonEmpty(fields, "body", builder.body); + addNonNull(fields, "data", builder.data); + addNonNullNonEmpty(fields, "dir", builder.direction != null ? builder.direction.value : null); + addNonNullNonEmpty(fields, "icon", builder.icon); + addNonNullNonEmpty(fields, "image", builder.image); + addNonNullNonEmpty(fields, "lang", builder.language); + addNonNull(fields, "renotify", builder.renotify); + addNonNull(fields, "requireInteraction", builder.requireInteraction); + addNonNull(fields, "silent", builder.silent); + addNonNullNonEmpty(fields, "tag", builder.tag); + addNonNull(fields, "timestamp", builder.timestamp); + addNonNullNonEmpty(fields, "title", builder.title); + addNonNull(fields, "vibrate", builder.vibrate); fields.putAll(builder.customData); this.fields = fields.build(); } @@ -416,4 +388,18 @@ public WebpushNotification build() { return new WebpushNotification(this); } } + + private static void addNonNull( + ImmutableMap.Builder fields, String key, Object value) { + if (value != null) { + fields.put(key, value); + } + } + + private static void addNonNullNonEmpty( + ImmutableMap.Builder fields, String key, String value) { + if (!Strings.isNullOrEmpty(value)) { + fields.put(key, value); + } + } } From bedc0dc65e271cdffa1e05b9a1737ec4a2babf5b Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Thu, 26 Jul 2018 14:40:27 -0700 Subject: [PATCH 6/7] Updated comment --- .../java/com/google/firebase/messaging/WebpushNotification.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/google/firebase/messaging/WebpushNotification.java b/src/main/java/com/google/firebase/messaging/WebpushNotification.java index 549e5f6cc..561085e70 100644 --- a/src/main/java/com/google/firebase/messaging/WebpushNotification.java +++ b/src/main/java/com/google/firebase/messaging/WebpushNotification.java @@ -221,7 +221,7 @@ public Builder setBody(String body) { /** * Sets any arbitrary data that should be associated with the notification. * - * @param data A json-serializable object. + * @param data A JSON-serializable object. * @return This builder. */ public Builder setData(Object data) { From 7d2d9f1574890a2e715187c22897546d1e0f22ef Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Fri, 27 Jul 2018 13:39:42 -0700 Subject: [PATCH 7/7] Renamed setTimestamp to setTimestampMillis as per API review feedback --- .../firebase/messaging/WebpushNotification.java | 12 ++++++------ .../firebase/messaging/FirebaseMessagingTest.java | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/google/firebase/messaging/WebpushNotification.java b/src/main/java/com/google/firebase/messaging/WebpushNotification.java index 561085e70..ef9b97f22 100644 --- a/src/main/java/com/google/firebase/messaging/WebpushNotification.java +++ b/src/main/java/com/google/firebase/messaging/WebpushNotification.java @@ -78,7 +78,7 @@ private WebpushNotification(Builder builder) { addNonNull(fields, "requireInteraction", builder.requireInteraction); addNonNull(fields, "silent", builder.silent); addNonNullNonEmpty(fields, "tag", builder.tag); - addNonNull(fields, "timestamp", builder.timestamp); + addNonNull(fields, "timestamp", builder.timestampMillis); addNonNullNonEmpty(fields, "title", builder.title); addNonNull(fields, "vibrate", builder.vibrate); fields.putAll(builder.customData); @@ -166,7 +166,7 @@ public static class Builder { private Boolean requireInteraction; private Boolean silent; private String tag; - private Long timestamp; + private Long timestampMillis; private String title; private List vibrate; private final Map customData = new HashMap<>(); @@ -319,13 +319,13 @@ public Builder setTag(String tag) { } /** - * Sets a timestamp value on the notification. + * Sets a timestamp value in milliseconds on the notification. * - * @param timestamp A timestamp value as a number. + * @param timestampMillis A timestamp value as a number. * @return This builder. */ - public Builder setTimestamp(long timestamp) { - this.timestamp = timestamp; + public Builder setTimestampMillis(long timestampMillis) { + this.timestampMillis = timestampMillis; return this; } diff --git a/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java b/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java index 434256113..b269978f2 100644 --- a/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java +++ b/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java @@ -719,7 +719,7 @@ private static Map> buildTestMessages() { .setRenotify(true) .setRequireInteraction(false) .setSilent(true) - .setTimestamp(100L) + .setTimestampMillis(100L) .setVibrate(new int[]{200, 100, 200}) .addAction(new Action("action1", "title1")) .addAllActions(ImmutableList.of(new Action("action2", "title2", "icon2")))