You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.4 KiB

6 years ago
6 years ago
6 years ago
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firebase/Messaging/FIRMessagingPubSubRegistrar.h"
  17. #import "Firebase/Messaging/FIRMessagingDefines.h"
  18. #import "Firebase/Messaging/FIRMessagingPubSubRegistrar.h"
  19. #import "Firebase/Messaging/FIRMessagingTopicsCommon.h"
  20. #import "Firebase/Messaging/NSError+FIRMessaging.h"
  21. @interface FIRMessagingPubSubRegistrar ()
  22. @property(nonatomic, readonly, strong) NSOperationQueue *topicOperations;
  23. // Common errors, instantiated, to avoid generating multiple copies
  24. @property(nonatomic, readwrite, strong) NSError *operationInProgressError;
  25. @end
  26. @implementation FIRMessagingPubSubRegistrar
  27. - (instancetype)init {
  28. self = [super init];
  29. if (self) {
  30. _topicOperations = [[NSOperationQueue alloc] init];
  31. // Do 10 topic operations at a time; it's enough to keep the TCP connection to the host alive,
  32. // saving hundreds of milliseconds on each request (compared to a serial queue).
  33. _topicOperations.maxConcurrentOperationCount = 10;
  34. }
  35. return self;
  36. }
  37. - (void)stopAllSubscriptionRequests {
  38. [self.topicOperations cancelAllOperations];
  39. }
  40. - (void)updateSubscriptionToTopic:(NSString *)topic
  41. withToken:(NSString *)token
  42. options:(NSDictionary *)options
  43. shouldDelete:(BOOL)shouldDelete
  44. handler:(FIRMessagingTopicOperationCompletion)handler {
  45. FIRMessagingTopicAction action = FIRMessagingTopicActionSubscribe;
  46. if (shouldDelete) {
  47. action = FIRMessagingTopicActionUnsubscribe;
  48. }
  49. FIRMessagingTopicOperation *operation =
  50. [[FIRMessagingTopicOperation alloc] initWithTopic:topic
  51. action:action
  52. token:token
  53. options:options
  54. completion:handler];
  55. [self.topicOperations addOperation:operation];
  56. }
  57. @end