Skip to content
28 changes: 28 additions & 0 deletions Example/Messaging/Tests/FIRMessagingServiceTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions Firebase/Messaging/FIRMessaging.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions Firebase/Messaging/FIRMessagingPubSub.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ NS_ASSUME_NONNULL_BEGIN
*/

+ (NSString *)removePrefixFromTopic:(NSString *)topic;

/**
* Check if the topic name has "/topics/" prefix.
*
Expand Down
4 changes: 4 additions & 0 deletions Firebase/Messaging/Public/FIRMessaging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down