Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(fcm): Add AndroidConfigV2 support to MulticastMessage
  • Loading branch information
jonathanedey committed Jul 14, 2026
commit 281974a06f1405ddee9aac24864f1074709f71ea
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

import static com.google.common.base.Preconditions.checkArgument;

import com.google.api.client.util.Strings;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Booleans;
import com.google.firebase.internal.NonNull;

import java.util.Collection;
Expand Down Expand Up @@ -52,7 +53,9 @@ public class MulticastMessage {
private final List<String> fids;
private final Map<String, String> data;
private final Notification notification;
@Deprecated
private final AndroidConfig androidConfig;
private final AndroidConfigV2 androidConfigV2;
private final WebpushConfig webpushConfig;
private final ApnsConfig apnsConfig;
private final FcmOptions fcmOptions;
Expand All @@ -71,9 +74,16 @@ private MulticastMessage(Builder builder) {
for (String fid : this.fids) {
checkArgument(!Strings.isNullOrEmpty(fid), "none of the fids can be null or empty");
}
int androidConfigs = Booleans.countTrue(
builder.androidConfig != null,
builder.androidConfigV2 != null
);
checkArgument(androidConfigs <= 1,
"androidConfig and androidConfigV2 are mutually exclusive");
this.data = builder.data.isEmpty() ? null : ImmutableMap.copyOf(builder.data);
this.notification = builder.notification;
this.androidConfig = builder.androidConfig;
this.androidConfigV2 = builder.androidConfigV2;
this.webpushConfig = builder.webpushConfig;
this.apnsConfig = builder.apnsConfig;
this.fcmOptions = builder.fcmOptions;
Expand Down Expand Up @@ -103,6 +113,7 @@ private Message.Builder getMetadataBuilder() {
Message.Builder builder = Message.builder()
.setNotification(this.notification)
.setAndroidConfig(this.androidConfig)
.setAndroidConfigV2(this.androidConfigV2)
.setApnsConfig(this.apnsConfig)
.setWebpushConfig(this.webpushConfig)
.setFcmOptions(this.fcmOptions);
Expand All @@ -128,7 +139,9 @@ public static class Builder {
private final ImmutableList.Builder<String> fids = ImmutableList.builder();
private final Map<String, String> data = new HashMap<>();
private Notification notification;
@Deprecated
private AndroidConfig androidConfig;
private AndroidConfigV2 androidConfigV2;
private WebpushConfig webpushConfig;
private ApnsConfig apnsConfig;
private FcmOptions fcmOptions;
Expand Down Expand Up @@ -207,12 +220,25 @@ public Builder setNotification(Notification notification) {
*
* @param androidConfig An {@link AndroidConfig} instance.
* @return This builder.
* @deprecated Use {@link #setAndroidConfigV2(AndroidConfigV2)} instead.
*/
@Deprecated
public Builder setAndroidConfig(AndroidConfig androidConfig) {
this.androidConfig = androidConfig;
return this;
}

/**
* Sets the Android-specific information to be included in the message.
*
* @param androidConfigV2 An {@link AndroidConfigV2} instance.
* @return This builder.
*/
public Builder setAndroidConfigV2(AndroidConfigV2 androidConfigV2) {
this.androidConfigV2 = androidConfigV2;
return this;
}

/**
* Sets the Webpush-specific information to be included in the message.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class MulticastMessageTest {
private static final AndroidConfig ANDROID = AndroidConfig.builder()
.setCollapseKey("collapseKey")
.build();
private static final AndroidConfigV2 ANDROID_V2 = AndroidConfigV2.builder()
.setCollapseKey("collapseKeyV2")
.build();
private static final ApnsConfig APNS = ApnsConfig.builder()
.setAps(Aps.builder()
.setBadge(42)
Expand Down Expand Up @@ -72,6 +75,50 @@ public void testNoTokens() {
MulticastMessage.builder().build();
}

@Test
public void testMulticastMessageWithAndroidConfigV2() {
MulticastMessage multicastMessage = MulticastMessage.builder()
.setAndroidConfigV2(ANDROID_V2)
.setApnsConfig(APNS)
.setWebpushConfig(WEBPUSH)
.setNotification(NOTIFICATION)
.setFcmOptions(FCM_OPTIONS)
.putData("key1", "value1")
.putAllData(ImmutableMap.of("key2", "value2"))
.addFid("fid1")
.addAllFids(ImmutableList.of("fid2", "fid3"))
.build();

List<Message> messages = multicastMessage.getMessageList();

assertEquals(3, messages.size());
for (int i = 0; i < 3; i++) {
Message message = messages.get(i);
assertSame(ANDROID_V2, message.getAndroidConfigV2());
assertSame(APNS, message.getApnsConfig());
assertSame(WEBPUSH, message.getWebpushConfig());
assertSame(NOTIFICATION, message.getNotification());
assertSame(FCM_OPTIONS, message.getFcmOptions());
assertEquals(ImmutableMap.of("key1", "value1", "key2", "value2"), message.getData());
assertEquals("fid" + (i + 1), message.getFid());
}
}

@Test
public void testMultipleAndroidConfigs() {
try {
MulticastMessage.builder()
.setAndroidConfig(ANDROID)
.setAndroidConfigV2(ANDROID_V2)
.addToken("token1")
.build();
fail("No error thrown for multiple android configs");
} catch (IllegalArgumentException expected) {
assertEquals("androidConfig and androidConfigV2 are mutually exclusive",
expected.getMessage());
}
}

@Test
public void testTooManyTokens() {
MulticastMessage.Builder builder = MulticastMessage.builder();
Expand Down
Loading