-
Notifications
You must be signed in to change notification settings - Fork 306
feat(fcm): Enable fid and deprecate token for Send API
#1211
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
base: main
Are you sure you want to change the base?
Changes from all commits
14537af
b75992d
57e3ccc
da5efd9
d76240f
e3f510b
8cf57be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,22 +30,26 @@ | |
|
|
||
| /** | ||
| * Represents a message that can be sent to multiple devices via Firebase Cloud Messaging (FCM). | ||
| * Contains payload information as well as the list of device registration tokens to which the | ||
| * message should be sent. A single {@code MulticastMessage} may contain up to 500 registration | ||
| * tokens. | ||
| * Contains payload information as well as the list of device registration tokens and/or | ||
| * Firebase Installation IDs (FIDs) to which the message should be sent. A single | ||
| * {@code MulticastMessage} may contain up to 500 registration tokens and FIDs combined. | ||
| * | ||
| * <p>Instances of this class are thread-safe and immutable. Use {@link MulticastMessage.Builder} | ||
| * to create new instances. See {@link FirebaseMessaging#sendMulticast(MulticastMessage)} for | ||
| * details on how to send the message to FCM for multicast delivery. | ||
| * | ||
| * <p>This class and the associated Builder retain the order of tokens. Therefore the order of | ||
| * the responses list obtained by calling {@link BatchResponse#getResponses()} on the return value | ||
| * of {@link FirebaseMessaging#sendMulticast(MulticastMessage)} corresponds to the order in which | ||
| * tokens were added to the {@link MulticastMessage.Builder}. | ||
| * <p>This class and the associated Builder retain the order of tokens and FIDs. Therefore | ||
| * the order of the responses list obtained by calling {@link BatchResponse#getResponses()} | ||
| * on the return value of {@link FirebaseMessaging#sendMulticast(MulticastMessage)} | ||
| * corresponds to the order in which targets were added to the | ||
| * {@link MulticastMessage.Builder}. If both tokens and FIDs are provided, tokens are | ||
| * processed first, followed by FIDs. | ||
| */ | ||
| public class MulticastMessage { | ||
|
|
||
| @Deprecated | ||
| private final List<String> tokens; | ||
| private final List<String> fids; | ||
|
yvonnep165 marked this conversation as resolved.
|
||
| private final Map<String, String> data; | ||
| private final Notification notification; | ||
| private final AndroidConfig androidConfig; | ||
|
|
@@ -55,11 +59,18 @@ public class MulticastMessage { | |
|
|
||
| private MulticastMessage(Builder builder) { | ||
| this.tokens = builder.tokens.build(); | ||
| checkArgument(!this.tokens.isEmpty(), "at least one token must be specified"); | ||
| checkArgument(this.tokens.size() <= 500, "no more than 500 tokens can be specified"); | ||
| this.fids = builder.fids.build(); | ||
| int tokensSize = this.tokens.size(); | ||
| int fidsSize = this.fids.size(); | ||
| checkArgument(tokensSize + fidsSize > 0, "at least one token or fid must be specified"); | ||
| checkArgument(tokensSize + fidsSize <= 500, | ||
| "no more than 500 tokens and fids combined can be specified"); | ||
| for (String token : this.tokens) { | ||
| checkArgument(!Strings.isNullOrEmpty(token), "none of the tokens can be null or empty"); | ||
| } | ||
| for (String fid : this.fids) { | ||
| checkArgument(!Strings.isNullOrEmpty(fid), "none of the fids can be null or empty"); | ||
| } | ||
| this.data = builder.data.isEmpty() ? null : ImmutableMap.copyOf(builder.data); | ||
| this.notification = builder.notification; | ||
| this.androidConfig = builder.androidConfig; | ||
|
|
@@ -69,6 +80,26 @@ private MulticastMessage(Builder builder) { | |
| } | ||
|
|
||
| List<Message> getMessageList() { | ||
| ImmutableList.Builder<Message> messages = ImmutableList.builder(); | ||
|
|
||
| if (!this.tokens.isEmpty()) { | ||
| Message.Builder tokenBuilder = getMetadataBuilder(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: it seems we only need one builder
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review! If we only use one builder, calling
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. Thanks for the explanation! |
||
| for (String token : this.tokens) { | ||
| messages.add(tokenBuilder.setToken(token).build()); | ||
| } | ||
| } | ||
|
|
||
| if (!this.fids.isEmpty()) { | ||
| Message.Builder fidBuilder = getMetadataBuilder(); | ||
| for (String fid : this.fids) { | ||
| messages.add(fidBuilder.setFid(fid).build()); | ||
| } | ||
| } | ||
|
|
||
| return messages.build(); | ||
| } | ||
|
yvonnep165 marked this conversation as resolved.
|
||
|
|
||
| private Message.Builder getMetadataBuilder() { | ||
| Message.Builder builder = Message.builder() | ||
| .setNotification(this.notification) | ||
| .setAndroidConfig(this.androidConfig) | ||
|
|
@@ -78,11 +109,7 @@ List<Message> getMessageList() { | |
| if (this.data != null) { | ||
| builder.putAllData(this.data); | ||
| } | ||
| ImmutableList.Builder<Message> messages = ImmutableList.builder(); | ||
| for (String token : this.tokens) { | ||
| messages.add(builder.setToken(token).build()); | ||
| } | ||
| return messages.build(); | ||
| return builder; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -96,7 +123,9 @@ public static Builder builder() { | |
|
|
||
| public static class Builder { | ||
|
|
||
| @Deprecated | ||
| private final ImmutableList.Builder<String> tokens = ImmutableList.builder(); | ||
| private final ImmutableList.Builder<String> fids = ImmutableList.builder(); | ||
| private final Map<String, String> data = new HashMap<>(); | ||
| private Notification notification; | ||
| private AndroidConfig androidConfig; | ||
|
|
@@ -107,29 +136,61 @@ public static class Builder { | |
| private Builder() {} | ||
|
|
||
| /** | ||
| * Adds a token to which the message should be sent. Up to 500 tokens can be specified on | ||
| * a single instance of {@link MulticastMessage}. | ||
| * Adds a token to which the message should be sent. Up to 500 tokens | ||
| * and FIDs combined can be specified on a single instance of | ||
| * {@link MulticastMessage}. | ||
| * | ||
| * @param token A non-null, non-empty Firebase device registration token. | ||
| * @return This builder. | ||
| * @deprecated Use {@link #addFid(String)} instead. | ||
| */ | ||
| @Deprecated | ||
| public Builder addToken(@NonNull String token) { | ||
| this.tokens.add(token); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds a collection of tokens to which the message should be sent. Up to 500 tokens can be | ||
| * specified on a single instance of {@link MulticastMessage}. | ||
| * Adds a Firebase Installation ID (FID) to which the message should be sent. | ||
| * Up to 500 tokens and FIDs combined can be specified on a single instance | ||
| * of {@link MulticastMessage}. | ||
| * | ||
| * @param fid A non-null, non-empty Firebase Installation ID. | ||
| * @return This builder. | ||
| */ | ||
| public Builder addFid(@NonNull String fid) { | ||
| this.fids.add(fid); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds a collection of tokens to which the message should be sent. Up to 500 | ||
| * tokens and FIDs combined can be specified on a single instance of | ||
| * {@link MulticastMessage}. | ||
| * | ||
| * @param tokens Collection of Firebase device registration tokens. | ||
| * @return This builder. | ||
| * @deprecated Use {@link #addAllFids(Collection)} instead. | ||
| */ | ||
| @Deprecated | ||
| public Builder addAllTokens(@NonNull Collection<String> tokens) { | ||
| this.tokens.addAll(tokens); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds a collection of Firebase Installation IDs (FIDs) to which the message | ||
| * should be sent. Up to 500 tokens and FIDs combined can be specified on a | ||
| * single instance of {@link MulticastMessage}. | ||
| * | ||
| * @param fids Collection of Firebase Installation IDs. | ||
| * @return This builder. | ||
| */ | ||
| public Builder addAllFids(@NonNull Collection<String> fids) { | ||
| this.fids.addAll(fids); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the notification information to be included in the message. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Javadoc states that the response order corresponds to the order in which targets were added to the builder. However, the implementation groups tokens first and then FIDs (as noted in the subsequent sentence), which means the response order will not match the addition order if tokens and FIDs were interleaved. The documentation should be clarified to reflect that the order is preserved within each group (tokens then FIDs).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Such statement is included at the end as "If both tokens and FIDs are provided, tokens are processed first, followed by FIDs."