From 69ccd6d8b18b833acfd47ce251f05e94000e6ff5 Mon Sep 17 00:00:00 2001 From: Chen Liang Date: Tue, 19 Jun 2018 14:08:53 -0700 Subject: Corrected the deprecation warning when subscribing to or unsubscribing from a topic (#1425) --- Example/Messaging/Tests/FIRMessagingPubSubTest.m | 9 +++++++++ Firebase/Messaging/FIRMessaging.m | 6 ++++-- Firebase/Messaging/FIRMessagingPubSub.h | 9 +++++++++ Firebase/Messaging/FIRMessagingPubSub.m | 8 ++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Example/Messaging/Tests/FIRMessagingPubSubTest.m b/Example/Messaging/Tests/FIRMessagingPubSubTest.m index 3af1402..e1260f5 100644 --- a/Example/Messaging/Tests/FIRMessagingPubSubTest.m +++ b/Example/Messaging/Tests/FIRMessagingPubSubTest.m @@ -78,4 +78,13 @@ static NSString *const kTopicName = @"topic-Name"; XCTAssertTrue([FIRMessagingPubSub isValidTopicWithPrefix:topic]); } +- (void)testRemoveTopicPrefix { + NSString *topic = [NSString stringWithFormat:@"/topics/%@", kTopicName]; + topic = [FIRMessagingPubSub removePrefixFromTopic:topic]; + XCTAssertEqualObjects(topic, kTopicName); + // if the topic doesn't have the prefix, should return topic itself. + topic = [FIRMessagingPubSub removePrefixFromTopic:kTopicName]; + XCTAssertEqualObjects(topic, kTopicName); +} + @end diff --git a/Firebase/Messaging/FIRMessaging.m b/Firebase/Messaging/FIRMessaging.m index a97374b..efafcc2 100644 --- a/Firebase/Messaging/FIRMessaging.m +++ b/Firebase/Messaging/FIRMessaging.m @@ -700,7 +700,8 @@ NSString *const kFIRMessagingPlistAutoInitEnabled = if ([FIRMessagingPubSub hasTopicsPrefix:topic]) { FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTopicFormatIsDeprecated, @"Format '%@' is deprecated. Only '%@' should be used in " - @"subscribeToTopic.", topic, normalizeTopic); + @"subscribeToTopic.", topic, + [FIRMessagingPubSub removePrefixFromTopic:topic]); } if (normalizeTopic.length) { [self.pubsub subscribeToTopic:normalizeTopic handler:completion]; @@ -726,7 +727,8 @@ NSString *const kFIRMessagingPlistAutoInitEnabled = if ([FIRMessagingPubSub hasTopicsPrefix:topic]) { FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTopicFormatIsDeprecated, @"Format '%@' is deprecated. Only '%@' should be used in " - @"unsubscribeFromTopic.", topic, normalizeTopic); + @"unsubscribeFromTopic.", topic, + [FIRMessagingPubSub removePrefixFromTopic:topic]); } if (normalizeTopic.length) { [self.pubsub unsubscribeFromTopic:normalizeTopic handler:completion]; diff --git a/Firebase/Messaging/FIRMessagingPubSub.h b/Firebase/Messaging/FIRMessagingPubSub.h index 1c615d1..ebb4ca8 100644 --- a/Firebase/Messaging/FIRMessagingPubSub.h +++ b/Firebase/Messaging/FIRMessagingPubSub.h @@ -138,6 +138,15 @@ NS_ASSUME_NONNULL_BEGIN */ + (NSString *)addPrefixToTopic:(NSString *)topic; +/** + * Removes the "/topics/" prefix from the topic. + * + * @param topic The topic to remove the prefix from. + * + * @return The new topic name with the "/topics/" prefix removed. + */ + ++ (NSString *)removePrefixFromTopic:(NSString *)topic; /** * Check if the topic name has "/topics/" prefix. * diff --git a/Firebase/Messaging/FIRMessagingPubSub.m b/Firebase/Messaging/FIRMessagingPubSub.m index 09491b4..3f954e8 100644 --- a/Firebase/Messaging/FIRMessagingPubSub.m +++ b/Firebase/Messaging/FIRMessagingPubSub.m @@ -231,6 +231,14 @@ static NSString *const kTopicRegexPattern = @"/topics/([a-zA-Z0-9-_.~%]+)"; } } ++ (NSString *)removePrefixFromTopic:(NSString *)topic { + if ([self hasTopicsPrefix:topic]) { + return [topic substringFromIndex:kTopicsPrefix.length]; + } else { + return [topic copy]; + } +} + + (BOOL)hasTopicsPrefix:(NSString *)topic { return [topic hasPrefix:kTopicsPrefix]; } -- cgit v1.2.3