FIRMessaging.m 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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
  180. if (![GULAppEnvironmentUtil isAppExtension]) {
  181. [self didCompleteConfigure];
  182. }
  183. #endif
  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
  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
  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. [_messagingUserDefaults synchronize];
  429. if (!isFCMAutoInitEnabled && autoInitEnabled) {
  430. [self.tokenManager tokenAndRequestIfNotExist];
  431. }
  432. }
  433. - (NSString *)FCMToken {
  434. // Gets the current default token, and requets a new one if it doesn't exist.
  435. NSString *token = [self.tokenManager tokenAndRequestIfNotExist];
  436. return token;
  437. }
  438. - (void)tokenWithCompletion:(FIRMessagingFCMTokenFetchCompletion)completion {
  439. FIROptions *options = FIRApp.defaultApp.options;
  440. [self retrieveFCMTokenForSenderID:options.GCMSenderID completion:completion];
  441. }
  442. - (void)deleteTokenWithCompletion:(FIRMessagingDeleteFCMTokenCompletion)completion {
  443. FIROptions *options = FIRApp.defaultApp.options;
  444. [self deleteFCMTokenForSenderID:options.GCMSenderID completion:completion];
  445. }
  446. - (void)retrieveFCMTokenForSenderID:(nonnull NSString *)senderID
  447. completion:(nonnull FIRMessagingFCMTokenFetchCompletion)completion {
  448. if (!senderID.length) {
  449. NSString *description = @"Couldn't fetch token because a Sender ID was not supplied. A valid "
  450. @"Sender ID is required to fetch an FCM token";
  451. FIRMessagingLoggerError(kFIRMessagingMessageCodeSenderIDNotSuppliedForTokenFetch, @"%@",
  452. description);
  453. if (completion) {
  454. NSError *error = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeMissingAuthorizedEntity
  455. failureReason:description];
  456. completion(nil, error);
  457. }
  458. return;
  459. }
  460. NSDictionary *options = nil;
  461. if (self.APNSToken) {
  462. options = @{kFIRMessagingTokenOptionsAPNSKey : self.APNSToken};
  463. } else {
  464. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeAPNSTokenNotAvailableDuringTokenFetch,
  465. @"APNS device token not set before retrieving FCM Token for Sender ID "
  466. @"'%@'."
  467. @"Be sure to re-retrieve the FCM token once the APNS device token is "
  468. @"set.",
  469. senderID);
  470. }
  471. [self.tokenManager
  472. tokenWithAuthorizedEntity:senderID
  473. scope:kFIRMessagingDefaultTokenScope
  474. options:options
  475. handler:^(NSString *_Nullable FCMToken, NSError *_Nullable error) {
  476. if (completion) {
  477. completion(FCMToken, error);
  478. }
  479. }];
  480. }
  481. - (void)deleteFCMTokenForSenderID:(nonnull NSString *)senderID
  482. completion:(nonnull FIRMessagingDeleteFCMTokenCompletion)completion {
  483. if (!senderID.length) {
  484. NSString *description = @"Couldn't delete token because a Sender ID was not supplied. A "
  485. @"valid Sender ID is required to delete an FCM token";
  486. FIRMessagingLoggerError(kFIRMessagingMessageCodeSenderIDNotSuppliedForTokenDelete, @"%@",
  487. description);
  488. if (completion) {
  489. NSError *error = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeInvalidRequest
  490. failureReason:description];
  491. completion(error);
  492. }
  493. return;
  494. }
  495. FIRMessaging_WEAKIFY(self);
  496. [self.installations
  497. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  498. FIRMessaging_STRONGIFY(self);
  499. if (error) {
  500. NSError *newError = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeInvalidIdentity
  501. failureReason:@"Failed to get installation ID."];
  502. completion(newError);
  503. } else {
  504. [self.tokenManager deleteTokenWithAuthorizedEntity:senderID
  505. scope:kFIRMessagingDefaultTokenScope
  506. instanceID:identifier
  507. handler:^(NSError *_Nullable error) {
  508. if (completion) {
  509. completion(error);
  510. }
  511. }];
  512. }
  513. }];
  514. }
  515. - (void)deleteDataWithCompletion:(void (^)(NSError *_Nullable))completion {
  516. FIRMessaging_WEAKIFY(self);
  517. [self.tokenManager deleteWithHandler:^(NSError *error) {
  518. FIRMessaging_STRONGIFY(self);
  519. if (error) {
  520. if (completion) {
  521. completion(error);
  522. }
  523. return;
  524. }
  525. // Only request new token if FCM auto initialization is
  526. // enabled.
  527. if ([self isAutoInitEnabled]) {
  528. // Deletion succeeds! Requesting new checkin, IID and token.
  529. [self tokenWithCompletion:^(NSString *_Nullable token, NSError *_Nullable error) {
  530. if (completion) {
  531. completion(error);
  532. }
  533. }];
  534. return;
  535. }
  536. if (completion) {
  537. completion(nil);
  538. }
  539. }];
  540. }
  541. #pragma mark - FIRMessagingDelegate helper methods
  542. - (void)setDelegate:(id<FIRMessagingDelegate>)delegate {
  543. _delegate = delegate;
  544. [self validateDelegateConformsToTokenAvailabilityMethods];
  545. }
  546. // Check if the delegate conforms to |didReceiveRegistrationToken:|
  547. // and display a warning to the developer if not.
  548. // NOTE: Once |didReceiveRegistrationToken:| can be made a required method, this
  549. // check can be removed.
  550. - (void)validateDelegateConformsToTokenAvailabilityMethods {
  551. if (self.delegate && ![self.delegate respondsToSelector:@selector(messaging:
  552. didReceiveRegistrationToken:)]) {
  553. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTokenDelegateMethodsNotImplemented,
  554. @"The object %@ does not respond to "
  555. @"-messaging:didReceiveRegistrationToken:. Please implement "
  556. @"-messaging:didReceiveRegistrationToken: to be provided with an FCM "
  557. @"token.",
  558. self.delegate.description);
  559. }
  560. }
  561. - (void)notifyRefreshedFCMToken {
  562. __weak FIRMessaging *weakSelf = self;
  563. if (![NSThread isMainThread]) {
  564. dispatch_async(dispatch_get_main_queue(), ^{
  565. [weakSelf notifyRefreshedFCMToken];
  566. });
  567. return;
  568. }
  569. if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) {
  570. [self.delegate messaging:self didReceiveRegistrationToken:self.tokenManager.defaultFCMToken];
  571. }
  572. // Should always trigger the token refresh notification when the delegate method is called
  573. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  574. [center postNotificationName:FIRMessagingRegistrationTokenRefreshedNotification
  575. object:self.tokenManager.defaultFCMToken];
  576. }
  577. #pragma mark - Topics
  578. + (NSString *)normalizeTopic:(NSString *)topic {
  579. if (!topic.length) {
  580. return nil;
  581. }
  582. if (![FIRMessagingPubSub hasTopicsPrefix:topic]) {
  583. topic = [FIRMessagingPubSub addPrefixToTopic:topic];
  584. }
  585. if ([FIRMessagingPubSub isValidTopicWithPrefix:topic]) {
  586. return [topic copy];
  587. }
  588. return nil;
  589. }
  590. - (void)subscribeToTopic:(NSString *)topic {
  591. [self subscribeToTopic:topic completion:nil];
  592. }
  593. - (void)subscribeToTopic:(NSString *)topic
  594. completion:(nullable FIRMessagingTopicOperationCompletion)completion {
  595. if ([FIRMessagingPubSub hasTopicsPrefix:topic]) {
  596. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTopicFormatIsDeprecated,
  597. @"Format '%@' is deprecated. Only '%@' should be used in "
  598. @"subscribeToTopic.",
  599. topic, [FIRMessagingPubSub removePrefixFromTopic:topic]);
  600. }
  601. __weak FIRMessaging *weakSelf = self;
  602. [self
  603. retrieveFCMTokenForSenderID:self.tokenManager.fcmSenderID
  604. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error) {
  605. if (error) {
  606. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging010,
  607. @"The subscription operation failed due to an "
  608. @"error getting the FCM token: %@.",
  609. error);
  610. if (completion) {
  611. completion(error);
  612. }
  613. return;
  614. }
  615. FIRMessaging *strongSelf = weakSelf;
  616. NSString *normalizeTopic = [[strongSelf class] normalizeTopic:topic];
  617. if (normalizeTopic.length) {
  618. [strongSelf.pubsub subscribeToTopic:normalizeTopic handler:completion];
  619. return;
  620. }
  621. NSString *failureReason = [NSString
  622. stringWithFormat:@"Cannot parse topic name: '%@'. Will not subscribe.",
  623. topic];
  624. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging009, @"%@",
  625. failureReason);
  626. if (completion) {
  627. completion([NSError
  628. messagingErrorWithCode:kFIRMessagingErrorCodeInvalidTopicName
  629. failureReason:failureReason]);
  630. }
  631. }];
  632. }
  633. - (void)unsubscribeFromTopic:(NSString *)topic {
  634. [self unsubscribeFromTopic:topic completion:nil];
  635. }
  636. - (void)unsubscribeFromTopic:(NSString *)topic
  637. completion:(nullable FIRMessagingTopicOperationCompletion)completion {
  638. if ([FIRMessagingPubSub hasTopicsPrefix:topic]) {
  639. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTopicFormatIsDeprecated,
  640. @"Format '%@' is deprecated. Only '%@' should be used in "
  641. @"unsubscribeFromTopic.",
  642. topic, [FIRMessagingPubSub removePrefixFromTopic:topic]);
  643. }
  644. __weak FIRMessaging *weakSelf = self;
  645. [self retrieveFCMTokenForSenderID:self.tokenManager.fcmSenderID
  646. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error) {
  647. if (error) {
  648. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging012,
  649. @"The unsubscription operation failed due to "
  650. @"an error getting the FCM token: %@.",
  651. error);
  652. if (completion) {
  653. completion(error);
  654. }
  655. return;
  656. }
  657. FIRMessaging *strongSelf = weakSelf;
  658. NSString *normalizeTopic = [[strongSelf class] normalizeTopic:topic];
  659. if (normalizeTopic.length) {
  660. [strongSelf.pubsub unsubscribeFromTopic:normalizeTopic
  661. handler:completion];
  662. return;
  663. }
  664. NSString *failureReason = [NSString
  665. stringWithFormat:
  666. @"Cannot parse topic name: '%@'. Will not unsubscribe.", topic];
  667. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging011, @"%@",
  668. failureReason);
  669. if (completion) {
  670. completion([NSError
  671. messagingErrorWithCode:kFIRMessagingErrorCodeInvalidTopicName
  672. failureReason:failureReason]);
  673. }
  674. }];
  675. }
  676. #pragma mark - GULReachabilityDelegate
  677. - (void)reachability:(GULReachabilityChecker *)reachability
  678. statusChanged:(GULReachabilityStatus)status {
  679. [self onNetworkStatusChanged];
  680. }
  681. #pragma mark - Network
  682. - (void)onNetworkStatusChanged {
  683. if ([self isNetworkAvailable]) {
  684. [self.pubsub scheduleSync:YES];
  685. }
  686. }
  687. - (BOOL)isNetworkAvailable {
  688. GULReachabilityStatus status = self.reachability.reachabilityStatus;
  689. return (status == kGULReachabilityViaCellular || status == kGULReachabilityViaWifi);
  690. }
  691. - (FIRMessagingNetworkStatus)networkType {
  692. GULReachabilityStatus status = self.reachability.reachabilityStatus;
  693. if (![self isNetworkAvailable]) {
  694. return kFIRMessagingReachabilityNotReachable;
  695. } else if (status == kGULReachabilityViaCellular) {
  696. return kFIRMessagingReachabilityReachableViaWWAN;
  697. } else {
  698. return kFIRMessagingReachabilityReachableViaWiFi;
  699. }
  700. }
  701. #pragma mark - Notifications
  702. - (void)defaultFCMTokenWasRefreshed:(NSNotification *)notification {
  703. if (notification.object && ![notification.object isKindOfClass:[NSString class]]) {
  704. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging015,
  705. @"Invalid default FCM token type %@",
  706. NSStringFromClass([notification.object class]));
  707. return;
  708. }
  709. NSString *newToken = [(NSString *)notification.object copy];
  710. [self updateDefaultFCMToken:newToken];
  711. }
  712. - (void)updateDefaultFCMToken:(NSString *)defaultFCMToken {
  713. NSString *oldToken = self.tokenManager.defaultFCMToken;
  714. NSString *newToken = defaultFCMToken;
  715. if ([self.tokenManager hasTokenChangedFromOldToken:oldToken toNewToken:newToken]) {
  716. // Make sure to set default token first before notifying others.
  717. [self.tokenManager saveDefaultTokenInfoInKeychain:newToken];
  718. [self notifyDelegateOfFCMTokenAvailability];
  719. [self.pubsub scheduleSync:YES];
  720. }
  721. }
  722. - (void)notifyDelegateOfFCMTokenAvailability {
  723. __weak FIRMessaging *weakSelf = self;
  724. if (![NSThread isMainThread]) {
  725. dispatch_async(dispatch_get_main_queue(), ^{
  726. [weakSelf notifyDelegateOfFCMTokenAvailability];
  727. });
  728. return;
  729. }
  730. if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) {
  731. [self.delegate messaging:self didReceiveRegistrationToken:self.tokenManager.defaultFCMToken];
  732. }
  733. // Should always trigger the token refresh notification when the delegate method is called
  734. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  735. [center postNotificationName:FIRMessagingRegistrationTokenRefreshedNotification
  736. object:self.tokenManager.defaultFCMToken];
  737. }
  738. #pragma mark - Application Support Directory
  739. + (BOOL)hasSubDirectory:(NSString *)subDirectoryName {
  740. NSString *subDirectoryPath = [self pathForSubDirectory:subDirectoryName];
  741. BOOL isDirectory;
  742. if (![[NSFileManager defaultManager] fileExistsAtPath:subDirectoryPath
  743. isDirectory:&isDirectory]) {
  744. return NO;
  745. } else if (!isDirectory) {
  746. return NO;
  747. }
  748. return YES;
  749. }
  750. + (NSString *)pathForSubDirectory:(NSString *)subDirectoryName {
  751. NSArray *directoryPaths =
  752. NSSearchPathForDirectoriesInDomains(FIRMessagingSupportedDirectory(), NSUserDomainMask, YES);
  753. NSString *dirPath = directoryPaths.lastObject;
  754. NSArray *components = @[ dirPath, subDirectoryName ];
  755. return [NSString pathWithComponents:components];
  756. }
  757. + (BOOL)createSubDirectory:(NSString *)subDirectoryName {
  758. NSString *subDirectoryPath = [self pathForSubDirectory:subDirectoryName];
  759. BOOL hasSubDirectory;
  760. if (![[NSFileManager defaultManager] fileExistsAtPath:subDirectoryPath
  761. isDirectory:&hasSubDirectory]) {
  762. NSError *error;
  763. [[NSFileManager defaultManager] createDirectoryAtPath:subDirectoryPath
  764. withIntermediateDirectories:YES
  765. attributes:nil
  766. error:&error];
  767. if (error) {
  768. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging017,
  769. @"Cannot create directory %@, error: %@", subDirectoryPath, error);
  770. return NO;
  771. }
  772. } else {
  773. if (!hasSubDirectory) {
  774. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging018,
  775. @"Found file instead of directory at %@", subDirectoryPath);
  776. return NO;
  777. }
  778. }
  779. return YES;
  780. }
  781. #pragma mark - Locales
  782. + (NSString *)currentLocale {
  783. NSArray *locales = [self firebaseLocales];
  784. NSArray *preferredLocalizations =
  785. [NSBundle preferredLocalizationsFromArray:locales
  786. forPreferences:[NSLocale preferredLanguages]];
  787. NSString *legalDocsLanguage = [preferredLocalizations firstObject];
  788. // Use en as the default language
  789. return legalDocsLanguage ? legalDocsLanguage : @"en";
  790. }
  791. + (NSArray *)firebaseLocales {
  792. NSMutableArray *locales = [NSMutableArray array];
  793. NSDictionary *localesMap = [self firebaselocalesMap];
  794. for (NSString *key in localesMap) {
  795. [locales addObjectsFromArray:localesMap[key]];
  796. }
  797. return locales;
  798. }
  799. + (NSDictionary *)firebaselocalesMap {
  800. return @{
  801. // Albanian
  802. @"sq" : @[ @"sq_AL" ],
  803. // Belarusian
  804. @"be" : @[ @"be_BY" ],
  805. // Bulgarian
  806. @"bg" : @[ @"bg_BG" ],
  807. // Catalan
  808. @"ca" : @[ @"ca", @"ca_ES" ],
  809. // Croatian
  810. @"hr" : @[ @"hr", @"hr_HR" ],
  811. // Czech
  812. @"cs" : @[ @"cs", @"cs_CZ" ],
  813. // Danish
  814. @"da" : @[ @"da", @"da_DK" ],
  815. // Estonian
  816. @"et" : @[ @"et_EE" ],
  817. // Finnish
  818. @"fi" : @[ @"fi", @"fi_FI" ],
  819. // Hebrew
  820. @"he" : @[ @"he", @"iw_IL" ],
  821. // Hindi
  822. @"hi" : @[ @"hi_IN" ],
  823. // Hungarian
  824. @"hu" : @[ @"hu", @"hu_HU" ],
  825. // Icelandic
  826. @"is" : @[ @"is_IS" ],
  827. // Indonesian
  828. @"id" : @[ @"id", @"in_ID", @"id_ID" ],
  829. // Irish
  830. @"ga" : @[ @"ga_IE" ],
  831. // Korean
  832. @"ko" : @[ @"ko", @"ko_KR", @"ko-KR" ],
  833. // Latvian
  834. @"lv" : @[ @"lv_LV" ],
  835. // Lithuanian
  836. @"lt" : @[ @"lt_LT" ],
  837. // Macedonian
  838. @"mk" : @[ @"mk_MK" ],
  839. // Malay
  840. @"ms" : @[ @"ms_MY" ],
  841. // Maltese
  842. @"mt" : @[ @"mt_MT" ],
  843. // Polish
  844. @"pl" : @[ @"pl", @"pl_PL", @"pl-PL" ],
  845. // Romanian
  846. @"ro" : @[ @"ro", @"ro_RO" ],
  847. // Russian
  848. @"ru" : @[ @"ru_RU", @"ru", @"ru_BY", @"ru_KZ", @"ru-RU" ],
  849. // Slovak
  850. @"sk" : @[ @"sk", @"sk_SK" ],
  851. // Slovenian
  852. @"sl" : @[ @"sl_SI" ],
  853. // Swedish
  854. @"sv" : @[ @"sv", @"sv_SE", @"sv-SE" ],
  855. // Turkish
  856. @"tr" : @[ @"tr", @"tr-TR", @"tr_TR" ],
  857. // Ukrainian
  858. @"uk" : @[ @"uk", @"uk_UA" ],
  859. // Vietnamese
  860. @"vi" : @[ @"vi", @"vi_VN" ],
  861. // The following are groups of locales or locales that sub-divide a
  862. // language).
  863. // Arabic
  864. @"ar" : @[
  865. @"ar", @"ar_DZ", @"ar_BH", @"ar_EG", @"ar_IQ", @"ar_JO", @"ar_KW",
  866. @"ar_LB", @"ar_LY", @"ar_MA", @"ar_OM", @"ar_QA", @"ar_SA", @"ar_SD",
  867. @"ar_SY", @"ar_TN", @"ar_AE", @"ar_YE", @"ar_GB", @"ar-IQ", @"ar_US"
  868. ],
  869. // Simplified Chinese
  870. @"zh_Hans" : @[ @"zh_CN", @"zh_SG", @"zh-Hans" ],
  871. // Traditional Chinese
  872. @"zh_Hant" : @[ @"zh_HK", @"zh_TW", @"zh-Hant", @"zh-HK", @"zh-TW" ],
  873. // Dutch
  874. @"nl" : @[ @"nl", @"nl_BE", @"nl_NL", @"nl-NL" ],
  875. // English
  876. @"en" : @[
  877. @"en", @"en_AU", @"en_CA", @"en_IN", @"en_IE", @"en_MT", @"en_NZ", @"en_PH",
  878. @"en_SG", @"en_ZA", @"en_GB", @"en_US", @"en_AE", @"en-AE", @"en_AS", @"en-AU",
  879. @"en_BD", @"en-CA", @"en_EG", @"en_ES", @"en_GB", @"en-GB", @"en_HK", @"en_ID",
  880. @"en-IN", @"en_NG", @"en-PH", @"en_PK", @"en-SG", @"en-US"
  881. ],
  882. // French
  883. @"fr" :
  884. @[ @"fr", @"fr_BE", @"fr_CA", @"fr_FR", @"fr_LU", @"fr_CH", @"fr-CA", @"fr-FR", @"fr_MA" ],
  885. // German
  886. @"de" : @[ @"de", @"de_AT", @"de_DE", @"de_LU", @"de_CH", @"de-DE" ],
  887. // Greek
  888. @"el" : @[ @"el", @"el_CY", @"el_GR" ],
  889. // Italian
  890. @"it" : @[ @"it", @"it_IT", @"it_CH", @"it-IT" ],
  891. // Japanese
  892. @"ja" : @[ @"ja", @"ja_JP", @"ja_JP_JP", @"ja-JP" ],
  893. // Norwegian
  894. @"no" : @[ @"nb", @"no_NO", @"no_NO_NY", @"nb_NO" ],
  895. // Brazilian Portuguese
  896. @"pt_BR" : @[ @"pt_BR", @"pt-BR" ],
  897. // European Portuguese
  898. @"pt_PT" : @[ @"pt", @"pt_PT", @"pt-PT" ],
  899. // Serbian
  900. @"sr" : @[ @"sr_BA", @"sr_ME", @"sr_RS", @"sr_Latn_BA", @"sr_Latn_ME", @"sr_Latn_RS" ],
  901. // European Spanish
  902. @"es_ES" : @[ @"es", @"es_ES", @"es-ES" ],
  903. // Mexican Spanish
  904. @"es_MX" : @[ @"es-MX", @"es_MX", @"es_US", @"es-US" ],
  905. // Latin American Spanish
  906. @"es_419" : @[
  907. @"es_AR", @"es_BO", @"es_CL", @"es_CO", @"es_CR", @"es_DO", @"es_EC",
  908. @"es_SV", @"es_GT", @"es_HN", @"es_NI", @"es_PA", @"es_PY", @"es_PE",
  909. @"es_PR", @"es_UY", @"es_VE", @"es-AR", @"es-CL", @"es-CO"
  910. ],
  911. // Thai
  912. @"th" : @[ @"th", @"th_TH", @"th_TH_TH" ],
  913. };
  914. }
  915. #pragma mark - Utilities used by InstanceID
  916. + (NSString *)FIRMessagingSDKVersion {
  917. return FIRFirebaseVersion();
  918. }
  919. + (NSString *)FIRMessagingSDKCurrentLocale {
  920. return [self currentLocale];
  921. }
  922. @end