Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Support for adding arbitrary key-value pairs to WebpushNotification
  • Loading branch information
hiranya911 committed Jul 16, 2018
commit 1e08ef7fa25de0ac297dfeeaff2d568452d3d3e4
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public class WebpushConfig {
private final Map<String, String> data;

@Key("notification")
private final WebpushNotification notification;
private final Map<String, Object> notification;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't quite follow where this private fields are ever read. Can you elaborate?

I am asking this because I liked using WebpushNotification here better but I can see reason why the Map would be more appropriate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these get serialized into JSON. We now need a Map because the notification now allows arbitrary key-value pairs (via putCustomData()).


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

/**
Expand Down
139 changes: 80 additions & 59 deletions src/main/java/com/google/firebase/messaging/WebpushNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -34,50 +37,7 @@
*/
public class WebpushNotification {

@Key("actions")
private final List<Action> 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<Integer> vibrate;
private final Map<String, Object> fields;

/**
* Creates a new notification with the given title and body. Overrides the options set via
Expand All @@ -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<String, Object> 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<String, Object> getFields() {
return fields;
}

/**
Expand Down Expand Up @@ -200,6 +197,7 @@ public static class Builder {
private Long timestamp;
private String title;
private List<Integer> vibrate;
private final Map<String, Object> customData = new HashMap<>();

private Builder() {}

Expand Down Expand Up @@ -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<String, Object> fields) {
this.customData.putAll(fields);
return this;
}

/**
* Creates a new {@link WebpushNotification} from the parameters set on this builder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,25 @@ private static Map<Message, Map<String, Object>> 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.<String, Object>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()
Expand Down Expand Up @@ -705,6 +723,8 @@ private static Map<Message, Map<String, Object>> 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.<String, Object>of("k5", "v5", "k6", "v6"))
.build())
.build())
.setTopic("test-topic")
Expand Down Expand Up @@ -733,6 +753,9 @@ private static Map<Message, Map<String, Object>> 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())
));

Expand Down