Repository: firebase/firebase-admin-java
Pull request #311 (2019-09-11 21:27:51)

feat(fcm): Added a `Builder` for constructing `Notification` objects
Add a builder for Notification class.

Mark the existing two constructors for the Notification class as deprecated. 
Adjust the tests. 


Patch:
diff --git a/src/main/java/com/google/firebase/messaging/Notification.java b/src/main/java/com/google/firebase/messaging/Notification.java
index 866c066..d9b2034 100644
--- a/src/main/java/com/google/firebase/messaging/Notification.java
+++ b/src/main/java/com/google/firebase/messaging/Notification.java
@@ -38,17 +38,21 @@ 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;
@@ -56,4 +60,71 @@ public class Notification {
     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);
+    }
+  }
+
 }


Test patch:
diff --git a/src/test/java/com/google/firebase/messaging/FirebaseMessagingIT.java b/src/test/java/com/google/firebase/messaging/FirebaseMessagingIT.java
index 494923f..8951d7c 100644
--- a/src/test/java/com/google/firebase/messaging/FirebaseMessagingIT.java
+++ b/src/test/java/com/google/firebase/messaging/FirebaseMessagingIT.java
@@ -45,7 +45,11 @@ public class FirebaseMessagingIT {
   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())
diff --git a/src/test/java/com/google/firebase/messaging/MessageTest.java b/src/test/java/com/google/firebase/messaging/MessageTest.java
index 2db28d5..556f2d9 100644
--- a/src/test/java/com/google/firebase/messaging/MessageTest.java
+++ b/src/test/java/com/google/firebase/messaging/MessageTest.java
@@ -88,7 +88,7 @@ public class MessageTest {
   }
 
   @Test
-  public void testNotificationMessage() throws IOException {
+  public void testNotificationMessageDeprecatedConstructor() throws IOException {
     Message message = Message.builder()
         .setNotification(new Notification("title", "body"))
         .setTopic("test-topic")
@@ -97,6 +97,16 @@ public class MessageTest {
     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()
@@ -703,7 +713,7 @@ public class MessageTest {
   }
 
   @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")
@@ -713,6 +723,21 @@ public class MessageTest {
     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()