Skip to content
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

-
- [added] Added `WebpushFcmOptions` to the `FirebaseMessaging` API, providing
the `setLink()` method.

# v6.9.0

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/google/firebase/messaging/WebpushConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.api.client.util.Key;
import com.google.common.collect.ImmutableMap;
import com.google.firebase.internal.NonNull;

import java.util.HashMap;
import java.util.Map;

Expand All @@ -37,10 +38,14 @@ public class WebpushConfig {
@Key("notification")
private final Map<String, Object> notification;

@Key("fcm_options")
private final WebpushFcmOptions fcmOptions;

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 != null ? builder.notification.getFields() : null;
this.fcmOptions = builder.fcmOptions;
}

/**
Expand All @@ -57,6 +62,8 @@ public static class Builder {
private final Map<String, String> headers = new HashMap<>();
private final Map<String, String> data = new HashMap<>();
private WebpushNotification notification;
private WebpushFcmOptions fcmOptions;


private Builder() {}

Expand Down Expand Up @@ -125,6 +132,17 @@ public Builder setNotification(WebpushNotification notification) {
return this;
}

/**
* Sets the Webpush FCM options to be included in the Webpush config.
*
* @param fcmOptions A {@link WebpushFcmOptions} instance.
* @return This builder.
*/
public Builder setFcmOptions(WebpushFcmOptions fcmOptions) {
this.fcmOptions = fcmOptions;
return this;
}

/**
* Creates a new {@link WebpushConfig} instance from the parameters set on this builder.
*
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/google/firebase/messaging/WebpushFcmOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.google.firebase.messaging;

import com.google.api.client.util.Key;

/**
* Represents options for features provided by the FCM SDK for Web.
* Can be included in {@link WebpushConfig}. Instances of this class are thread-safe and immutable.
*/
public final class WebpushFcmOptions {

@Key("link")
private final String link;

private WebpushFcmOptions(Builder builder) {
this.link = builder.link;
}

/**
* Creates a new {@code WebpushFcmOptions} using given link.
*
* @param link The link to open when the user clicks on the notification.
* For all URL values, HTTPS is required.
*/
public static WebpushFcmOptions withLink(String link) {
return new Builder().setLink(link).build();
}

/**
* Creates a new {@link WebpushFcmOptions.Builder}.
*
* @return An {@link WebpushFcmOptions.Builder} instance.
*/
public static Builder builder() {
return new WebpushFcmOptions.Builder();
}

public static class Builder {

private String link;

private Builder() {}

/**
* @param link The link to open when the user clicks on the notification.
* For all URL values, HTTPS is required.
* @return This builder
*/
public Builder setLink(String link) {
this.link = link;
return this;
}

/**
* Creates a new {@link WebpushFcmOptions} instance from the parameters set on this builder.
*
* @return A new {@link WebpushFcmOptions} instance.
*/
public WebpushFcmOptions build() {
return new WebpushFcmOptions(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@ private static Map<Message, Map<String, Object>> buildTestMessages() {
.putCustomData("k4", "v4")
.putAllCustomData(ImmutableMap.<String, Object>of("k5", "v5", "k6", "v6"))
.build())
.setFcmOptions(WebpushFcmOptions.withLink("https://firebase.google.com"))
.build())
.setTopic("test-topic")
.build(),
Expand Down Expand Up @@ -811,7 +812,8 @@ private static Map<Message, Map<String, Object>> buildTestMessages() {
.put("k4", "v4")
.put("k5", "v5")
.put("k6", "v6")
.build())
.build(),
"fcm_options", ImmutableMap.of("link", "https://firebase.google.com"))
));

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void testSend() throws Exception {
.setWebpushConfig(WebpushConfig.builder()
.putHeader("X-Custom-Val", "Foo")
.setNotification(new WebpushNotification("Title", "Body"))
.setFcmOptions(WebpushFcmOptions.withLink("https://firebase.google.com"))
.build())
.setTopic("foo-bar")
.build();
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/google/firebase/messaging/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,26 @@ public void testWebpushMessageWithoutNotification() throws IOException {
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "webpush", data), message);
}

@Test
public void testWebpushMessageWithWebpushOptions() throws IOException {
Message message = Message.builder()
.setWebpushConfig(WebpushConfig.builder()
.putHeader("k1", "v1")
.putAllHeaders(ImmutableMap.of("k2", "v2", "k3", "v3"))
.putData("k1", "v1")
.putAllData(ImmutableMap.of("k2", "v2", "k3", "v3"))
.setFcmOptions(WebpushFcmOptions.withLink("https://my-server/page"))
.build())
.setTopic("test-topic")
.build();
Map<String, Object> data = ImmutableMap.<String, Object>of(
"headers", ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"),
"data", ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"),
"fcm_options", ImmutableMap.of("link", "https://my-server/page")
);
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "webpush", data), message);
}

@Test
public void testWebpushMessageWithNotification() throws IOException {
Message message = Message.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.firebase.messaging.SendResponse;
import com.google.firebase.messaging.TopicManagementResponse;
import com.google.firebase.messaging.WebpushConfig;
import com.google.firebase.messaging.WebpushFcmOptions;
import com.google.firebase.messaging.WebpushNotification;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -239,6 +240,7 @@ public Message webpushMessage() {
"$GOOG up 1.43% on the day",
"$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",
"https://my-server/icon.png"))
.setFcmOptions(WebpushFcmOptions.withLink("https://my-server/page-to-open-on-click"))
.build())
.setTopic("industry-tech")
.build();
Expand Down Expand Up @@ -310,4 +312,4 @@ public void unsubscribeFromTopic() throws FirebaseMessagingException {
// [END unsubscribe]
}

}
}