Skip to content

feat(fcm): Added a Builder for constructing Notification objects #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 11, 2019
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
73 changes: 72 additions & 1 deletion src/main/java/com/google/firebase/messaging/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,93 @@ public class Notification {
*
* @param title Title of the notification.
* @param body Body of the notification.
*
* @deprecated Use {@link #Notification(Builder)} instead.
*/
public Notification(String title, String body) {
this(title, body, null);
}

/**
* Creates a new {@code Notification} using the given title, body, and image.
*
* @param title Title of the notification.
* @param body Body of the notification.
* @param imageUrl URL of the image that is going to be displayed in the notification.
*
* @deprecated Use {@link #Notification(Builder)} instead.
*/
public Notification(String title, String body, String imageUrl) {
this.title = title;
this.body = body;
this.image = imageUrl;
}

private Notification(Builder builder) {
this.title = builder.title;
this.body = builder.body;
this.image = builder.image;
}

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

public static class Builder {

private String title;
private String body;
private String image;

private Builder() {}

/**
* Sets the title of the notification.
*
* @param title Title of the notification.
* @return This builder.
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}

/**
* Sets the body of the notification.
*
* @param body Body of the notification.
* @return This builder.
*/
public Builder setBody(String body) {
this.body = body;
return this;
}

/**
* Sets the URL of the image that is going to be displayed in the notification.
*
* @param imageUrl URL of the image that is going to be displayed in the notification.
* @return This builder.
*/
public Builder setImage(String imageUrl) {
this.image = imageUrl;
return this;
}

/**
* Creates a new {@link Notification} instance from the parameters set on this builder.
*
* @return A new {@link Notification} instance.
* @throws IllegalArgumentException If any of the parameters set on the builder are invalid.
*/
public Notification build() {
return new Notification(this);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public static void setUpClass() {
public void testSend() throws Exception {
FirebaseMessaging messaging = FirebaseMessaging.getInstance();
Message message = Message.builder()
.setNotification(new Notification("Title", "Body", TEST_IMAGE_URL))
.setNotification(Notification.builder()
.setTitle("Title")
.setBody("Body")
.setImage(TEST_IMAGE_URL)
.build())
.setAndroidConfig(AndroidConfig.builder()
.setRestrictedPackageName("com.google.firebase.testing")
.build())
Expand Down
29 changes: 27 additions & 2 deletions src/test/java/com/google/firebase/messaging/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void testPrefixedTopicName() throws IOException {
}

@Test
public void testNotificationMessage() throws IOException {
public void testNotificationMessageDeprecatedConstructor() throws IOException {
Message message = Message.builder()
.setNotification(new Notification("title", "body"))
.setTopic("test-topic")
Expand All @@ -97,6 +97,16 @@ public void testNotificationMessage() throws IOException {
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "notification", data), message);
}

@Test
public void testNotificationMessage() throws IOException {
Message message = Message.builder()
.setNotification(Notification.builder().setTitle("title").setBody("body").build())
.setTopic("test-topic")
.build();
Map<String, String> data = ImmutableMap.of("title", "title", "body", "body");
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "notification", data), message);
}

@Test
public void testEmptyAndroidMessage() throws IOException {
Message message = Message.builder()
Expand Down Expand Up @@ -703,7 +713,7 @@ public void testIncorrectAnalyticsLabelFormat() {
}

@Test
public void testImageInNotification() throws IOException {
public void testImageInNotificationDeprecatedConstructor() throws IOException {
Message message = Message.builder()
.setNotification(new Notification("title", "body", TEST_IMAGE_URL))
.setTopic("test-topic")
Expand All @@ -713,6 +723,21 @@ public void testImageInNotification() throws IOException {
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "notification", data), message);
}

@Test
public void testImageInNotification() throws IOException {
Message message = Message.builder()
.setNotification(Notification.builder()
.setTitle("title")
.setBody("body")
.setImage(TEST_IMAGE_URL)
.build())
.setTopic("test-topic")
.build();
Map<String, String> data = ImmutableMap.of(
"title", "title", "body", "body", "image", TEST_IMAGE_URL);
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "notification", data), message);
}

@Test
public void testImageInAndroidNotification() throws IOException {
Message message = Message.builder()
Expand Down