Skip to content

feat(fcm): Add image in notification support. #298

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 5 commits into from
Aug 27, 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,6 +100,7 @@ private AndroidNotification(Builder builder) {
this.titleLocArgs = null;
}
this.channelId = builder.channelId;
this.image = builder.image;
}

/**
Expand All @@ -122,6 +126,7 @@ public static class Builder {
private String titleLocKey;
private List<String> titleLocArgs = new ArrayList<>();
private String channelId;
private String image;

private Builder() {}

Expand Down Expand Up @@ -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.
*
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/google/firebase/messaging/ApnsFcmOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -53,6 +57,8 @@ public static class Builder {

private String analyticsLabel;

private String image;

private Builder() {}

/**
Expand All @@ -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.
*
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/google/firebase/messaging/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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())
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/com/google/firebase/messaging/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<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()
.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<String, Object> notification = ImmutableMap.<String, Object>builder()
.put("title", "title")
.put("body", "body")
.put("image", TEST_IMAGE_URL)
.build();
Map<String, Object> androidConfig = ImmutableMap.<String, Object>builder()
.put("notification", ImmutableMap.<String, Object>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<String, Object> notification =
ImmutableMap.<String, Object>builder()
.put("title", "title")
.put("body", "body")
.put("image", TEST_IMAGE_URL)
.build();
ImmutableMap<String, Object> apnsConfig =
ImmutableMap.<String, Object>builder()
.put("fcm_options", ImmutableMap.of("image", TEST_IMAGE_URL_APNS))
.put("payload", ImmutableMap.of("aps", ImmutableMap.of()))
.build();
ImmutableMap<String, Object> expected =
ImmutableMap.<String, Object>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));
Expand Down