Skip to content

Commit 845a25e

Browse files
Fix the issue that when topic is invalid, the callback was not triggered. (#1880)
1 parent fac0b7f commit 845a25e

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

Example/Messaging/Tests/FIRMessagingServiceTest.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,34 @@ - (void)testUnsubscribeCompletionHandlerWithSuccess {
269269
}];
270270
}
271271

272+
- (void)testSubscriptionCompletionHandlerWithInvalidTopicName {
273+
XCTestExpectation *subscriptionCompletionExpectation =
274+
[self expectationWithDescription:@"Subscription is complete"];
275+
[_messaging subscribeToTopic:@"!@#$%^&*()"
276+
completion:^(NSError *_Nullable error) {
277+
XCTAssertNotNil(error);
278+
XCTAssertEqual(error.code, FIRMessagingErrorInvalidTopicName);
279+
[subscriptionCompletionExpectation fulfill];
280+
}];
281+
[self waitForExpectationsWithTimeout:0.2
282+
handler:^(NSError *_Nullable error){
283+
}];
284+
}
285+
286+
- (void)testUnsubscribeCompletionHandlerWithInvalidTopicName {
287+
XCTestExpectation *unsubscriptionCompletionExpectation =
288+
[self expectationWithDescription:@"Unsubscription is complete"];
289+
[_messaging unsubscribeFromTopic:@"!@#$%^&*()"
290+
completion:^(NSError *error) {
291+
XCTAssertNotNil(error);
292+
XCTAssertEqual(error.code, FIRMessagingErrorInvalidTopicName);
293+
[unsubscriptionCompletionExpectation fulfill];
294+
}];
295+
[self waitForExpectationsWithTimeout:0.2
296+
handler:^(NSError *_Nullable error){
297+
}];
298+
}
299+
272300
- (void)testFIRMessagingSDKVersionInFIRMessagingService {
273301
Class versionClass = NSClassFromString(kFIRMessagingSDKClassString);
274302
SEL versionSelector = NSSelectorFromString(kFIRMessagingSDKVersionSelectorString);

Firebase/Messaging/FIRMessaging.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,9 @@ - (void)subscribeToTopic:(NSString *)topic
760760
}
761761
FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging009,
762762
@"Cannot parse topic name %@. Will not subscribe.", topic);
763+
if (completion) {
764+
completion([NSError fcm_errorWithCode:FIRMessagingErrorInvalidTopicName userInfo:nil]);
765+
}
763766
}
764767

765768
- (void)unsubscribeFromTopic:(NSString *)topic {
@@ -786,6 +789,9 @@ - (void)unsubscribeFromTopic:(NSString *)topic
786789
}
787790
FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging011,
788791
@"Cannot parse topic name %@. Will not unsubscribe.", topic);
792+
if (completion) {
793+
completion([NSError fcm_errorWithCode:FIRMessagingErrorInvalidTopicName userInfo:nil]);
794+
}
789795
}
790796

791797
#pragma mark - Send

Firebase/Messaging/FIRMessagingPubSub.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ NS_ASSUME_NONNULL_BEGIN
147147
*/
148148

149149
+ (NSString *)removePrefixFromTopic:(NSString *)topic;
150+
150151
/**
151152
* Check if the topic name has "/topics/" prefix.
152153
*

Firebase/Messaging/Public/FIRMessaging.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ typedef NS_ENUM(NSUInteger, FIRMessagingError) {
189189

190190
/// Some parameters of the request were invalid.
191191
FIRMessagingErrorInvalidRequest = 7,
192+
193+
/// Topic name is invalid for subscription/unsubscription.
194+
FIRMessagingErrorInvalidTopicName = 8,
195+
192196
} NS_SWIFT_NAME(MessagingError);
193197

194198
/// Status for the downstream message received by the app.

0 commit comments

Comments
 (0)