FIRMessaging.m 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. #if !__has_feature(objc_arc)
  17. #error FIRMessagingLib should be compiled with ARC.
  18. #endif
  19. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
  20. #import <GoogleUtilities/GULAppDelegateSwizzler.h>
  21. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  22. #import <GoogleUtilities/GULReachabilityChecker.h>
  23. #import <GoogleUtilities/GULUserDefaults.h>
  24. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  25. #import "FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.h"
  26. #import "FirebaseMessaging/Interop/FIRMessagingInterop.h"
  27. #import "FirebaseMessaging/Sources/FIRMessagingAnalytics.h"
  28. #import "FirebaseMessaging/Sources/FIRMessagingCode.h"
  29. #import "FirebaseMessaging/Sources/FIRMessagingConstants.h"
  30. #import "FirebaseMessaging/Sources/FIRMessagingContextManagerService.h"
  31. #import "FirebaseMessaging/Sources/FIRMessagingDefines.h"
  32. #import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
  33. #import "FirebaseMessaging/Sources/FIRMessagingPubSub.h"
  34. #import "FirebaseMessaging/Sources/FIRMessagingRemoteNotificationsProxy.h"
  35. #import "FirebaseMessaging/Sources/FIRMessagingRmqManager.h"
  36. #import "FirebaseMessaging/Sources/FIRMessagingSyncMessageManager.h"
  37. #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  38. #import "FirebaseMessaging/Sources/FIRMessaging_Private.h"
  39. #import "FirebaseMessaging/Sources/NSError+FIRMessaging.h"
  40. #import "FirebaseMessaging/Sources/Token/FIRMessagingAuthService.h"
  41. #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenInfo.h"
  42. #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenManager.h"
  43. #import "Interop/Analytics/Public/FIRAnalyticsInterop.h"
  44. static NSString *const kFIRMessagingMessageViaAPNSRootKey = @"aps";
  45. static NSString *const kFIRMessagingReachabilityHostname = @"www.google.com";
  46. const NSNotificationName FIRMessagingRegistrationTokenRefreshedNotification =
  47. @"com.firebase.messaging.notif.fcm-token-refreshed";
  48. NSString *const kFIRMessagingUserDefaultsKeyAutoInitEnabled =
  49. @"com.firebase.messaging.auto-init.enabled"; // Auto Init Enabled key stored in NSUserDefaults
  50. NSString *const kFIRMessagingPlistAutoInitEnabled =
  51. @"FirebaseMessagingAutoInitEnabled"; // Auto Init Enabled key stored in Info.plist
  52. NSString *const FIRMessagingErrorDomain = @"com.google.fcm";
  53. BOOL FIRMessagingIsAPNSSyncMessage(NSDictionary *message) {
  54. if ([message[kFIRMessagingMessageViaAPNSRootKey] isKindOfClass:[NSDictionary class]]) {
  55. NSDictionary *aps = message[kFIRMessagingMessageViaAPNSRootKey];
  56. if (aps && [aps isKindOfClass:[NSDictionary class]]) {
  57. return [aps[kFIRMessagingMessageAPNSContentAvailableKey] boolValue];
  58. }
  59. }
  60. return NO;
  61. }
  62. BOOL FIRMessagingIsContextManagerMessage(NSDictionary *message) {
  63. return [FIRMessagingContextManagerService isContextManagerMessage:message];
  64. }
  65. @interface FIRMessagingMessageInfo ()
  66. @property(nonatomic, readwrite, assign) FIRMessagingMessageStatus status;
  67. @end
  68. @implementation FIRMessagingMessageInfo
  69. - (instancetype)init {
  70. FIRMessagingInvalidateInitializer();
  71. }
  72. - (instancetype)initWithStatus:(FIRMessagingMessageStatus)status {
  73. self = [super init];
  74. if (self) {
  75. _status = status;
  76. }
  77. return self;
  78. }
  79. @end
  80. @interface FIRMessaging () <GULReachabilityDelegate>
  81. // FIRApp properties
  82. @property(nonatomic, readwrite, strong) NSData *apnsTokenData;
  83. @property(nonatomic, readwrite, strong) FIRMessagingClient *client;
  84. @property(nonatomic, readwrite, strong) GULReachabilityChecker *reachability;
  85. @property(nonatomic, readwrite, strong) FIRMessagingPubSub *pubsub;
  86. @property(nonatomic, readwrite, strong) FIRMessagingRmqManager *rmq2Manager;
  87. @property(nonatomic, readwrite, strong) FIRMessagingSyncMessageManager *syncMessageManager;
  88. @property(nonatomic, readwrite, strong) GULUserDefaults *messagingUserDefaults;
  89. @property(nonatomic, readwrite, strong) FIRInstallations *installations;
  90. @property(nonatomic, readwrite, strong) FIRMessagingTokenManager *tokenManager;
  91. @property(nonatomic, readwrite, strong) FIRHeartbeatLogger *heartbeatLogger;
  92. /// Message ID's logged for analytics. This prevents us from logging the same message twice
  93. /// which can happen if the user inadvertently calls `appDidReceiveMessage` along with us
  94. /// calling it implicitly during swizzling.
  95. @property(nonatomic, readwrite, strong) NSMutableSet *loggedMessageIDs;
  96. @property(nonatomic, readwrite, strong) id<FIRAnalyticsInterop> _Nullable analytics;
  97. @end
  98. @interface FIRMessaging () <FIRMessagingInterop, FIRLibrary>
  99. @end
  100. @implementation FIRMessaging
  101. + (FIRMessaging *)messaging {
  102. FIRApp *defaultApp = [FIRApp defaultApp]; // Missing configure will be logged here.
  103. id<FIRMessagingInterop> instance = FIR_COMPONENT(FIRMessagingInterop, defaultApp.container);
  104. // We know the instance coming from the container is a FIRMessaging instance, cast it and move on.
  105. return (FIRMessaging *)instance;
  106. }
  107. - (instancetype)initWithAnalytics:(nullable id<FIRAnalyticsInterop>)analytics
  108. userDefaults:(GULUserDefaults *)defaults
  109. heartbeatLogger:(FIRHeartbeatLogger *)heartbeatLogger {
  110. self = [super init];
  111. if (self != nil) {
  112. _loggedMessageIDs = [NSMutableSet set];
  113. _messagingUserDefaults = defaults;
  114. _analytics = analytics;
  115. _heartbeatLogger = heartbeatLogger;
  116. }
  117. return self;
  118. }
  119. - (void)dealloc {
  120. [self.reachability stop];
  121. [[NSNotificationCenter defaultCenter] removeObserver:self];
  122. [self teardown];
  123. }
  124. #pragma mark - Config
  125. + (void)load {
  126. [FIRApp registerInternalLibrary:(Class<FIRLibrary>)self withName:@"fire-fcm"];
  127. }
  128. + (nonnull NSArray<FIRComponent *> *)componentsToRegister {
  129. FIRComponentCreationBlock creationBlock =
  130. ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  131. if (!container.app.isDefaultApp) {
  132. // Only start for the default FIRApp.
  133. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeFIRApp001,
  134. @"Firebase Messaging only works with the default app.");
  135. return nil;
  136. }
  137. // Ensure it's cached so it returns the same instance every time messaging is called.
  138. *isCacheable = YES;
  139. id<FIRAnalyticsInterop> analytics = FIR_COMPONENT(FIRAnalyticsInterop, container);
  140. FIRMessaging *messaging =
  141. [[FIRMessaging alloc] initWithAnalytics:analytics
  142. userDefaults:[GULUserDefaults standardUserDefaults]
  143. heartbeatLogger:container.app.heartbeatLogger];
  144. [messaging start];
  145. [messaging configureMessagingWithOptions:container.app.options];
  146. [messaging configureNotificationSwizzlingIfEnabled];
  147. return messaging;
  148. };
  149. FIRComponent *messagingProvider =
  150. [FIRComponent componentWithProtocol:@protocol(FIRMessagingInterop)
  151. instantiationTiming:FIRInstantiationTimingEagerInDefaultApp
  152. creationBlock:creationBlock];
  153. return @[ messagingProvider ];
  154. }
  155. - (void)configureMessagingWithOptions:(FIROptions *)options {
  156. NSString *GCMSenderID = options.GCMSenderID;
  157. if (!GCMSenderID.length) {
  158. FIRMessagingLoggerError(kFIRMessagingMessageCodeFIRApp000,
  159. @"Firebase not set up correctly, nil or empty senderID.");
  160. [NSException raise:FIRMessagingErrorDomain
  161. format:@"Could not configure Firebase Messaging. GCMSenderID must not be nil or "
  162. @"empty."];
  163. }
  164. self.tokenManager.fcmSenderID = GCMSenderID;
  165. self.tokenManager.firebaseAppID = options.googleAppID;
  166. // FCM generates a FCM token during app start for sending push notification to device.
  167. // This is not needed for app extension except for watch.
  168. #if TARGET_OS_WATCH
  169. [self didCompleteConfigure];
  170. #else // TARGET_OS_WATCH
  171. if (![GULAppEnvironmentUtil isAppExtension]) {
  172. [self didCompleteConfigure];
  173. }
  174. #endif // TARGET_OS_WATCH
  175. }
  176. - (void)didCompleteConfigure {
  177. NSString *cachedToken =
  178. [self.tokenManager cachedTokenInfoWithAuthorizedEntity:self.tokenManager.fcmSenderID
  179. scope:kFIRMessagingDefaultTokenScope]
  180. .token;
  181. // When there is a cached token, do the token refresh.
  182. // Before fetching FCM token, confirm there is an APNS token to avoid validation error
  183. // This error is innocuous since we refetch after APNS token is set but it can seem alarming
  184. if (cachedToken) {
  185. // Clean up expired tokens by checking the token refresh policy.
  186. [self.installations installationIDWithCompletion:^(NSString *_Nullable identifier,
  187. NSError *_Nullable error) {
  188. if ([self.tokenManager checkTokenRefreshPolicyWithIID:identifier] && self.APNSToken) {
  189. // Default token is expired, fetch default token from server.
  190. [self retrieveFCMTokenForSenderID:self.tokenManager.fcmSenderID
  191. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error){
  192. }];
  193. }
  194. // Set the default FCM token, there's an issue that FIRApp configure
  195. // happens before developers able to set the delegate
  196. // Hence first token set must be happen here after listener is set
  197. // TODO(chliangGoogle) Need to investigate better solution.
  198. [self updateDefaultFCMToken:self.FCMToken];
  199. }];
  200. } else if (self.isAutoInitEnabled && self.APNSToken) {
  201. // When there is no cached token, must check auto init is enabled.
  202. // If it's disabled, don't initiate token generation/refresh.
  203. // If no cache token and auto init is enabled, fetch a token from server.
  204. [self retrieveFCMTokenForSenderID:self.tokenManager.fcmSenderID
  205. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error){
  206. }];
  207. }
  208. }
  209. - (void)configureNotificationSwizzlingIfEnabled {
  210. // Swizzle remote-notification-related methods (app delegate and UNUserNotificationCenter)
  211. if ([FIRMessagingRemoteNotificationsProxy canSwizzleMethods]) {
  212. NSString *docsURLString = @"https://firebase.google.com/docs/cloud-messaging/ios/client"
  213. @"#method_swizzling_in_firebase_messaging";
  214. FIRMessagingLoggerNotice(kFIRMessagingMessageCodeFIRApp000,
  215. @"FIRMessaging Remote Notifications proxy enabled, will swizzle "
  216. @"remote notification receiver handlers. If you'd prefer to manually "
  217. @"integrate Firebase Messaging, add \"%@\" to your Info.plist, "
  218. @"and set it to NO. Follow the instructions at:\n%@\nto ensure "
  219. @"proper integration.",
  220. kFIRMessagingRemoteNotificationsProxyEnabledInfoPlistKey,
  221. docsURLString);
  222. [[FIRMessagingRemoteNotificationsProxy sharedProxy] swizzleMethodsIfPossible];
  223. }
  224. }
  225. - (void)start {
  226. [self setupFileManagerSubDirectory];
  227. [self setupNotificationListeners];
  228. self.tokenManager =
  229. [[FIRMessagingTokenManager alloc] initWithHeartbeatLogger:self.heartbeatLogger];
  230. self.installations = [FIRInstallations installations];
  231. [self setupTopics];
  232. // Print the library version for logging.
  233. NSString *currentLibraryVersion = FIRFirebaseVersion();
  234. FIRMessagingLoggerInfo(kFIRMessagingMessageCodeMessagingPrintLibraryVersion,
  235. @"FIRMessaging library version %@", currentLibraryVersion);
  236. NSString *hostname = kFIRMessagingReachabilityHostname;
  237. self.reachability = [[GULReachabilityChecker alloc] initWithReachabilityDelegate:self
  238. withHost:hostname];
  239. [self.reachability start];
  240. // setup FIRMessaging objects
  241. [self setupRmqManager];
  242. [self setupSyncMessageManager];
  243. }
  244. - (void)setupFileManagerSubDirectory {
  245. if (![[self class] hasSubDirectory:kFIRMessagingSubDirectoryName]) {
  246. [[self class] createSubDirectory:kFIRMessagingSubDirectoryName];
  247. }
  248. if (![[self class] hasSubDirectory:kFIRMessagingInstanceIDSubDirectoryName]) {
  249. [[self class] createSubDirectory:kFIRMessagingInstanceIDSubDirectoryName];
  250. }
  251. }
  252. - (void)setupNotificationListeners {
  253. // To prevent multiple notifications remove self as observer for all events.
  254. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  255. [center removeObserver:self];
  256. [center addObserver:self
  257. selector:@selector(defaultFCMTokenWasRefreshed:)
  258. name:kFIRMessagingRegistrationTokenRefreshNotification
  259. object:nil];
  260. }
  261. - (void)setupRmqManager {
  262. self.rmq2Manager = [[FIRMessagingRmqManager alloc] initWithDatabaseName:@"rmq2"];
  263. [self.rmq2Manager loadRmqId];
  264. }
  265. - (void)setupTopics {
  266. self.pubsub = [[FIRMessagingPubSub alloc] initWithTokenManager:self.tokenManager];
  267. }
  268. - (void)setupSyncMessageManager {
  269. self.syncMessageManager =
  270. [[FIRMessagingSyncMessageManager alloc] initWithRmqManager:self.rmq2Manager];
  271. [self.syncMessageManager removeExpiredSyncMessages];
  272. }
  273. - (void)teardown {
  274. self.pubsub = nil;
  275. self.syncMessageManager = nil;
  276. self.rmq2Manager = nil;
  277. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging001, @"Did successfully teardown");
  278. }
  279. #pragma mark - Messages
  280. - (FIRMessagingMessageInfo *)appDidReceiveMessage:(NSDictionary *)message {
  281. if (!message.count) {
  282. return [[FIRMessagingMessageInfo alloc] initWithStatus:FIRMessagingMessageStatusUnknown];
  283. }
  284. // For downstream messages that go via MCS we should strip out this key before sending
  285. // the message to the device.
  286. BOOL isOldMessage = NO;
  287. NSString *messageID = message[kFIRMessagingMessageIDKey];
  288. if (messageID.length) {
  289. [self.rmq2Manager saveS2dMessageWithRmqId:messageID];
  290. BOOL isSyncMessage = FIRMessagingIsAPNSSyncMessage(message);
  291. if (isSyncMessage) {
  292. isOldMessage = [self.syncMessageManager didReceiveAPNSSyncMessage:message];
  293. }
  294. // Prevent duplicates by keeping a cache of all the logged messages during each session.
  295. // The duplicates only happen when the 3P app calls `appDidReceiveMessage:` along with
  296. // us swizzling their implementation to call the same method implicitly.
  297. // We need to rule out the contextual message because it shares the same message ID
  298. // as the local notification it will schedule. And because it is also a APNSSync message
  299. // its duplication is already checked previously.
  300. if (!isOldMessage && !FIRMessagingIsContextManagerMessage(message)) {
  301. isOldMessage = [self.loggedMessageIDs containsObject:messageID];
  302. if (!isOldMessage) {
  303. [self.loggedMessageIDs addObject:messageID];
  304. }
  305. }
  306. }
  307. if (!isOldMessage) {
  308. [FIRMessagingAnalytics logMessage:message toAnalytics:_analytics];
  309. [self handleContextManagerMessage:message];
  310. [self handleIncomingLinkIfNeededFromMessage:message];
  311. }
  312. return [[FIRMessagingMessageInfo alloc] initWithStatus:FIRMessagingMessageStatusNew];
  313. }
  314. - (BOOL)handleContextManagerMessage:(NSDictionary *)message {
  315. if (FIRMessagingIsContextManagerMessage(message)) {
  316. return [FIRMessagingContextManagerService handleContextManagerMessage:message];
  317. }
  318. return NO;
  319. }
  320. - (void)handleIncomingLinkIfNeededFromMessage:(NSDictionary *)message {
  321. #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION
  322. NSURL *url = [self linkURLFromMessage:message];
  323. if (url == nil) {
  324. return;
  325. }
  326. if (![NSThread isMainThread]) {
  327. dispatch_async(dispatch_get_main_queue(), ^{
  328. [self handleIncomingLinkIfNeededFromMessage:message];
  329. });
  330. return;
  331. }
  332. UIApplication *application = [GULAppDelegateSwizzler sharedApplication];
  333. if (!application) {
  334. return;
  335. }
  336. id<UIApplicationDelegate> appDelegate = application.delegate;
  337. SEL continueUserActivitySelector = @selector(application:
  338. continueUserActivity:restorationHandler:);
  339. // Due to FIRAAppDelegateProxy swizzling, this selector will most likely get chosen, whether or
  340. // not the actual application has implemented
  341. // |application:continueUserActivity:restorationHandler:|. A warning will be displayed to the user
  342. // if they haven't implemented it.
  343. if ([NSUserActivity class] != nil &&
  344. [appDelegate respondsToSelector:continueUserActivitySelector]) {
  345. NSUserActivity *userActivity =
  346. [[NSUserActivity alloc] initWithActivityType:NSUserActivityTypeBrowsingWeb];
  347. userActivity.webpageURL = url;
  348. [appDelegate application:application
  349. continueUserActivity:userActivity
  350. restorationHandler:^(NSArray *_Nullable restorableObjects){
  351. // Do nothing, as we don't support the app calling this block
  352. }];
  353. }
  354. #endif // TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION
  355. }
  356. - (NSURL *)linkURLFromMessage:(NSDictionary *)message {
  357. NSString *urlString = message[kFIRMessagingMessageLinkKey];
  358. if (urlString == nil || ![urlString isKindOfClass:[NSString class]] || urlString.length == 0) {
  359. return nil;
  360. }
  361. NSURL *url = [NSURL URLWithString:urlString];
  362. return url;
  363. }
  364. #pragma mark - APNS
  365. - (NSData *)APNSToken {
  366. return self.apnsTokenData;
  367. }
  368. - (void)setAPNSToken:(NSData *)APNSToken {
  369. [self setAPNSToken:APNSToken type:FIRMessagingAPNSTokenTypeUnknown];
  370. }
  371. - (void)setAPNSToken:(NSData *)apnsToken type:(FIRMessagingAPNSTokenType)type {
  372. if ([apnsToken isEqual:self.apnsTokenData]) {
  373. return;
  374. }
  375. self.apnsTokenData = apnsToken;
  376. // Notify InstanceID that APNS Token has been set.
  377. NSDictionary *userInfo = @{kFIRMessagingAPNSTokenType : @(type)};
  378. // TODO(chliang) This is sent to InstanceID in case users are still using the deprecated SDK.
  379. // Should be safe to remove once InstanceID is removed.
  380. NSNotification *notification =
  381. [NSNotification notificationWithName:kFIRMessagingAPNSTokenNotification
  382. object:[apnsToken copy]
  383. userInfo:userInfo];
  384. [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
  385. [self.tokenManager setAPNSToken:[apnsToken copy] withUserInfo:userInfo];
  386. }
  387. #pragma mark - FCM Token
  388. - (BOOL)isAutoInitEnabled {
  389. // Defer to the class method since we're just reading from regular userDefaults and we need to
  390. // read this from IID without instantiating the Messaging singleton.
  391. return [[self class] isAutoInitEnabledWithUserDefaults:_messagingUserDefaults];
  392. }
  393. /// Checks if Messaging auto-init is enabled in the user defaults instance passed in. This is
  394. /// exposed as a class property for IID to fetch the property without instantiating an instance of
  395. /// Messaging. Since Messaging can only be used with the default FIRApp, we can have one point of
  396. /// entry without context of which FIRApp instance is being used.
  397. /// ** THIS METHOD IS DEPENDED ON INTERNALLY BY IID USING REFLECTION. PLEASE DO NOT CHANGE THE
  398. /// SIGNATURE, AS IT WOULD BREAK AUTOINIT FUNCTIONALITY WITHIN IID. **
  399. + (BOOL)isAutoInitEnabledWithUserDefaults:(GULUserDefaults *)userDefaults {
  400. // Check storage
  401. id isAutoInitEnabledObject =
  402. [userDefaults objectForKey:kFIRMessagingUserDefaultsKeyAutoInitEnabled];
  403. if (isAutoInitEnabledObject) {
  404. return [isAutoInitEnabledObject boolValue];
  405. }
  406. // Check Info.plist
  407. isAutoInitEnabledObject =
  408. [[NSBundle mainBundle] objectForInfoDictionaryKey:kFIRMessagingPlistAutoInitEnabled];
  409. if (isAutoInitEnabledObject) {
  410. return [isAutoInitEnabledObject boolValue];
  411. }
  412. // If none of above exists, we default to the global switch that comes from FIRApp.
  413. return [[FIRApp defaultApp] isDataCollectionDefaultEnabled];
  414. }
  415. - (void)setAutoInitEnabled:(BOOL)autoInitEnabled {
  416. BOOL isFCMAutoInitEnabled = [self isAutoInitEnabled];
  417. [_messagingUserDefaults setBool:autoInitEnabled
  418. forKey:kFIRMessagingUserDefaultsKeyAutoInitEnabled];
  419. if (!isFCMAutoInitEnabled && autoInitEnabled) {
  420. [self.tokenManager tokenAndRequestIfNotExist];
  421. }
  422. }
  423. - (NSString *)FCMToken {
  424. // Gets the current default token, and requests a new one if it doesn't exist.
  425. NSString *token = [self.tokenManager tokenAndRequestIfNotExist];
  426. return token;
  427. }
  428. - (void)tokenWithCompletion:(FIRMessagingFCMTokenFetchCompletion)completion {
  429. FIROptions *options = FIRApp.defaultApp.options;
  430. [self retrieveFCMTokenForSenderID:options.GCMSenderID completion:completion];
  431. }
  432. - (void)deleteTokenWithCompletion:(FIRMessagingDeleteFCMTokenCompletion)completion {
  433. FIROptions *options = FIRApp.defaultApp.options;
  434. [self deleteFCMTokenForSenderID:options.GCMSenderID completion:completion];
  435. }
  436. - (void)retrieveFCMTokenForSenderID:(nonnull NSString *)senderID
  437. completion:(nonnull FIRMessagingFCMTokenFetchCompletion)completion {
  438. if (!senderID.length) {
  439. NSString *description = @"Couldn't fetch token because a Sender ID was not supplied. A valid "
  440. @"Sender ID is required to fetch an FCM token";
  441. FIRMessagingLoggerError(kFIRMessagingMessageCodeSenderIDNotSuppliedForTokenFetch, @"%@",
  442. description);
  443. if (completion) {
  444. NSError *error = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeMissingAuthorizedEntity
  445. failureReason:description];
  446. completion(nil, error);
  447. }
  448. return;
  449. }
  450. NSDictionary *options = nil;
  451. if (self.APNSToken) {
  452. options = @{kFIRMessagingTokenOptionsAPNSKey : self.APNSToken};
  453. } else {
  454. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeAPNSTokenNotAvailableDuringTokenFetch,
  455. @"APNS device token not set before retrieving FCM Token for Sender ID "
  456. @"'%@'."
  457. @"Be sure to re-retrieve the FCM token once the APNS device token is "
  458. @"set.",
  459. senderID);
  460. }
  461. [self.tokenManager
  462. tokenWithAuthorizedEntity:senderID
  463. scope:kFIRMessagingDefaultTokenScope
  464. options:options
  465. handler:^(NSString *_Nullable FCMToken, NSError *_Nullable error) {
  466. if (completion) {
  467. completion(FCMToken, error);
  468. }
  469. }];
  470. }
  471. - (void)deleteFCMTokenForSenderID:(nonnull NSString *)senderID
  472. completion:(nonnull FIRMessagingDeleteFCMTokenCompletion)completion {
  473. if (!senderID.length) {
  474. NSString *description = @"Couldn't delete token because a Sender ID was not supplied. A "
  475. @"valid Sender ID is required to delete an FCM token";
  476. FIRMessagingLoggerError(kFIRMessagingMessageCodeSenderIDNotSuppliedForTokenDelete, @"%@",
  477. description);
  478. if (completion) {
  479. NSError *error = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeInvalidRequest
  480. failureReason:description];
  481. completion(error);
  482. }
  483. return;
  484. }
  485. FIRMessaging_WEAKIFY(self);
  486. [self.installations
  487. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  488. FIRMessaging_STRONGIFY(self);
  489. if (error) {
  490. NSError *newError = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeInvalidIdentity
  491. failureReason:@"Failed to get installation ID."];
  492. completion(newError);
  493. } else {
  494. [self.tokenManager deleteTokenWithAuthorizedEntity:senderID
  495. scope:kFIRMessagingDefaultTokenScope
  496. instanceID:identifier
  497. handler:^(NSError *_Nullable error) {
  498. if (completion) {
  499. completion(error);
  500. }
  501. }];
  502. }
  503. }];
  504. }
  505. - (void)deleteDataWithCompletion:(void (^)(NSError *_Nullable))completion {
  506. FIRMessaging_WEAKIFY(self);
  507. [self.tokenManager deleteWithHandler:^(NSError *error) {
  508. FIRMessaging_STRONGIFY(self);
  509. if (error) {
  510. if (completion) {
  511. completion(error);
  512. }
  513. return;
  514. }
  515. // Only request new token if FCM auto initialization is
  516. // enabled.
  517. if ([self isAutoInitEnabled]) {
  518. // Deletion succeeds! Requesting new checkin, IID and token.
  519. [self tokenWithCompletion:^(NSString *_Nullable token, NSError *_Nullable error) {
  520. if (completion) {
  521. completion(error);
  522. }
  523. }];
  524. return;
  525. }
  526. if (completion) {
  527. completion(nil);
  528. }
  529. }];
  530. }
  531. #pragma mark - FIRMessagingDelegate helper methods
  532. - (void)setDelegate:(id<FIRMessagingDelegate>)delegate {
  533. _delegate = delegate;
  534. [self validateDelegateConformsToTokenAvailabilityMethods];
  535. }
  536. // Check if the delegate conforms to |didReceiveRegistrationToken:|
  537. // and display a warning to the developer if not.
  538. // NOTE: Once |didReceiveRegistrationToken:| can be made a required method, this
  539. // check can be removed.
  540. - (void)validateDelegateConformsToTokenAvailabilityMethods {
  541. if (self.delegate && ![self.delegate respondsToSelector:@selector(messaging:
  542. didReceiveRegistrationToken:)]) {
  543. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTokenDelegateMethodsNotImplemented,
  544. @"The object %@ does not respond to "
  545. @"-messaging:didReceiveRegistrationToken:. Please implement "
  546. @"-messaging:didReceiveRegistrationToken: to be provided with an FCM "
  547. @"token.",
  548. self.delegate.description);
  549. }
  550. }
  551. - (void)notifyRefreshedFCMToken {
  552. __weak FIRMessaging *weakSelf = self;
  553. if (![NSThread isMainThread]) {
  554. dispatch_async(dispatch_get_main_queue(), ^{
  555. [weakSelf notifyRefreshedFCMToken];
  556. });
  557. return;
  558. }
  559. if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) {
  560. [self.delegate messaging:self didReceiveRegistrationToken:self.tokenManager.defaultFCMToken];
  561. }
  562. // Should always trigger the token refresh notification when the delegate method is called
  563. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  564. [center postNotificationName:FIRMessagingRegistrationTokenRefreshedNotification
  565. object:self.tokenManager.defaultFCMToken];
  566. }
  567. #pragma mark - Topics
  568. + (NSString *)normalizeTopic:(NSString *)topic {
  569. if (!topic.length) {
  570. return nil;
  571. }
  572. if (![FIRMessagingPubSub hasTopicsPrefix:topic]) {
  573. topic = [FIRMessagingPubSub addPrefixToTopic:topic];
  574. }
  575. if ([FIRMessagingPubSub isValidTopicWithPrefix:topic]) {
  576. return [topic copy];
  577. }
  578. return nil;
  579. }
  580. - (void)subscribeToTopic:(NSString *)topic {
  581. [self subscribeToTopic:topic completion:nil];
  582. }
  583. - (void)subscribeToTopic:(NSString *)topic
  584. completion:(nullable FIRMessagingTopicOperationCompletion)completion {
  585. if ([FIRMessagingPubSub hasTopicsPrefix:topic]) {
  586. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTopicFormatIsDeprecated,
  587. @"Format '%@' is deprecated. Only '%@' should be used in "
  588. @"subscribeToTopic.",
  589. topic, [FIRMessagingPubSub removePrefixFromTopic:topic]);
  590. }
  591. __weak FIRMessaging *weakSelf = self;
  592. [self
  593. retrieveFCMTokenForSenderID:self.tokenManager.fcmSenderID
  594. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error) {
  595. if (error) {
  596. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging010,
  597. @"The subscription operation failed due to an "
  598. @"error getting the FCM token: %@.",
  599. error);
  600. if (completion) {
  601. completion(error);
  602. }
  603. return;
  604. }
  605. FIRMessaging *strongSelf = weakSelf;
  606. NSString *normalizeTopic = [[strongSelf class] normalizeTopic:topic];
  607. if (normalizeTopic.length) {
  608. [strongSelf.pubsub subscribeToTopic:normalizeTopic handler:completion];
  609. return;
  610. }
  611. NSString *failureReason = [NSString
  612. stringWithFormat:@"Cannot parse topic name: '%@'. Will not subscribe.",
  613. topic];
  614. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging009, @"%@",
  615. failureReason);
  616. if (completion) {
  617. completion([NSError
  618. messagingErrorWithCode:kFIRMessagingErrorCodeInvalidTopicName
  619. failureReason:failureReason]);
  620. }
  621. }];
  622. }
  623. - (void)unsubscribeFromTopic:(NSString *)topic {
  624. [self unsubscribeFromTopic:topic completion:nil];
  625. }
  626. - (void)unsubscribeFromTopic:(NSString *)topic
  627. completion:(nullable FIRMessagingTopicOperationCompletion)completion {
  628. if ([FIRMessagingPubSub hasTopicsPrefix:topic]) {
  629. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTopicFormatIsDeprecated,
  630. @"Format '%@' is deprecated. Only '%@' should be used in "
  631. @"unsubscribeFromTopic.",
  632. topic, [FIRMessagingPubSub removePrefixFromTopic:topic]);
  633. }
  634. __weak FIRMessaging *weakSelf = self;
  635. [self retrieveFCMTokenForSenderID:self.tokenManager.fcmSenderID
  636. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error) {
  637. if (error) {
  638. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging012,
  639. @"The unsubscription operation failed due to "
  640. @"an error getting the FCM token: %@.",
  641. error);
  642. if (completion) {
  643. completion(error);
  644. }
  645. return;
  646. }
  647. FIRMessaging *strongSelf = weakSelf;
  648. NSString *normalizeTopic = [[strongSelf class] normalizeTopic:topic];
  649. if (normalizeTopic.length) {
  650. [strongSelf.pubsub unsubscribeFromTopic:normalizeTopic
  651. handler:completion];
  652. return;
  653. }
  654. NSString *failureReason = [NSString
  655. stringWithFormat:
  656. @"Cannot parse topic name: '%@'. Will not unsubscribe.", topic];
  657. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging011, @"%@",
  658. failureReason);
  659. if (completion) {
  660. completion([NSError
  661. messagingErrorWithCode:kFIRMessagingErrorCodeInvalidTopicName
  662. failureReason:failureReason]);
  663. }
  664. }];
  665. }
  666. #pragma mark - GULReachabilityDelegate
  667. - (void)reachability:(GULReachabilityChecker *)reachability
  668. statusChanged:(GULReachabilityStatus)status {
  669. [self onNetworkStatusChanged];
  670. }
  671. #pragma mark - Network
  672. - (void)onNetworkStatusChanged {
  673. if ([self isNetworkAvailable]) {
  674. [self.pubsub scheduleSync:YES];
  675. }
  676. }
  677. - (BOOL)isNetworkAvailable {
  678. GULReachabilityStatus status = self.reachability.reachabilityStatus;
  679. return (status == kGULReachabilityViaCellular || status == kGULReachabilityViaWifi);
  680. }
  681. - (FIRMessagingNetworkStatus)networkType {
  682. GULReachabilityStatus status = self.reachability.reachabilityStatus;
  683. if (![self isNetworkAvailable]) {
  684. return kFIRMessagingReachabilityNotReachable;
  685. } else if (status == kGULReachabilityViaCellular) {
  686. return kFIRMessagingReachabilityReachableViaWWAN;
  687. } else {
  688. return kFIRMessagingReachabilityReachableViaWiFi;
  689. }
  690. }
  691. #pragma mark - Notifications
  692. - (void)defaultFCMTokenWasRefreshed:(NSNotification *)notification {
  693. if (notification.object && ![notification.object isKindOfClass:[NSString class]]) {
  694. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging015,
  695. @"Invalid default FCM token type %@",
  696. NSStringFromClass([notification.object class]));
  697. return;
  698. }
  699. NSString *newToken = [(NSString *)notification.object copy];
  700. [self updateDefaultFCMToken:newToken];
  701. }
  702. - (void)updateDefaultFCMToken:(NSString *)defaultFCMToken {
  703. NSString *oldToken = self.tokenManager.defaultFCMToken;
  704. NSString *newToken = defaultFCMToken;
  705. if ([self.tokenManager hasTokenChangedFromOldToken:oldToken toNewToken:newToken]) {
  706. // Make sure to set default token first before notifying others.
  707. [self.tokenManager saveDefaultTokenInfoInKeychain:newToken];
  708. [self notifyDelegateOfFCMTokenAvailability];
  709. [self.pubsub scheduleSync:YES];
  710. }
  711. }
  712. - (void)notifyDelegateOfFCMTokenAvailability {
  713. __weak FIRMessaging *weakSelf = self;
  714. if (![NSThread isMainThread]) {
  715. dispatch_async(dispatch_get_main_queue(), ^{
  716. [weakSelf notifyDelegateOfFCMTokenAvailability];
  717. });
  718. return;
  719. }
  720. if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) {
  721. [self.delegate messaging:self didReceiveRegistrationToken:self.tokenManager.defaultFCMToken];
  722. }
  723. // Should always trigger the token refresh notification when the delegate method is called
  724. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  725. [center postNotificationName:FIRMessagingRegistrationTokenRefreshedNotification
  726. object:self.tokenManager.defaultFCMToken];
  727. }
  728. #pragma mark - Application Support Directory
  729. + (BOOL)hasSubDirectory:(NSString *)subDirectoryName {
  730. NSString *subDirectoryPath = [self pathForSubDirectory:subDirectoryName];
  731. BOOL isDirectory;
  732. if (![[NSFileManager defaultManager] fileExistsAtPath:subDirectoryPath
  733. isDirectory:&isDirectory]) {
  734. return NO;
  735. } else if (!isDirectory) {
  736. return NO;
  737. }
  738. return YES;
  739. }
  740. + (NSString *)pathForSubDirectory:(NSString *)subDirectoryName {
  741. NSArray *directoryPaths =
  742. NSSearchPathForDirectoriesInDomains(FIRMessagingSupportedDirectory(), NSUserDomainMask, YES);
  743. NSString *dirPath = directoryPaths.lastObject;
  744. NSArray *components = @[ dirPath, subDirectoryName ];
  745. return [NSString pathWithComponents:components];
  746. }
  747. + (BOOL)createSubDirectory:(NSString *)subDirectoryName {
  748. NSString *subDirectoryPath = [self pathForSubDirectory:subDirectoryName];
  749. BOOL hasSubDirectory;
  750. if (![[NSFileManager defaultManager] fileExistsAtPath:subDirectoryPath
  751. isDirectory:&hasSubDirectory]) {
  752. NSError *error;
  753. [[NSFileManager defaultManager] createDirectoryAtPath:subDirectoryPath
  754. withIntermediateDirectories:YES
  755. attributes:nil
  756. error:&error];
  757. if (error) {
  758. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging017,
  759. @"Cannot create directory %@, error: %@", subDirectoryPath, error);
  760. return NO;
  761. }
  762. } else {
  763. if (!hasSubDirectory) {
  764. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging018,
  765. @"Found file instead of directory at %@", subDirectoryPath);
  766. return NO;
  767. }
  768. }
  769. return YES;
  770. }
  771. #pragma mark - Locales
  772. + (NSString *)currentLocale {
  773. NSArray *locales = [self firebaseLocales];
  774. NSArray *preferredLocalizations =
  775. [NSBundle preferredLocalizationsFromArray:locales
  776. forPreferences:[NSLocale preferredLanguages]];
  777. NSString *legalDocsLanguage = [preferredLocalizations firstObject];
  778. // Use en as the default language
  779. return legalDocsLanguage ? legalDocsLanguage : @"en";
  780. }
  781. + (NSArray *)firebaseLocales {
  782. NSMutableArray *locales = [NSMutableArray array];
  783. NSDictionary *localesMap = [self firebaselocalesMap];
  784. for (NSString *key in localesMap) {
  785. [locales addObjectsFromArray:localesMap[key]];
  786. }
  787. return locales;
  788. }
  789. + (NSDictionary *)firebaselocalesMap {
  790. return @{
  791. // Albanian
  792. @"sq" : @[ @"sq_AL" ],
  793. // Belarusian
  794. @"be" : @[ @"be_BY" ],
  795. // Bulgarian
  796. @"bg" : @[ @"bg_BG" ],
  797. // Catalan
  798. @"ca" : @[ @"ca", @"ca_ES" ],
  799. // Croatian
  800. @"hr" : @[ @"hr", @"hr_HR" ],
  801. // Czech
  802. @"cs" : @[ @"cs", @"cs_CZ" ],
  803. // Danish
  804. @"da" : @[ @"da", @"da_DK" ],
  805. // Estonian
  806. @"et" : @[ @"et_EE" ],
  807. // Finnish
  808. @"fi" : @[ @"fi", @"fi_FI" ],
  809. // Hebrew
  810. @"he" : @[ @"he", @"iw_IL" ],
  811. // Hindi
  812. @"hi" : @[ @"hi_IN" ],
  813. // Hungarian
  814. @"hu" : @[ @"hu", @"hu_HU" ],
  815. // Icelandic
  816. @"is" : @[ @"is_IS" ],
  817. // Indonesian
  818. @"id" : @[ @"id", @"in_ID", @"id_ID" ],
  819. // Irish
  820. @"ga" : @[ @"ga_IE" ],
  821. // Korean
  822. @"ko" : @[ @"ko", @"ko_KR", @"ko-KR" ],
  823. // Latvian
  824. @"lv" : @[ @"lv_LV" ],
  825. // Lithuanian
  826. @"lt" : @[ @"lt_LT" ],
  827. // Macedonian
  828. @"mk" : @[ @"mk_MK" ],
  829. // Malay
  830. @"ms" : @[ @"ms_MY" ],
  831. // Maltese
  832. @"mt" : @[ @"mt_MT" ],
  833. // Polish
  834. @"pl" : @[ @"pl", @"pl_PL", @"pl-PL" ],
  835. // Romanian
  836. @"ro" : @[ @"ro", @"ro_RO" ],
  837. // Russian
  838. @"ru" : @[ @"ru_RU", @"ru", @"ru_BY", @"ru_KZ", @"ru-RU" ],
  839. // Slovak
  840. @"sk" : @[ @"sk", @"sk_SK" ],
  841. // Slovenian
  842. @"sl" : @[ @"sl_SI" ],
  843. // Swedish
  844. @"sv" : @[ @"sv", @"sv_SE", @"sv-SE" ],
  845. // Turkish
  846. @"tr" : @[ @"tr", @"tr-TR", @"tr_TR" ],
  847. // Ukrainian
  848. @"uk" : @[ @"uk", @"uk_UA" ],
  849. // Vietnamese
  850. @"vi" : @[ @"vi", @"vi_VN" ],
  851. // The following are groups of locales or locales that sub-divide a
  852. // language).
  853. // Arabic
  854. @"ar" : @[
  855. @"ar", @"ar_DZ", @"ar_BH", @"ar_EG", @"ar_IQ", @"ar_JO", @"ar_KW",
  856. @"ar_LB", @"ar_LY", @"ar_MA", @"ar_OM", @"ar_QA", @"ar_SA", @"ar_SD",
  857. @"ar_SY", @"ar_TN", @"ar_AE", @"ar_YE", @"ar_GB", @"ar-IQ", @"ar_US"
  858. ],
  859. // Simplified Chinese
  860. @"zh_Hans" : @[ @"zh_CN", @"zh_SG", @"zh-Hans" ],
  861. // Traditional Chinese
  862. @"zh_Hant" : @[ @"zh_HK", @"zh_TW", @"zh-Hant", @"zh-HK", @"zh-TW" ],
  863. // Dutch
  864. @"nl" : @[ @"nl", @"nl_BE", @"nl_NL", @"nl-NL" ],
  865. // English
  866. @"en" : @[
  867. @"en", @"en_AU", @"en_CA", @"en_IN", @"en_IE", @"en_MT", @"en_NZ", @"en_PH",
  868. @"en_SG", @"en_ZA", @"en_GB", @"en_US", @"en_AE", @"en-AE", @"en_AS", @"en-AU",
  869. @"en_BD", @"en-CA", @"en_EG", @"en_ES", @"en_GB", @"en-GB", @"en_HK", @"en_ID",
  870. @"en-IN", @"en_NG", @"en-PH", @"en_PK", @"en-SG", @"en-US"
  871. ],
  872. // French
  873. @"fr" :
  874. @[ @"fr", @"fr_BE", @"fr_CA", @"fr_FR", @"fr_LU", @"fr_CH", @"fr-CA", @"fr-FR", @"fr_MA" ],
  875. // German
  876. @"de" : @[ @"de", @"de_AT", @"de_DE", @"de_LU", @"de_CH", @"de-DE" ],
  877. // Greek
  878. @"el" : @[ @"el", @"el_CY", @"el_GR" ],
  879. // Italian
  880. @"it" : @[ @"it", @"it_IT", @"it_CH", @"it-IT" ],
  881. // Japanese
  882. @"ja" : @[ @"ja", @"ja_JP", @"ja_JP_JP", @"ja-JP" ],
  883. // Norwegian
  884. @"no" : @[ @"nb", @"no_NO", @"no_NO_NY", @"nb_NO" ],
  885. // Brazilian Portuguese
  886. @"pt_BR" : @[ @"pt_BR", @"pt-BR" ],
  887. // European Portuguese
  888. @"pt_PT" : @[ @"pt", @"pt_PT", @"pt-PT" ],
  889. // Serbian
  890. @"sr" : @[ @"sr_BA", @"sr_ME", @"sr_RS", @"sr_Latn_BA", @"sr_Latn_ME", @"sr_Latn_RS" ],
  891. // European Spanish
  892. @"es_ES" : @[ @"es", @"es_ES", @"es-ES" ],
  893. // Mexican Spanish
  894. @"es_MX" : @[ @"es-MX", @"es_MX", @"es_US", @"es-US" ],
  895. // Latin American Spanish
  896. @"es_419" : @[
  897. @"es_AR", @"es_BO", @"es_CL", @"es_CO", @"es_CR", @"es_DO", @"es_EC",
  898. @"es_SV", @"es_GT", @"es_HN", @"es_NI", @"es_PA", @"es_PY", @"es_PE",
  899. @"es_PR", @"es_UY", @"es_VE", @"es-AR", @"es-CL", @"es-CO"
  900. ],
  901. // Thai
  902. @"th" : @[ @"th", @"th_TH", @"th_TH_TH" ],
  903. };
  904. }
  905. #pragma mark - Utilities used by InstanceID
  906. + (NSString *)FIRMessagingSDKVersion {
  907. return FIRFirebaseVersion();
  908. }
  909. + (NSString *)FIRMessagingSDKCurrentLocale {
  910. return [self currentLocale];
  911. }
  912. #pragma mark - Force Category Linking
  913. #if SWIFT_PACKAGE || COCOAPODS || FIREBASE_BUILD_CARTHAGE || FIREBASE_BUILD_ZIP_FILE
  914. extern void FIRInclude_FIRMessaging_ExtensionHelper_Category(void);
  915. #endif // SWIFT_PACKAGE || COCOAPODS || FIREBASE_BUILD_CARTHAGE || FIREBASE_BUILD_ZIP_FILE
  916. extern void FIRInclude_NSDictionary_FIRMessaging_Category(void);
  917. extern void FIRInclude_NSError_FIRMessaging_Category(void);
  918. /// Does nothing when called, and not meant to be called.
  919. ///
  920. /// This method forces the linker to include categories even if
  921. /// users do not include the '-ObjC' linker flag in their project.
  922. + (void)noop {
  923. #if SWIFT_PACKAGE || COCOAPODS || FIREBASE_BUILD_CARTHAGE || FIREBASE_BUILD_ZIP_FILE
  924. FIRInclude_FIRMessaging_ExtensionHelper_Category();
  925. #endif // SWIFT_PACKAGE || COCOAPODS || FIREBASE_BUILD_CARTHAGE || FIREBASE_BUILD_ZIP_FILE
  926. FIRInclude_NSDictionary_FIRMessaging_Category();
  927. FIRInclude_NSError_FIRMessaging_Category();
  928. }
  929. @end