diff --git a/src/main/java/com/google/firebase/messaging/AndroidNotification.java b/src/main/java/com/google/firebase/messaging/AndroidNotification.java index 0a6cd062..b341603e 100644 --- a/src/main/java/com/google/firebase/messaging/AndroidNotification.java +++ b/src/main/java/com/google/firebase/messaging/AndroidNotification.java @@ -66,6 +66,9 @@ public class AndroidNotification { @Key("channel_id") private final String channelId; + + @Key("image") + private final String image; private AndroidNotification(Builder builder) { this.title = builder.title; @@ -97,6 +100,7 @@ private AndroidNotification(Builder builder) { this.titleLocArgs = null; } this.channelId = builder.channelId; + this.image = builder.image; } /** @@ -122,6 +126,7 @@ public static class Builder { private String titleLocKey; private List titleLocArgs = new ArrayList<>(); private String channelId; + private String image; private Builder() {} @@ -292,6 +297,18 @@ public Builder setChannelId(String channelId) { return this; } + /** + * Sets the URL of the image that is going to be displayed in the notification. When provided, + * overrides the imageUrl set via {@link 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 AndroidNotification} instance from the parameters set on this builder. * diff --git a/src/main/java/com/google/firebase/messaging/ApnsFcmOptions.java b/src/main/java/com/google/firebase/messaging/ApnsFcmOptions.java index 708c0b56..46bc5702 100644 --- a/src/main/java/com/google/firebase/messaging/ApnsFcmOptions.java +++ b/src/main/java/com/google/firebase/messaging/ApnsFcmOptions.java @@ -24,10 +24,14 @@ public final class ApnsFcmOptions { @Key("analytics_label") private final String analyticsLabel; + + @Key("image") + private final String image; private ApnsFcmOptions(Builder builder) { FcmOptionsUtil.checkAnalyticsLabel(builder.analyticsLabel); this.analyticsLabel = builder.analyticsLabel; + this.image = builder.image; } /** @@ -53,6 +57,8 @@ public static class Builder { private String analyticsLabel; + private String image; + private Builder() {} /** @@ -64,6 +70,15 @@ public Builder setAnalyticsLabel(String analyticsLabel) { return this; } + /** + * @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 ApnsFcmOptions} instance from the parameters set on this builder. * diff --git a/src/main/java/com/google/firebase/messaging/Notification.java b/src/main/java/com/google/firebase/messaging/Notification.java index eaa1e144..866c0668 100644 --- a/src/main/java/com/google/firebase/messaging/Notification.java +++ b/src/main/java/com/google/firebase/messaging/Notification.java @@ -29,6 +29,9 @@ public class Notification { @Key("body") private final String body; + + @Key("image") + private final String image; /** * Creates a new {@code Notification} using the given title and body. @@ -37,8 +40,20 @@ public class Notification { * @param body Body of the notification. */ 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. + */ + public Notification(String title, String body, String imageUrl) { this.title = title; this.body = body; + this.image = imageUrl; } } diff --git a/src/test/java/com/google/firebase/messaging/FirebaseMessagingIT.java b/src/test/java/com/google/firebase/messaging/FirebaseMessagingIT.java index 9a2b3ff8..da4320d7 100644 --- a/src/test/java/com/google/firebase/messaging/FirebaseMessagingIT.java +++ b/src/test/java/com/google/firebase/messaging/FirebaseMessagingIT.java @@ -34,6 +34,7 @@ public class FirebaseMessagingIT { private static final String TEST_REGISTRATION_TOKEN = "fGw0qy4TGgk:APA91bGtWGjuhp4WRhHXgbabIYp1jxEKI08ofj_v1bKhWAGJQ4e3arRCWzeTfHaLz83mBnDh0a" + "PWB1AykXAVUUGl2h1wT4XI6XazWpvY7RBUSYfoxtqSWGIm2nvWh2BOP1YG501SsRoE"; + private static final String TEST_IMAGE_URL = "https://example.com/image.png"; @BeforeClass public static void setUpClass() { @@ -44,7 +45,7 @@ public static void setUpClass() { public void testSend() throws Exception { FirebaseMessaging messaging = FirebaseMessaging.getInstance(); Message message = Message.builder() - .setNotification(new Notification("Title", "Body")) + .setNotification(new Notification("Title", "Body", TEST_IMAGE_URL)) .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 5f555fe3..b33c5874 100644 --- a/src/test/java/com/google/firebase/messaging/MessageTest.java +++ b/src/test/java/com/google/firebase/messaging/MessageTest.java @@ -36,6 +36,10 @@ public class MessageTest { + private static final String TEST_IMAGE_URL = "https://example.com/image.png"; + private static final String TEST_IMAGE_URL_ANDROID = "https://example.com/android-image.png"; + private static final String TEST_IMAGE_URL_APNS = "https://example.com/apns-image.png"; + @Test(expected = IllegalArgumentException.class) public void testMessageWithoutTarget() { Message.builder().build(); @@ -678,6 +682,76 @@ public void testIncorrectAnalyticsLabelFormat() { } } + @Test + public void testImageInNotification() throws IOException { + Message message = Message.builder() + .setNotification(new Notification("title", "body", TEST_IMAGE_URL)) + .setTopic("test-topic") + .build(); + Map 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() + .setNotification(new Notification("title", "body", TEST_IMAGE_URL)) + .setAndroidConfig(AndroidConfig.builder() + .setNotification(AndroidNotification.builder() + .setTitle("android-title") + .setBody("android-body") + .setImage(TEST_IMAGE_URL_ANDROID) + .build()) + .build()) + .setTopic("test-topic") + .build(); + Map notification = ImmutableMap.builder() + .put("title", "title") + .put("body", "body") + .put("image", TEST_IMAGE_URL) + .build(); + Map androidConfig = ImmutableMap.builder() + .put("notification", ImmutableMap.builder() + .put("title", "android-title") + .put("body", "android-body") + .put("image", TEST_IMAGE_URL_ANDROID) + .build()) + .build(); + assertJsonEquals(ImmutableMap.of( + "topic", "test-topic", "notification", notification, "android", androidConfig), message); + } + + @Test + public void testImageInApnsNotification() throws IOException { + Message message = Message.builder() + .setTopic("test-topic") + .setNotification(new Notification("title", "body", TEST_IMAGE_URL)) + .setApnsConfig( + ApnsConfig.builder().setAps(Aps.builder().build()) + .setFcmOptions(ApnsFcmOptions.builder().setImage(TEST_IMAGE_URL_APNS).build()) + .build()).build(); + + ImmutableMap notification = + ImmutableMap.builder() + .put("title", "title") + .put("body", "body") + .put("image", TEST_IMAGE_URL) + .build(); + ImmutableMap apnsConfig = + ImmutableMap.builder() + .put("fcm_options", ImmutableMap.of("image", TEST_IMAGE_URL_APNS)) + .put("payload", ImmutableMap.of("aps", ImmutableMap.of())) + .build(); + ImmutableMap expected = + ImmutableMap.builder() + .put("topic", "test-topic") + .put("notification", notification) + .put("apns", apnsConfig) + .build(); + assertJsonEquals(expected, message); + } + private static void assertJsonEquals( Map expected, Object actual) throws IOException { assertEquals(expected, toMap(actual));