Skip to content
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

Exposing message list limit for external usage #517

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 12 additions & 6 deletions src/main/java/com/google/firebase/messaging/FirebaseMessaging.java
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,8 @@ 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 +174,8 @@ 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 +190,8 @@ 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 +202,8 @@ 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 +292,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
17 changes: 11 additions & 6 deletions src/main/java/com/google/firebase/messaging/MulticastMessage.java
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,8 @@
/**
* 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 +58,8 @@ 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,8 +110,9 @@ 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 {@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.
* @return This builder.
Expand All @@ -119,8 +123,9 @@ public Builder addToken(@NonNull String token) {
}

/**
* 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 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.
* @return This builder.
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