FIRMessaging.m 41 KB

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