diff --git a/Example/Messaging/Tests/FIRMessagingServiceTest.m b/Example/Messaging/Tests/FIRMessagingServiceTest.m index afbae465dbc..1924a7ffc1a 100644 --- a/Example/Messaging/Tests/FIRMessagingServiceTest.m +++ b/Example/Messaging/Tests/FIRMessagingServiceTest.m @@ -269,6 +269,34 @@ - (void)testUnsubscribeCompletionHandlerWithSuccess { }]; } +- (void)testSubscriptionCompletionHandlerWithInvalidTopicName { + XCTestExpectation *subscriptionCompletionExpectation = + [self expectationWithDescription:@"Subscription is complete"]; + [_messaging subscribeToTopic:@"!@#$%^&*()" + completion:^(NSError *_Nullable error) { + XCTAssertNotNil(error); + XCTAssertEqual(error.code, FIRMessagingErrorInvalidTopicName); + [subscriptionCompletionExpectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:0.2 + handler:^(NSError *_Nullable error){ + }]; +} + +- (void)testUnsubscribeCompletionHandlerWithInvalidTopicName { + XCTestExpectation *unsubscriptionCompletionExpectation = + [self expectationWithDescription:@"Unsubscription is complete"]; + [_messaging unsubscribeFromTopic:@"!@#$%^&*()" + completion:^(NSError *error) { + XCTAssertNotNil(error); + XCTAssertEqual(error.code, FIRMessagingErrorInvalidTopicName); + [unsubscriptionCompletionExpectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:0.2 + handler:^(NSError *_Nullable error){ + }]; +} + - (void)testFIRMessagingSDKVersionInFIRMessagingService { Class versionClass = NSClassFromString(kFIRMessagingSDKClassString); SEL versionSelector = NSSelectorFromString(kFIRMessagingSDKVersionSelectorString); diff --git a/Firebase/Messaging/FIRMessaging.m b/Firebase/Messaging/FIRMessaging.m index d158e51d3c5..186afdf3359 100644 --- a/Firebase/Messaging/FIRMessaging.m +++ b/Firebase/Messaging/FIRMessaging.m @@ -760,6 +760,9 @@ - (void)subscribeToTopic:(NSString *)topic } FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging009, @"Cannot parse topic name %@. Will not subscribe.", topic); + if (completion) { + completion([NSError fcm_errorWithCode:FIRMessagingErrorInvalidTopicName userInfo:nil]); + } } - (void)unsubscribeFromTopic:(NSString *)topic { @@ -786,6 +789,9 @@ - (void)unsubscribeFromTopic:(NSString *)topic } FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging011, @"Cannot parse topic name %@. Will not unsubscribe.", topic); + if (completion) { + completion([NSError fcm_errorWithCode:FIRMessagingErrorInvalidTopicName userInfo:nil]); + } } #pragma mark - Send diff --git a/Firebase/Messaging/FIRMessagingPubSub.h b/Firebase/Messaging/FIRMessagingPubSub.h index ebb4ca82e2b..2c2fbe62cbc 100644 --- a/Firebase/Messaging/FIRMessagingPubSub.h +++ b/Firebase/Messaging/FIRMessagingPubSub.h @@ -147,6 +147,7 @@ NS_ASSUME_NONNULL_BEGIN */ + (NSString *)removePrefixFromTopic:(NSString *)topic; + /** * Check if the topic name has "/topics/" prefix. * diff --git a/Firebase/Messaging/Public/FIRMessaging.h b/Firebase/Messaging/Public/FIRMessaging.h index acf79fa2712..c498d1f75ab 100644 --- a/Firebase/Messaging/Public/FIRMessaging.h +++ b/Firebase/Messaging/Public/FIRMessaging.h @@ -189,6 +189,10 @@ typedef NS_ENUM(NSUInteger, FIRMessagingError) { /// Some parameters of the request were invalid. FIRMessagingErrorInvalidRequest = 7, + + /// Topic name is invalid for subscription/unsubscription. + FIRMessagingErrorInvalidTopicName = 8, + } NS_SWIFT_NAME(MessagingError); /// Status for the downstream message received by the app.