From 8f3766722b3841a713c975f993e4d7cdf300edca Mon Sep 17 00:00:00 2001 From: Oleksandr Shmatko Date: Tue, 9 Mar 2021 12:55:51 +0100 Subject: [PATCH 1/2] Exposing message list limit for external usage --- .../firebase/messaging/FirebaseMessaging.java | 14 ++++++++------ .../firebase/messaging/MulticastMessage.java | 9 +++++---- .../firebase/messaging/FirebaseMessagingIT.java | 9 +++++---- .../firebase/messaging/FirebaseMessagingTest.java | 3 ++- .../firebase/messaging/MulticastMessageTest.java | 3 ++- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/google/firebase/messaging/FirebaseMessaging.java b/src/main/java/com/google/firebase/messaging/FirebaseMessaging.java index 95366eaa7..c1437b479 100644 --- a/src/main/java/com/google/firebase/messaging/FirebaseMessaging.java +++ b/src/main/java/com/google/firebase/messaging/FirebaseMessaging.java @@ -41,6 +41,8 @@ */ public class FirebaseMessaging { + public static final int MAX_MESSAGES_IN_LIST = 500; + private final FirebaseApp app; private final Supplier messagingClient; private final Supplier instanceIdClient; @@ -147,7 +149,7 @@ protected String execute() throws FirebaseMessagingException { *

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 @@ -171,7 +173,7 @@ public BatchResponse sendAll( *

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 @@ -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. */ @@ -197,7 +199,7 @@ public ApiFuture sendAllAsync(@NonNull List 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. @@ -286,8 +288,8 @@ private CallableOperation sendAllOp( final List 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() { @Override diff --git a/src/main/java/com/google/firebase/messaging/MulticastMessage.java b/src/main/java/com/google/firebase/messaging/MulticastMessage.java index b15e58e5f..d5165cf5f 100644 --- a/src/main/java/com/google/firebase/messaging/MulticastMessage.java +++ b/src/main/java/com/google/firebase/messaging/MulticastMessage.java @@ -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; @@ -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. * *

Instances of this class are thread-safe and immutable. Use {@link MulticastMessage.Builder} @@ -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"); } @@ -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. @@ -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. diff --git a/src/test/java/com/google/firebase/messaging/FirebaseMessagingIT.java b/src/test/java/com/google/firebase/messaging/FirebaseMessagingIT.java index 9ae7bba24..16933ceaa 100644 --- a/src/test/java/com/google/firebase/messaging/FirebaseMessagingIT.java +++ b/src/test/java/com/google/firebase/messaging/FirebaseMessagingIT.java @@ -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; @@ -147,16 +148,16 @@ public void testSendAll() throws Exception { } @Test - public void testSendFiveHundred() throws Exception { + public void testSendMaximumAmountOfMessages() throws Exception { List 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()) { diff --git a/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java b/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java index 8e8e79e07..c0f892591 100644 --- a/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java +++ b/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java @@ -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; @@ -297,7 +298,7 @@ public void testSendAllWithTooManyMessages() throws FirebaseMessagingException { MockFirebaseMessagingClient client = MockFirebaseMessagingClient.fromMessageId(null); FirebaseMessaging messaging = getMessagingForSend(Suppliers.ofInstance(client)); ImmutableList.Builder 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()); } diff --git a/src/test/java/com/google/firebase/messaging/MulticastMessageTest.java b/src/test/java/com/google/firebase/messaging/MulticastMessageTest.java index f26d0816f..7828261e8 100644 --- a/src/test/java/com/google/firebase/messaging/MulticastMessageTest.java +++ b/src/test/java/com/google/firebase/messaging/MulticastMessageTest.java @@ -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; @@ -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 { From bb8e8eb6f54412dac99cba23271285238fcd9d2b Mon Sep 17 00:00:00 2001 From: Oleksandr Shmatko Date: Tue, 9 Mar 2021 13:22:27 +0100 Subject: [PATCH 2/2] Fixing issues related to a linter (line length) --- .../firebase/messaging/FirebaseMessaging.java | 12 ++++++++---- .../firebase/messaging/MulticastMessage.java | 16 ++++++++++------ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/google/firebase/messaging/FirebaseMessaging.java b/src/main/java/com/google/firebase/messaging/FirebaseMessaging.java index c1437b479..91106bc65 100644 --- a/src/main/java/com/google/firebase/messaging/FirebaseMessaging.java +++ b/src/main/java/com/google/firebase/messaging/FirebaseMessaging.java @@ -149,7 +149,8 @@ protected String execute() throws FirebaseMessagingException { *

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 {@value #MAX_MESSAGES_IN_LIST} 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 @@ -173,7 +174,8 @@ public BatchResponse sendAll( *

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 {@value #MAX_MESSAGES_IN_LIST} 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 @@ -188,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 {@value #MAX_MESSAGES_IN_LIST} 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. */ @@ -199,7 +202,8 @@ public ApiFuture sendAllAsync(@NonNull List messages) { /** * Similar to {@link #sendAll(List, boolean)} but performs the operation asynchronously. * - * @param messages A non-null, non-empty list containing up to {@value #MAX_MESSAGES_IN_LIST} 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. diff --git a/src/main/java/com/google/firebase/messaging/MulticastMessage.java b/src/main/java/com/google/firebase/messaging/MulticastMessage.java index d5165cf5f..aa519344a 100644 --- a/src/main/java/com/google/firebase/messaging/MulticastMessage.java +++ b/src/main/java/com/google/firebase/messaging/MulticastMessage.java @@ -32,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 {@value com.google.firebase.messaging.FirebaseMessaging#MAX_MESSAGES_IN_LIST} 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. * *

Instances of this class are thread-safe and immutable. Use {@link MulticastMessage.Builder} @@ -57,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() <= MAX_MESSAGES_IN_LIST, "no more than " + MAX_MESSAGES_IN_LIST + " 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"); } @@ -108,8 +110,9 @@ public static class Builder { private Builder() {} /** - * 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}. + * 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. @@ -120,8 +123,9 @@ public Builder addToken(@NonNull String token) { } /** - * 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}. + * 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.