Skip to content

Commit

Permalink
Exposing message list limit for external usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Shmatko committed Mar 9, 2021
1 parent af6f3d9 commit 76ffdaa
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
*/
public class FirebaseMessaging {

public static final int MAX_MESSAGES_IN_LIST = 500;

private final FirebaseApp app;
private final Supplier<? extends FirebaseMessagingClient> messagingClient;
private final Supplier<? extends InstanceIdClient> instanceIdClient;
Expand Down Expand Up @@ -147,7 +149,7 @@ protected String execute() throws FirebaseMessagingException {
* <p>The responses list obtained by calling {@link BatchResponse#getResponses()} on the return
* value corresponds to the order of input messages.
*
* @param messages A non-null, non-empty list containing up to 500 messages.
* @param messages A non-null, non-empty list containing up to {@value #MAX_MESSAGES_IN_LIST} messages.
* @return A {@link BatchResponse} indicating the result of the operation.
* @throws FirebaseMessagingException If an error occurs while handing the messages off to FCM for
* delivery. An exception here indicates a total failure -- i.e. none of the messages in the
Expand All @@ -171,7 +173,7 @@ public BatchResponse sendAll(
* <p>The responses list obtained by calling {@link BatchResponse#getResponses()} on the return
* value corresponds to the order of input messages.
*
* @param messages A non-null, non-empty list containing up to 500 messages.
* @param messages A non-null, non-empty list containing up to {@value #MAX_MESSAGES_IN_LIST} messages.
* @param dryRun A boolean indicating whether to perform a dry run (validation only) of the send.
* @return A {@link BatchResponse} indicating the result of the operation.
* @throws FirebaseMessagingException If an error occurs while handing the messages off to FCM for
Expand All @@ -186,7 +188,7 @@ public BatchResponse sendAll(
/**
* Similar to {@link #sendAll(List)} but performs the operation asynchronously.
*
* @param messages A non-null, non-empty list containing up to 500 messages.
* @param messages A non-null, non-empty list containing up to {@value #MAX_MESSAGES_IN_LIST} messages.
* @return @return An {@code ApiFuture} that will complete with a {@link BatchResponse} when
* the messages have been sent.
*/
Expand All @@ -197,7 +199,7 @@ public ApiFuture<BatchResponse> sendAllAsync(@NonNull List<Message> messages) {
/**
* Similar to {@link #sendAll(List, boolean)} but performs the operation asynchronously.
*
* @param messages A non-null, non-empty list containing up to 500 messages.
* @param messages A non-null, non-empty list containing up to {@value #MAX_MESSAGES_IN_LIST} messages.
* @param dryRun A boolean indicating whether to perform a dry run (validation only) of the send.
* @return @return An {@code ApiFuture} that will complete with a {@link BatchResponse} when
* the messages have been sent, or when the emulation has finished.
Expand Down Expand Up @@ -286,8 +288,8 @@ private CallableOperation<BatchResponse, FirebaseMessagingException> sendAllOp(

final List<Message> immutableMessages = ImmutableList.copyOf(messages);
checkArgument(!immutableMessages.isEmpty(), "messages list must not be empty");
checkArgument(immutableMessages.size() <= 500,
"messages list must not contain more than 500 elements");
checkArgument(immutableMessages.size() <= MAX_MESSAGES_IN_LIST,
"messages list must not contain more than " + MAX_MESSAGES_IN_LIST + " elements");
final FirebaseMessagingClient messagingClient = getMessagingClient();
return new CallableOperation<BatchResponse,FirebaseMessagingException>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.firebase.messaging;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.firebase.messaging.FirebaseMessaging.MAX_MESSAGES_IN_LIST;

import com.google.api.client.util.Strings;
import com.google.common.collect.ImmutableList;
Expand All @@ -31,7 +32,7 @@
/**
* 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
* message should be sent. A single {@code MulticastMessage} may contain up to {@value com.google.firebase.messaging.FirebaseMessaging#MAX_MESSAGES_IN_LIST} registration
* tokens.
*
* <p>Instances of this class are thread-safe and immutable. Use {@link MulticastMessage.Builder}
Expand All @@ -56,7 +57,7 @@ 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");
checkArgument(this.tokens.size() <= MAX_MESSAGES_IN_LIST, "no more than " + MAX_MESSAGES_IN_LIST + " tokens can be specified");
for (String token : this.tokens) {
checkArgument(!Strings.isNullOrEmpty(token), "none of the tokens can be null or empty");
}
Expand Down Expand Up @@ -107,7 +108,7 @@ public static class Builder {
private Builder() {}

/**
* Adds a token to which the message should be sent. Up to 500 tokens can be specified on
* Adds a token to which the message should be sent. Up to {@value com.google.firebase.messaging.FirebaseMessaging#MAX_MESSAGES_IN_LIST} tokens can be specified on
* a single instance of {@link MulticastMessage}.
*
* @param token A non-null, non-empty Firebase device registration token.
Expand All @@ -119,7 +120,7 @@ public Builder addToken(@NonNull String token) {
}

/**
* Adds a collection of tokens to which the message should be sent. Up to 500 tokens can be
* Adds a collection of tokens to which the message should be sent. Up to {@value com.google.firebase.messaging.FirebaseMessaging#MAX_MESSAGES_IN_LIST} tokens can be
* specified on a single instance of {@link MulticastMessage}.
*
* @param tokens Collection of Firebase device registration tokens.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.firebase.messaging;

import static com.google.firebase.messaging.FirebaseMessaging.MAX_MESSAGES_IN_LIST;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -147,16 +148,16 @@ public void testSendAll() throws Exception {
}

@Test
public void testSendFiveHundred() throws Exception {
public void testSendMaximumAmountOfMessages() throws Exception {
List<Message> messages = new ArrayList<>();
for (int i = 0; i < 500; i++) {
for (int i = 0; i < MAX_MESSAGES_IN_LIST; i++) {
messages.add(Message.builder().setTopic("foo-bar-" + (i % 10)).build());
}

BatchResponse response = FirebaseMessaging.getInstance().sendAll(messages, true);

assertEquals(500, response.getResponses().size());
assertEquals(500, response.getSuccessCount());
assertEquals(MAX_MESSAGES_IN_LIST, response.getResponses().size());
assertEquals(MAX_MESSAGES_IN_LIST, response.getSuccessCount());
assertEquals(0, response.getFailureCount());
for (SendResponse sendResponse : response.getResponses()) {
if (!sendResponse.isSuccessful()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.firebase.messaging;

import static com.google.firebase.messaging.FirebaseMessaging.MAX_MESSAGES_IN_LIST;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -297,7 +298,7 @@ public void testSendAllWithTooManyMessages() throws FirebaseMessagingException {
MockFirebaseMessagingClient client = MockFirebaseMessagingClient.fromMessageId(null);
FirebaseMessaging messaging = getMessagingForSend(Suppliers.ofInstance(client));
ImmutableList.Builder<Message> listBuilder = ImmutableList.builder();
for (int i = 0; i < 501; i++) {
for (int i = 0; i < MAX_MESSAGES_IN_LIST + 1; i++) {
listBuilder.add(Message.builder().setTopic("topic").build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.firebase.messaging;

import static com.google.firebase.messaging.FirebaseMessaging.MAX_MESSAGES_IN_LIST;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void testNoTokens() {
@Test
public void testTooManyTokens() {
MulticastMessage.Builder builder = MulticastMessage.builder();
for (int i = 0; i < 501; i++) {
for (int i = 0; i < MAX_MESSAGES_IN_LIST + 1; i++) {
builder.addToken("token" + i);
}
try {
Expand Down

0 comments on commit 76ffdaa

Please sign in to comment.