Skip to content

Commit d6cda02

Browse files
authored
feat(fcm): Added a Builder for constructing Notification objects (#311)
* Add a builder for Notification * Adjust the integration test
1 parent d8c6111 commit d6cda02

File tree

3 files changed

+104
-4
lines changed

3 files changed

+104
-4
lines changed

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

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,93 @@ public class Notification {
3838
*
3939
* @param title Title of the notification.
4040
* @param body Body of the notification.
41+
*
42+
* @deprecated Use {@link #Notification(Builder)} instead.
4143
*/
4244
public Notification(String title, String body) {
4345
this(title, body, null);
4446
}
45-
47+
4648
/**
4749
* Creates a new {@code Notification} using the given title, body, and image.
4850
*
4951
* @param title Title of the notification.
5052
* @param body Body of the notification.
5153
* @param imageUrl URL of the image that is going to be displayed in the notification.
54+
*
55+
* @deprecated Use {@link #Notification(Builder)} instead.
5256
*/
5357
public Notification(String title, String body, String imageUrl) {
5458
this.title = title;
5559
this.body = body;
5660
this.image = imageUrl;
5761
}
5862

63+
private Notification(Builder builder) {
64+
this.title = builder.title;
65+
this.body = builder.body;
66+
this.image = builder.image;
67+
}
68+
69+
/**
70+
* Creates a new {@link Notification.Builder}.
71+
*
72+
* @return A {@link Notification.Builder} instance.
73+
*/
74+
public static Builder builder() {
75+
return new Builder();
76+
}
77+
78+
public static class Builder {
79+
80+
private String title;
81+
private String body;
82+
private String image;
83+
84+
private Builder() {}
85+
86+
/**
87+
* Sets the title of the notification.
88+
*
89+
* @param title Title of the notification.
90+
* @return This builder.
91+
*/
92+
public Builder setTitle(String title) {
93+
this.title = title;
94+
return this;
95+
}
96+
97+
/**
98+
* Sets the body of the notification.
99+
*
100+
* @param body Body of the notification.
101+
* @return This builder.
102+
*/
103+
public Builder setBody(String body) {
104+
this.body = body;
105+
return this;
106+
}
107+
108+
/**
109+
* Sets the URL of the image that is going to be displayed in the notification.
110+
*
111+
* @param imageUrl URL of the image that is going to be displayed in the notification.
112+
* @return This builder.
113+
*/
114+
public Builder setImage(String imageUrl) {
115+
this.image = imageUrl;
116+
return this;
117+
}
118+
119+
/**
120+
* Creates a new {@link Notification} instance from the parameters set on this builder.
121+
*
122+
* @return A new {@link Notification} instance.
123+
* @throws IllegalArgumentException If any of the parameters set on the builder are invalid.
124+
*/
125+
public Notification build() {
126+
return new Notification(this);
127+
}
128+
}
129+
59130
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ public static void setUpClass() {
4545
public void testSend() throws Exception {
4646
FirebaseMessaging messaging = FirebaseMessaging.getInstance();
4747
Message message = Message.builder()
48-
.setNotification(new Notification("Title", "Body", TEST_IMAGE_URL))
48+
.setNotification(Notification.builder()
49+
.setTitle("Title")
50+
.setBody("Body")
51+
.setImage(TEST_IMAGE_URL)
52+
.build())
4953
.setAndroidConfig(AndroidConfig.builder()
5054
.setRestrictedPackageName("com.google.firebase.testing")
5155
.build())

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void testPrefixedTopicName() throws IOException {
8888
}
8989

9090
@Test
91-
public void testNotificationMessage() throws IOException {
91+
public void testNotificationMessageDeprecatedConstructor() throws IOException {
9292
Message message = Message.builder()
9393
.setNotification(new Notification("title", "body"))
9494
.setTopic("test-topic")
@@ -97,6 +97,16 @@ public void testNotificationMessage() throws IOException {
9797
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "notification", data), message);
9898
}
9999

100+
@Test
101+
public void testNotificationMessage() throws IOException {
102+
Message message = Message.builder()
103+
.setNotification(Notification.builder().setTitle("title").setBody("body").build())
104+
.setTopic("test-topic")
105+
.build();
106+
Map<String, String> data = ImmutableMap.of("title", "title", "body", "body");
107+
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "notification", data), message);
108+
}
109+
100110
@Test
101111
public void testEmptyAndroidMessage() throws IOException {
102112
Message message = Message.builder()
@@ -703,7 +713,7 @@ public void testIncorrectAnalyticsLabelFormat() {
703713
}
704714

705715
@Test
706-
public void testImageInNotification() throws IOException {
716+
public void testImageInNotificationDeprecatedConstructor() throws IOException {
707717
Message message = Message.builder()
708718
.setNotification(new Notification("title", "body", TEST_IMAGE_URL))
709719
.setTopic("test-topic")
@@ -713,6 +723,21 @@ public void testImageInNotification() throws IOException {
713723
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "notification", data), message);
714724
}
715725

726+
@Test
727+
public void testImageInNotification() throws IOException {
728+
Message message = Message.builder()
729+
.setNotification(Notification.builder()
730+
.setTitle("title")
731+
.setBody("body")
732+
.setImage(TEST_IMAGE_URL)
733+
.build())
734+
.setTopic("test-topic")
735+
.build();
736+
Map<String, String> data = ImmutableMap.of(
737+
"title", "title", "body", "body", "image", TEST_IMAGE_URL);
738+
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "notification", data), message);
739+
}
740+
716741
@Test
717742
public void testImageInAndroidNotification() throws IOException {
718743
Message message = Message.builder()

0 commit comments

Comments
 (0)