Skip to content

Commit 693d064

Browse files
authored
Merge pull request #732 from OleksiyA/FCM-subscribe-completion-1
[FCM] Add completion handler to subscribe/unsubscribe topic actions
2 parents 3cbdbf2 + 4e4c82b commit 693d064

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

Example/Messaging/Tests/FIRMessagingServiceTest.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ - (void)testSubscribeWithNoTopicPrefix {
198198
NSString *topicNameWithPrefix = [FIRMessagingPubSub addPrefixToTopic:topicName];
199199
messaging.pubsub = mockPubSub;
200200
messaging.defaultFcmToken = @"fake-default-token";
201-
OCMExpect([messaging.pubsub subscribeToTopic:[OCMArg isEqual:topicNameWithPrefix]]);
201+
OCMExpect([messaging.pubsub subscribeToTopic:[OCMArg isEqual:topicNameWithPrefix]
202+
handler:[OCMArg any]]);
202203
[messaging subscribeToTopic:topicName];
203204
OCMVerifyAll(mockPubSub);
204205
// Need to swap back since it's a singleton and hence will live beyond the scope of this test.
@@ -213,7 +214,7 @@ - (void)testSubscribeWithTopicPrefix {
213214
NSString *topicName = @"/topics/topicWithoutPrefix";
214215
messaging.pubsub = mockPubSub;
215216
messaging.defaultFcmToken = @"fake-default-token";
216-
OCMExpect([messaging.pubsub subscribeToTopic:[OCMArg isEqual:topicName]]);
217+
OCMExpect([messaging.pubsub subscribeToTopic:[OCMArg isEqual:topicName] handler:[OCMArg any]]);
217218
[messaging subscribeToTopic:topicName];
218219
OCMVerifyAll(mockPubSub);
219220
// Need to swap back since it's a singleton and hence will live beyond the scope of this test.
@@ -229,7 +230,8 @@ - (void)testUnsubscribeWithNoTopicPrefix {
229230
NSString *topicNameWithPrefix = [FIRMessagingPubSub addPrefixToTopic:topicName];
230231
messaging.pubsub = mockPubSub;
231232
messaging.defaultFcmToken = @"fake-default-token";
232-
OCMExpect([messaging.pubsub unsubscribeFromTopic:[OCMArg isEqual:topicNameWithPrefix]]);
233+
OCMExpect([messaging.pubsub unsubscribeFromTopic:[OCMArg isEqual:topicNameWithPrefix]
234+
handler:[OCMArg any]]);
233235
[messaging unsubscribeFromTopic:topicName];
234236
OCMVerifyAll(mockPubSub);
235237
// Need to swap back since it's a singleton and hence will live beyond the scope of this test.
@@ -244,7 +246,8 @@ - (void)testUnsubscribeWithTopicPrefix {
244246
NSString *topicName = @"/topics/topicWithPrefix";
245247
messaging.pubsub = mockPubSub;
246248
messaging.defaultFcmToken = @"fake-default-token";
247-
OCMExpect([messaging.pubsub unsubscribeFromTopic:[OCMArg isEqual:topicName]]);
249+
OCMExpect([messaging.pubsub unsubscribeFromTopic:[OCMArg isEqual:topicName]
250+
handler:[OCMArg any]]);
248251
[messaging unsubscribeFromTopic:topicName];
249252
OCMVerifyAll(mockPubSub);
250253
// Need to swap back since it's a singleton and hence will live beyond the scope of this test.

Firebase/Messaging/FIRMessaging.m

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,10 +683,15 @@ + (NSString *)normalizeTopic:(NSString *)topic {
683683
}
684684

685685
- (void)subscribeToTopic:(NSString *)topic {
686+
[self subscribeToTopic:topic completion:nil];
687+
}
688+
689+
- (void)subscribeToTopic:(NSString *)topic
690+
completion:(nullable FIRMessagingTopicOperationCompletion)completion {
686691
if (self.defaultFcmToken.length && topic.length) {
687692
NSString *normalizeTopic = [[self class ] normalizeTopic:topic];
688693
if (normalizeTopic.length) {
689-
[self.pubsub subscribeToTopic:normalizeTopic];
694+
[self.pubsub subscribeToTopic:normalizeTopic handler:completion];
690695
} else {
691696
FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging009,
692697
@"Cannot parse topic name %@. Will not subscribe.", topic);
@@ -699,10 +704,15 @@ - (void)subscribeToTopic:(NSString *)topic {
699704
}
700705

701706
- (void)unsubscribeFromTopic:(NSString *)topic {
707+
[self unsubscribeFromTopic:topic completion:nil];
708+
}
709+
710+
- (void)unsubscribeFromTopic:(NSString *)topic
711+
completion:(nullable FIRMessagingTopicOperationCompletion)completion {
702712
if (self.defaultFcmToken.length && topic.length) {
703713
NSString *normalizeTopic = [[self class] normalizeTopic:topic];
704714
if (normalizeTopic.length) {
705-
[self.pubsub unsubscribeFromTopic:normalizeTopic];
715+
[self.pubsub unsubscribeFromTopic:normalizeTopic handler:completion];
706716
} else {
707717
FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging011,
708718
@"Cannot parse topic name %@. Will not unsubscribe.", topic);

Firebase/Messaging/FIRMessagingPubSub.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
* library for a given `authorizedEntity` and "gcm" scope.
6060
* @param topic The topic to subscribe to. Should be of the form
6161
* `"/topics/<topic-name>"`.
62+
* @param options Unused parameter, please pass nil or empty dictionary.
6263
* @param handler The callback handler invoked when the subscribe call
6364
* ends. In case of success, a nil error is returned. Otherwise,
6465
* an appropriate error object is returned.
@@ -70,7 +71,6 @@
7071
options:(NSDictionary *)options
7172
handler:(FIRMessagingTopicOperationCompletion)handler;
7273

73-
7474
/**
7575
* Unsubscribes an app instance from a topic, stopping it from receiving
7676
* any further messages sent to that topic.
@@ -81,6 +81,7 @@
8181
* @param token The token used to subscribe to this topic.
8282
* @param topic The topic to unsubscribe from. Should be of the form
8383
* `"/topics/<topic-name>"`.
84+
* @param options Unused parameter, please pass nil or empty dictionary.
8485
* @param handler The handler that is invoked once the unsubscribe call ends.
8586
* In case of success, nil error is returned. Otherwise, an
8687
* appropriate error object is returned.
@@ -98,17 +99,25 @@
9899
* as compared to the `subscribe` method above which tries once.
99100
*
100101
* @param topic The topic name to subscribe to. Should be of the form `"/topics/<topic-name>"`.
102+
* @param handler The handler that is invoked once the unsubscribe call ends.
103+
* In case of success, nil error is returned. Otherwise, an
104+
* appropriate error object is returned.
101105
*/
102-
- (void)subscribeToTopic:(NSString *)topic;
106+
- (void)subscribeToTopic:(NSString *)topic
107+
handler:(nullable FIRMessagingTopicOperationCompletion)handler;
103108

104109
/**
105110
* Asynchronously unsubscribe from the topic. Adds to the pending list of topic operations.
106111
* Retry in case of failures. This makes a repeated attempt to unsubscribe from the topic
107112
* as compared to the `unsubscribe` method above which tries once.
108113
*
109114
* @param topic The topic name to unsubscribe from. Should be of the form `"/topics/<topic-name>"`.
115+
* @param handler The handler that is invoked once the unsubscribe call ends.
116+
* In case of success, nil error is returned. Otherwise, an
117+
* appropriate error object is returned.
110118
*/
111-
- (void)unsubscribeFromTopic:(NSString *)topic;
119+
- (void)unsubscribeFromTopic:(NSString *)topic
120+
handler:(nullable FIRMessagingTopicOperationCompletion)handler;
112121

113122
/**
114123
* Schedule subscriptions sync.

Firebase/Messaging/FIRMessagingPubSub.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,18 @@ - (void)unsubscribeWithToken:(NSString *)token
146146
}];
147147
}
148148

149-
- (void)subscribeToTopic:(NSString *)topic {
149+
- (void)subscribeToTopic:(NSString *)topic
150+
handler:(nullable FIRMessagingTopicOperationCompletion)handler {
150151
[self.pendingTopicUpdates addOperationForTopic:topic
151152
withAction:FIRMessagingTopicActionSubscribe
152-
completion:nil];
153+
completion:handler];
153154
}
154155

155-
- (void)unsubscribeFromTopic:(NSString *)topic {
156+
- (void)unsubscribeFromTopic:(NSString *)topic
157+
handler:(nullable FIRMessagingTopicOperationCompletion)handler {
156158
[self.pendingTopicUpdates addOperationForTopic:topic
157159
withAction:FIRMessagingTopicActionUnsubscribe
158-
completion:nil];
160+
completion:handler];
159161
}
160162

161163
- (void)scheduleSync:(BOOL)immediately {

Firebase/Messaging/FIRMessagingTopicOperation.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ - (instancetype)initWithTopic:(NSString *)topic
8282
_topic = topic;
8383
_action = action;
8484
_token = token;
85+
_options = options;
8586
_checkinService = checkinService;
8687
_completion = completion;
8788

0 commit comments

Comments
 (0)