Skip to content

Commit e00eaa8

Browse files
authored
Encode the topic name when subscribing (#220)
This should address the issue filed at: firebase/quickstart-ios#242, where a topic name containing a `%` character was failing. It turns out that the topic name was never being url-encoded.
1 parent 5d984b0 commit e00eaa8

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Firebase/Messaging/FIRMMessageCode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ typedef NS_ENUM(NSInteger, FIRMessagingMessageCode) {
166166
kFIRMessagingMessageCodeTopicOption000 = 17000, // I-FCM017000
167167
kFIRMessagingMessageCodeTopicOption001 = 17001, // I-FCM017001
168168
kFIRMessagingMessageCodeTopicOption002 = 17002, // I-FCM017002
169+
kFIRMessagingMessageCodeTopicOptionTopicEncodingFailed = 17003, // I-FCM017003
169170
// FIRMessagingUtilities.m
170171
kFIRMessagingMessageCodeUtilities000 = 18000, // I-FCM018000
171172
kFIRMessagingMessageCodeUtilities001 = 18001, // I-FCM018001

Firebase/Messaging/FIRMessagingTopicOperation.m

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,28 @@ - (void)performSubscriptionChange {
165165
[request setValue:appIdentifier forHTTPHeaderField:@"app"];
166166
[request setValue:self.checkinService.versionInfo forHTTPHeaderField:@"info"];
167167

168+
// Topic can contain special characters (like `%`) so encode the value.
169+
NSCharacterSet *characterSet = [NSCharacterSet URLQueryAllowedCharacterSet];
170+
NSString *encodedTopic =
171+
[self.topic stringByAddingPercentEncodingWithAllowedCharacters:characterSet];
172+
if (encodedTopic == nil) {
173+
// The transformation was somehow not possible, so use the original topic.
174+
FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTopicOptionTopicEncodingFailed,
175+
@"Unable to encode the topic '%@' during topic subscription change. "
176+
@"Please ensure that the topic name contains only valid characters.",
177+
self.topic);
178+
encodedTopic = self.topic;
179+
}
180+
168181
NSMutableString *content = [NSMutableString stringWithFormat:
169182
@"sender=%@&app=%@&device=%@&"
170183
@"app_ver=%@&X-gcm.topic=%@&X-scope=%@",
171184
self.token,
172185
appIdentifier,
173186
deviceAuthID,
174187
FIRMessagingCurrentAppVersion(),
175-
self.topic,
176-
self.topic];
188+
encodedTopic,
189+
encodedTopic];
177190

178191
if (self.action == FIRMessagingTopicActionUnsubscribe) {
179192
[content appendString:@"&delete=true"];

0 commit comments

Comments
 (0)