FIRMessagingClient.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/FIRMessagingClient.h"
  17. #import <FirebaseInstanceID/FIRInstanceID_Private.h>
  18. #import <FirebaseMessaging/FIRMessaging.h>
  19. #import "Firebase/Messaging/FIRMessagingConstants.h"
  20. #import "Firebase/Messaging/FIRMessagingDefines.h"
  21. #import "Firebase/Messaging/FIRMessagingLogger.h"
  22. #import "Firebase/Messaging/FIRMessagingPubSubRegistrar.h"
  23. #import "Firebase/Messaging/FIRMessagingTopicsCommon.h"
  24. #import "Firebase/Messaging/FIRMessagingUtilities.h"
  25. #import "Firebase/Messaging/NSError+FIRMessaging.h"
  26. // register device with checkin
  27. typedef void (^FIRMessagingRegisterDeviceHandler)(NSError *error);
  28. @interface FIRMessagingClient ()
  29. @property(nonatomic, readwrite, weak) id<FIRMessagingClientDelegate> clientDelegate;
  30. @property(nonatomic, readonly, strong) FIRMessagingPubSubRegistrar *registrar;
  31. @property(nonatomic, readwrite, strong) NSString *senderId;
  32. @end
  33. @implementation FIRMessagingClient
  34. - (instancetype)init {
  35. FIRMessagingInvalidateInitializer();
  36. }
  37. - (instancetype)initWithDelegate:(id<FIRMessagingClientDelegate>)delegate {
  38. self = [super init];
  39. if (self) {
  40. _clientDelegate = delegate;
  41. _registrar = [[FIRMessagingPubSubRegistrar alloc] init];
  42. }
  43. return self;
  44. }
  45. - (void)teardown {
  46. if (![NSThread isMainThread]) {
  47. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeClient000,
  48. @"FIRMessagingClient should be called from main thread only.");
  49. }
  50. // Stop all subscription requests
  51. [self.registrar stopAllSubscriptionRequests];
  52. [NSObject cancelPreviousPerformRequestsWithTarget:self];
  53. [[NSNotificationCenter defaultCenter] removeObserver:self];
  54. }
  55. - (void)cancelAllRequests {
  56. // Stop any checkin requests or any subscription requests
  57. [self.registrar stopAllSubscriptionRequests];
  58. }
  59. #pragma mark - FIRMessaging subscribe
  60. - (void)updateSubscriptionWithToken:(NSString *)token
  61. topic:(NSString *)topic
  62. options:(NSDictionary *)options
  63. shouldDelete:(BOOL)shouldDelete
  64. handler:(FIRMessagingTopicOperationCompletion)handler {
  65. FIRMessagingTopicOperationCompletion completion = ^void(NSError *error) {
  66. if (error) {
  67. FIRMessagingLoggerError(kFIRMessagingMessageCodeClient001, @"Failed to subscribe to topic %@",
  68. error);
  69. } else {
  70. if (shouldDelete) {
  71. FIRMessagingLoggerInfo(kFIRMessagingMessageCodeClient002,
  72. @"Successfully unsubscribed from topic %@", topic);
  73. } else {
  74. FIRMessagingLoggerInfo(kFIRMessagingMessageCodeClient003,
  75. @"Successfully subscribed to topic %@", topic);
  76. }
  77. }
  78. if (handler) {
  79. handler(error);
  80. }
  81. };
  82. if ([[FIRInstanceID instanceID] tryToLoadValidCheckinInfo]) {
  83. [self.registrar updateSubscriptionToTopic:topic
  84. withToken:token
  85. options:options
  86. shouldDelete:shouldDelete
  87. handler:completion];
  88. } else {
  89. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeRegistrar000,
  90. @"Device check in error, no auth credentials found");
  91. NSError *error = [NSError errorWithFCMErrorCode:kFIRMessagingErrorCodeMissingDeviceID];
  92. handler(error);
  93. }
  94. }
  95. @end