Skip to content

Commit cfb1989

Browse files
authored
feat(fcm): Add image in notification support. (#298)
* Add image in notification support. * Fix indentation * change param name from image to imageUrl * Fix minor issues * fix tests
1 parent fab1435 commit cfb1989

File tree

5 files changed

+123
-1
lines changed

5 files changed

+123
-1
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public class AndroidNotification {
6666

6767
@Key("channel_id")
6868
private final String channelId;
69+
70+
@Key("image")
71+
private final String image;
6972

7073
private AndroidNotification(Builder builder) {
7174
this.title = builder.title;
@@ -97,6 +100,7 @@ private AndroidNotification(Builder builder) {
97100
this.titleLocArgs = null;
98101
}
99102
this.channelId = builder.channelId;
103+
this.image = builder.image;
100104
}
101105

102106
/**
@@ -122,6 +126,7 @@ public static class Builder {
122126
private String titleLocKey;
123127
private List<String> titleLocArgs = new ArrayList<>();
124128
private String channelId;
129+
private String image;
125130

126131
private Builder() {}
127132

@@ -292,6 +297,18 @@ public Builder setChannelId(String channelId) {
292297
return this;
293298
}
294299

300+
/**
301+
* Sets the URL of the image that is going to be displayed in the notification. When provided,
302+
* overrides the imageUrl set via {@link Notification}.
303+
*
304+
* @param imageUrl URL of the image that is going to be displayed in the notification.
305+
* @return This builder.
306+
*/
307+
public Builder setImage(String imageUrl) {
308+
this.image = imageUrl;
309+
return this;
310+
}
311+
295312
/**
296313
* Creates a new {@link AndroidNotification} instance from the parameters set on this builder.
297314
*

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ public final class ApnsFcmOptions {
2424

2525
@Key("analytics_label")
2626
private final String analyticsLabel;
27+
28+
@Key("image")
29+
private final String image;
2730

2831
private ApnsFcmOptions(Builder builder) {
2932
FcmOptionsUtil.checkAnalyticsLabel(builder.analyticsLabel);
3033
this.analyticsLabel = builder.analyticsLabel;
34+
this.image = builder.image;
3135
}
3236

3337
/**
@@ -53,6 +57,8 @@ public static class Builder {
5357

5458
private String analyticsLabel;
5559

60+
private String image;
61+
5662
private Builder() {}
5763

5864
/**
@@ -64,6 +70,15 @@ public Builder setAnalyticsLabel(String analyticsLabel) {
6470
return this;
6571
}
6672

73+
/**
74+
* @param imageUrl URL of the image that is going to be displayed in the notification.
75+
* @return This builder
76+
*/
77+
public Builder setImage(String imageUrl) {
78+
this.image = imageUrl;
79+
return this;
80+
}
81+
6782
/**
6883
* Creates a new {@link ApnsFcmOptions} instance from the parameters set on this builder.
6984
*

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public class Notification {
2929

3030
@Key("body")
3131
private final String body;
32+
33+
@Key("image")
34+
private final String image;
3235

3336
/**
3437
* Creates a new {@code Notification} using the given title and body.
@@ -37,8 +40,20 @@ public class Notification {
3740
* @param body Body of the notification.
3841
*/
3942
public Notification(String title, String body) {
43+
this(title, body, null);
44+
}
45+
46+
/**
47+
* Creates a new {@code Notification} using the given title, body, and image.
48+
*
49+
* @param title Title of the notification.
50+
* @param body Body of the notification.
51+
* @param imageUrl URL of the image that is going to be displayed in the notification.
52+
*/
53+
public Notification(String title, String body, String imageUrl) {
4054
this.title = title;
4155
this.body = body;
56+
this.image = imageUrl;
4257
}
4358

4459
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class FirebaseMessagingIT {
3434
private static final String TEST_REGISTRATION_TOKEN =
3535
"fGw0qy4TGgk:APA91bGtWGjuhp4WRhHXgbabIYp1jxEKI08ofj_v1bKhWAGJQ4e3arRCWzeTfHaLz83mBnDh0a"
3636
+ "PWB1AykXAVUUGl2h1wT4XI6XazWpvY7RBUSYfoxtqSWGIm2nvWh2BOP1YG501SsRoE";
37+
private static final String TEST_IMAGE_URL = "https://example.com/image.png";
3738

3839
@BeforeClass
3940
public static void setUpClass() {
@@ -44,7 +45,7 @@ public static void setUpClass() {
4445
public void testSend() throws Exception {
4546
FirebaseMessaging messaging = FirebaseMessaging.getInstance();
4647
Message message = Message.builder()
47-
.setNotification(new Notification("Title", "Body"))
48+
.setNotification(new Notification("Title", "Body", TEST_IMAGE_URL))
4849
.setAndroidConfig(AndroidConfig.builder()
4950
.setRestrictedPackageName("com.google.firebase.testing")
5051
.build())

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636

3737
public class MessageTest {
3838

39+
private static final String TEST_IMAGE_URL = "https://example.com/image.png";
40+
private static final String TEST_IMAGE_URL_ANDROID = "https://example.com/android-image.png";
41+
private static final String TEST_IMAGE_URL_APNS = "https://example.com/apns-image.png";
42+
3943
@Test(expected = IllegalArgumentException.class)
4044
public void testMessageWithoutTarget() {
4145
Message.builder().build();
@@ -698,6 +702,76 @@ public void testIncorrectAnalyticsLabelFormat() {
698702
}
699703
}
700704

705+
@Test
706+
public void testImageInNotification() throws IOException {
707+
Message message = Message.builder()
708+
.setNotification(new Notification("title", "body", TEST_IMAGE_URL))
709+
.setTopic("test-topic")
710+
.build();
711+
Map<String, String> data = ImmutableMap.of(
712+
"title", "title", "body", "body", "image", TEST_IMAGE_URL);
713+
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "notification", data), message);
714+
}
715+
716+
@Test
717+
public void testImageInAndroidNotification() throws IOException {
718+
Message message = Message.builder()
719+
.setNotification(new Notification("title", "body", TEST_IMAGE_URL))
720+
.setAndroidConfig(AndroidConfig.builder()
721+
.setNotification(AndroidNotification.builder()
722+
.setTitle("android-title")
723+
.setBody("android-body")
724+
.setImage(TEST_IMAGE_URL_ANDROID)
725+
.build())
726+
.build())
727+
.setTopic("test-topic")
728+
.build();
729+
Map<String, Object> notification = ImmutableMap.<String, Object>builder()
730+
.put("title", "title")
731+
.put("body", "body")
732+
.put("image", TEST_IMAGE_URL)
733+
.build();
734+
Map<String, Object> androidConfig = ImmutableMap.<String, Object>builder()
735+
.put("notification", ImmutableMap.<String, Object>builder()
736+
.put("title", "android-title")
737+
.put("body", "android-body")
738+
.put("image", TEST_IMAGE_URL_ANDROID)
739+
.build())
740+
.build();
741+
assertJsonEquals(ImmutableMap.of(
742+
"topic", "test-topic", "notification", notification, "android", androidConfig), message);
743+
}
744+
745+
@Test
746+
public void testImageInApnsNotification() throws IOException {
747+
Message message = Message.builder()
748+
.setTopic("test-topic")
749+
.setNotification(new Notification("title", "body", TEST_IMAGE_URL))
750+
.setApnsConfig(
751+
ApnsConfig.builder().setAps(Aps.builder().build())
752+
.setFcmOptions(ApnsFcmOptions.builder().setImage(TEST_IMAGE_URL_APNS).build())
753+
.build()).build();
754+
755+
ImmutableMap<String, Object> notification =
756+
ImmutableMap.<String, Object>builder()
757+
.put("title", "title")
758+
.put("body", "body")
759+
.put("image", TEST_IMAGE_URL)
760+
.build();
761+
ImmutableMap<String, Object> apnsConfig =
762+
ImmutableMap.<String, Object>builder()
763+
.put("fcm_options", ImmutableMap.of("image", TEST_IMAGE_URL_APNS))
764+
.put("payload", ImmutableMap.of("aps", ImmutableMap.of()))
765+
.build();
766+
ImmutableMap<String, Object> expected =
767+
ImmutableMap.<String, Object>builder()
768+
.put("topic", "test-topic")
769+
.put("notification", notification)
770+
.put("apns", apnsConfig)
771+
.build();
772+
assertJsonEquals(expected, message);
773+
}
774+
701775
private static void assertJsonEquals(
702776
Map expected, Object actual) throws IOException {
703777
assertEquals(expected, toMap(actual));

0 commit comments

Comments
 (0)