Skip to content

Commit 0a06159

Browse files
kirmerzlikinhiranya911
authored andcommitted
Add missing WebpushFcmOptions entity (as per documentation) (firebase#295)
* Add missing WebpushFcmOptions entity As per https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushfcmoptions * Improve public API of WebpushFcmOptions * Clean up code. Update CHANGELOG * Slightly rephrase change description in CHANGELOG
1 parent 0e27bb0 commit 0a06159

7 files changed

Lines changed: 109 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22

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

56
# v6.9.0
67

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.api.client.util.Key;
2020
import com.google.common.collect.ImmutableMap;
2121
import com.google.firebase.internal.NonNull;
22+
2223
import java.util.HashMap;
2324
import java.util.Map;
2425

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

41+
@Key("fcm_options")
42+
private final WebpushFcmOptions fcmOptions;
43+
4044
private WebpushConfig(Builder builder) {
4145
this.headers = builder.headers.isEmpty() ? null : ImmutableMap.copyOf(builder.headers);
4246
this.data = builder.data.isEmpty() ? null : ImmutableMap.copyOf(builder.data);
4347
this.notification = builder.notification != null ? builder.notification.getFields() : null;
48+
this.fcmOptions = builder.fcmOptions;
4449
}
4550

4651
/**
@@ -57,6 +62,8 @@ public static class Builder {
5762
private final Map<String, String> headers = new HashMap<>();
5863
private final Map<String, String> data = new HashMap<>();
5964
private WebpushNotification notification;
65+
private WebpushFcmOptions fcmOptions;
66+
6067

6168
private Builder() {}
6269

@@ -125,6 +132,17 @@ public Builder setNotification(WebpushNotification notification) {
125132
return this;
126133
}
127134

135+
/**
136+
* Sets the Webpush FCM options to be included in the Webpush config.
137+
*
138+
* @param fcmOptions A {@link WebpushFcmOptions} instance.
139+
* @return This builder.
140+
*/
141+
public Builder setFcmOptions(WebpushFcmOptions fcmOptions) {
142+
this.fcmOptions = fcmOptions;
143+
return this;
144+
}
145+
128146
/**
129147
* Creates a new {@link WebpushConfig} instance from the parameters set on this builder.
130148
*
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.google.firebase.messaging;
2+
3+
import com.google.api.client.util.Key;
4+
5+
/**
6+
* Represents options for features provided by the FCM SDK for Web.
7+
* Can be included in {@link WebpushConfig}. Instances of this class are thread-safe and immutable.
8+
*/
9+
public final class WebpushFcmOptions {
10+
11+
@Key("link")
12+
private final String link;
13+
14+
private WebpushFcmOptions(Builder builder) {
15+
this.link = builder.link;
16+
}
17+
18+
/**
19+
* Creates a new {@code WebpushFcmOptions} using given link.
20+
*
21+
* @param link The link to open when the user clicks on the notification.
22+
* For all URL values, HTTPS is required.
23+
*/
24+
public static WebpushFcmOptions withLink(String link) {
25+
return new Builder().setLink(link).build();
26+
}
27+
28+
/**
29+
* Creates a new {@link WebpushFcmOptions.Builder}.
30+
*
31+
* @return An {@link WebpushFcmOptions.Builder} instance.
32+
*/
33+
public static Builder builder() {
34+
return new WebpushFcmOptions.Builder();
35+
}
36+
37+
public static class Builder {
38+
39+
private String link;
40+
41+
private Builder() {}
42+
43+
/**
44+
* @param link The link to open when the user clicks on the notification.
45+
* For all URL values, HTTPS is required.
46+
* @return This builder
47+
*/
48+
public Builder setLink(String link) {
49+
this.link = link;
50+
return this;
51+
}
52+
53+
/**
54+
* Creates a new {@link WebpushFcmOptions} instance from the parameters set on this builder.
55+
*
56+
* @return A new {@link WebpushFcmOptions} instance.
57+
*/
58+
public WebpushFcmOptions build() {
59+
return new WebpushFcmOptions(this);
60+
}
61+
}
62+
}

src/test/java/com/google/firebase/messaging/FirebaseMessagingClientImplTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ private static Map<Message, Map<String, Object>> buildTestMessages() {
781781
.putCustomData("k4", "v4")
782782
.putAllCustomData(ImmutableMap.<String, Object>of("k5", "v5", "k6", "v6"))
783783
.build())
784+
.setFcmOptions(WebpushFcmOptions.withLink("https://firebase.google.com"))
784785
.build())
785786
.setTopic("test-topic")
786787
.build(),
@@ -811,7 +812,8 @@ private static Map<Message, Map<String, Object>> buildTestMessages() {
811812
.put("k4", "v4")
812813
.put("k5", "v5")
813814
.put("k6", "v6")
814-
.build())
815+
.build(),
816+
"fcm_options", ImmutableMap.of("link", "https://firebase.google.com"))
815817
));
816818

817819
return builder.build();

src/test/java/com/google/firebase/messaging/FirebaseMessagingIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void testSend() throws Exception {
5959
.setWebpushConfig(WebpushConfig.builder()
6060
.putHeader("X-Custom-Val", "Foo")
6161
.setNotification(new WebpushNotification("Title", "Body"))
62+
.setFcmOptions(WebpushFcmOptions.withLink("https://firebase.google.com"))
6263
.build())
6364
.setTopic("foo-bar")
6465
.build();

src/test/java/com/google/firebase/messaging/MessageTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,26 @@ public void testWebpushMessageWithoutNotification() throws IOException {
271271
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "webpush", data), message);
272272
}
273273

274+
@Test
275+
public void testWebpushMessageWithWebpushOptions() throws IOException {
276+
Message message = Message.builder()
277+
.setWebpushConfig(WebpushConfig.builder()
278+
.putHeader("k1", "v1")
279+
.putAllHeaders(ImmutableMap.of("k2", "v2", "k3", "v3"))
280+
.putData("k1", "v1")
281+
.putAllData(ImmutableMap.of("k2", "v2", "k3", "v3"))
282+
.setFcmOptions(WebpushFcmOptions.withLink("https://my-server/page"))
283+
.build())
284+
.setTopic("test-topic")
285+
.build();
286+
Map<String, Object> data = ImmutableMap.<String, Object>of(
287+
"headers", ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"),
288+
"data", ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"),
289+
"fcm_options", ImmutableMap.of("link", "https://my-server/page")
290+
);
291+
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "webpush", data), message);
292+
}
293+
274294
@Test
275295
public void testWebpushMessageWithNotification() throws IOException {
276296
Message message = Message.builder()

src/test/java/com/google/firebase/snippets/FirebaseMessagingSnippets.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.google.firebase.messaging.SendResponse;
3131
import com.google.firebase.messaging.TopicManagementResponse;
3232
import com.google.firebase.messaging.WebpushConfig;
33+
import com.google.firebase.messaging.WebpushFcmOptions;
3334
import com.google.firebase.messaging.WebpushNotification;
3435
import java.util.ArrayList;
3536
import java.util.Arrays;
@@ -239,6 +240,7 @@ public Message webpushMessage() {
239240
"$GOOG up 1.43% on the day",
240241
"$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",
241242
"https://my-server/icon.png"))
243+
.setFcmOptions(WebpushFcmOptions.withLink("https://my-server/page-to-open-on-click"))
242244
.build())
243245
.setTopic("industry-tech")
244246
.build();
@@ -310,4 +312,4 @@ public void unsubscribeFromTopic() throws FirebaseMessagingException {
310312
// [END unsubscribe]
311313
}
312314

313-
}
315+
}

0 commit comments

Comments
 (0)