FIRMessaging.m 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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 "FIRMessaging.h"
  20. #import "FIRMessaging_Private.h"
  21. #import <UIKit/UIKit.h>
  22. #import "FIRMessagingClient.h"
  23. #import "FIRMessagingConstants.h"
  24. #import "FIRMessagingContextManagerService.h"
  25. #import "FIRMessagingDataMessageManager.h"
  26. #import "FIRMessagingDefines.h"
  27. #import "FIRMessagingLogger.h"
  28. #import "FIRMessagingPubSub.h"
  29. #import "FIRMessagingReceiver.h"
  30. #import "FIRMessagingRmqManager.h"
  31. #import "FIRMessagingSyncMessageManager.h"
  32. #import "FIRMessagingUtilities.h"
  33. #import "FIRMessagingVersionUtilities.h"
  34. #import <FirebaseCore/FIRReachabilityChecker.h>
  35. #import <FirebaseInstanceID/FirebaseInstanceID.h>
  36. #import "NSError+FIRMessaging.h"
  37. static NSString *const kFIRMessagingMessageViaAPNSRootKey = @"aps";
  38. static NSString *const kFIRMessagingReachabilityHostname = @"www.google.com";
  39. static NSString *const kFIRMessagingDefaultTokenScope = @"*";
  40. static NSString *const kFIRMessagingFCMTokenFetchAPNSOption = @"apns_token";
  41. #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  42. const NSNotificationName FIRMessagingSendSuccessNotification =
  43. @"com.firebase.messaging.notif.send-success";
  44. const NSNotificationName FIRMessagingSendErrorNotification =
  45. @"com.firebase.messaging.notif.send-error";
  46. const NSNotificationName FIRMessagingMessagesDeletedNotification =
  47. @"com.firebase.messaging.notif.messages-deleted";
  48. const NSNotificationName FIRMessagingConnectionStateChangedNotification =
  49. @"com.firebase.messaging.notif.connection-state-changed";
  50. const NSNotificationName FIRMessagingRegistrationTokenRefreshedNotification =
  51. @"com.firebase.messaging.notif.fcm-token-refreshed";
  52. #else
  53. NSString *const FIRMessagingSendSuccessNotification =
  54. @"com.firebase.messaging.notif.send-success";
  55. NSString *const FIRMessagingSendErrorNotification =
  56. @"com.firebase.messaging.notif.send-error";
  57. NSString * const FIRMessagingMessagesDeletedNotification =
  58. @"com.firebase.messaging.notif.messages-deleted";
  59. NSString * const FIRMessagingConnectionStateChangedNotification =
  60. @"com.firebase.messaging.notif.connection-state-changed";
  61. NSString * const FIRMessagingRegistrationTokenRefreshedNotification =
  62. @"com.firebase.messaging.notif.fcm-token-refreshed";
  63. #endif // defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  64. NSString *const kFIRMessagingUserDefaultsKeyAutoInitEnabled =
  65. @"com.firebase.messaging.auto-init.enabled"; // Auto Init Enabled key stored in NSUserDefaults
  66. NSString *const kFIRMessagingAPNSTokenType = @"APNSTokenType"; // APNS Token type key stored in user info.
  67. static NSString *const kFIRMessagingPlistAutoInitEnabled =
  68. @"FirebaseMessagingAutoInitEnabled"; // Auto Init Enabled key stored in Info.plist
  69. @interface FIRMessagingMessageInfo ()
  70. @property(nonatomic, readwrite, assign) FIRMessagingMessageStatus status;
  71. @end
  72. @implementation FIRMessagingMessageInfo
  73. - (instancetype)init {
  74. FIRMessagingInvalidateInitializer();
  75. }
  76. - (instancetype)initWithStatus:(FIRMessagingMessageStatus)status {
  77. self = [super init];
  78. if (self) {
  79. _status = status;
  80. }
  81. return self;
  82. }
  83. @end
  84. #pragma mark - for iOS 10 compatibility
  85. @implementation FIRMessagingRemoteMessage
  86. - (instancetype)init {
  87. self = [super init];
  88. if (self) {
  89. _appData = [[NSMutableDictionary alloc] init];
  90. }
  91. return self;
  92. }
  93. - (instancetype)initWithMessage:(FIRMessagingRemoteMessage *)message {
  94. self = [self init];
  95. if (self) {
  96. _appData = [message.appData copy];
  97. }
  98. return self;
  99. }
  100. @end
  101. @interface FIRMessaging ()<FIRMessagingClientDelegate, FIRMessagingReceiverDelegate,
  102. FIRReachabilityDelegate>
  103. // FIRApp properties
  104. @property(nonatomic, readwrite, copy) NSString *fcmSenderID;
  105. @property(nonatomic, readwrite, strong) NSData *apnsTokenData;
  106. @property(nonatomic, readwrite, strong) NSString *defaultFcmToken;
  107. @property(nonatomic, readwrite, strong) FIRInstanceID *instanceID;
  108. @property(nonatomic, readwrite, assign) BOOL isClientSetup;
  109. @property(nonatomic, readwrite, strong) FIRMessagingClient *client;
  110. @property(nonatomic, readwrite, strong) FIRReachabilityChecker *reachability;
  111. @property(nonatomic, readwrite, strong) FIRMessagingDataMessageManager *dataMessageManager;
  112. @property(nonatomic, readwrite, strong) FIRMessagingPubSub *pubsub;
  113. @property(nonatomic, readwrite, strong) FIRMessagingRmqManager *rmq2Manager;
  114. @property(nonatomic, readwrite, strong) FIRMessagingReceiver *receiver;
  115. @property(nonatomic, readwrite, strong) FIRMessagingSyncMessageManager *syncMessageManager;
  116. @property(nonatomic, readwrite, strong) NSUserDefaults *messagingUserDefaults;
  117. /// Message ID's logged for analytics. This prevents us from logging the same message twice
  118. /// which can happen if the user inadvertently calls `appDidReceiveMessage` along with us
  119. /// calling it implicitly during swizzling.
  120. @property(nonatomic, readwrite, strong) NSMutableSet *loggedMessageIDs;
  121. - (instancetype)initWithInstanceID:(FIRInstanceID *)instanceID
  122. userDefaults:(NSUserDefaults *)defaults NS_DESIGNATED_INITIALIZER;
  123. @end
  124. @implementation FIRMessaging
  125. + (FIRMessaging *)messaging {
  126. static FIRMessaging *messaging;
  127. static dispatch_once_t onceToken;
  128. dispatch_once(&onceToken, ^{
  129. messaging = [[FIRMessaging alloc] initPrivately];
  130. [messaging start];
  131. });
  132. return messaging;
  133. }
  134. - (instancetype)initWithInstanceID:(FIRInstanceID *)instanceID
  135. userDefaults:(NSUserDefaults *)defaults {
  136. self = [super init];
  137. if (self != nil) {
  138. _loggedMessageIDs = [NSMutableSet set];
  139. _instanceID = instanceID;
  140. _messagingUserDefaults = defaults;
  141. }
  142. return self;
  143. }
  144. - (instancetype)initPrivately {
  145. return [self initWithInstanceID:[FIRInstanceID instanceID]
  146. userDefaults:[NSUserDefaults standardUserDefaults]];
  147. }
  148. - (void)dealloc {
  149. [self.reachability stop];
  150. [[NSNotificationCenter defaultCenter] removeObserver:self];
  151. [self teardown];
  152. }
  153. #pragma mark - Config
  154. - (void)start {
  155. // Print the library version for logging.
  156. NSString *currentLibraryVersion = FIRMessagingCurrentLibraryVersion();
  157. FIRMessagingLoggerInfo(kFIRMessagingMessageCodeMessagingPrintLibraryVersion,
  158. @"FIRMessaging library version %@",
  159. currentLibraryVersion);
  160. [self setupReceiver];
  161. NSString *hostname = kFIRMessagingReachabilityHostname;
  162. self.reachability = [[FIRReachabilityChecker alloc] initWithReachabilityDelegate:self
  163. loggerDelegate:nil
  164. withHost:hostname];
  165. [self.reachability start];
  166. [self setupApplicationSupportSubDirectory];
  167. // setup FIRMessaging objects
  168. [self setupRmqManager];
  169. [self setupClient];
  170. [self setupSyncMessageManager];
  171. [self setupDataMessageManager];
  172. [self setupTopics];
  173. self.isClientSetup = YES;
  174. [self setupNotificationListeners];
  175. }
  176. - (void)setupApplicationSupportSubDirectory {
  177. NSString *messagingSubDirectory = kFIRMessagingApplicationSupportSubDirectory;
  178. if (![[self class] hasApplicationSupportSubDirectory:messagingSubDirectory]) {
  179. [[self class] createApplicationSupportSubDirectory:messagingSubDirectory];
  180. }
  181. }
  182. - (void)setupNotificationListeners {
  183. // To prevent multiple notifications remove self as observer for all events.
  184. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  185. [center removeObserver:self];
  186. [center addObserver:self
  187. selector:@selector(didReceiveDefaultInstanceIDToken:)
  188. name:kFIRMessagingFCMTokenNotification
  189. object:nil];
  190. [center addObserver:self
  191. selector:@selector(defaultInstanceIDTokenWasRefreshed:)
  192. name:kFIRMessagingRegistrationTokenRefreshNotification
  193. object:nil];
  194. [center addObserver:self
  195. selector:@selector(applicationStateChanged)
  196. name:UIApplicationDidBecomeActiveNotification
  197. object:nil];
  198. [center addObserver:self
  199. selector:@selector(applicationStateChanged)
  200. name:UIApplicationDidEnterBackgroundNotification
  201. object:nil];
  202. }
  203. - (void)setupReceiver {
  204. self.receiver = [[FIRMessagingReceiver alloc] init];
  205. self.receiver.delegate = self;
  206. }
  207. - (void)setupClient {
  208. self.client = [[FIRMessagingClient alloc] initWithDelegate:self
  209. reachability:self.reachability
  210. rmq2Manager:self.rmq2Manager];
  211. }
  212. - (void)setupDataMessageManager {
  213. self.dataMessageManager =
  214. [[FIRMessagingDataMessageManager alloc] initWithDelegate:self.receiver
  215. client:self.client
  216. rmq2Manager:self.rmq2Manager
  217. syncMessageManager:self.syncMessageManager];
  218. [self.dataMessageManager refreshDelayedMessages];
  219. [self.client setDataMessageManager:self.dataMessageManager];
  220. }
  221. - (void)setupRmqManager {
  222. self.rmq2Manager = [[FIRMessagingRmqManager alloc] initWithDatabaseName:@"rmq2"];
  223. [self.rmq2Manager loadRmqId];
  224. }
  225. - (void)setupTopics {
  226. _FIRMessagingDevAssert(self.client, @"Invalid nil client before init pubsub.");
  227. self.pubsub = [[FIRMessagingPubSub alloc] initWithClient:self.client];
  228. }
  229. - (void)setupSyncMessageManager {
  230. self.syncMessageManager =
  231. [[FIRMessagingSyncMessageManager alloc] initWithRmqManager:self.rmq2Manager];
  232. // Delete the expired messages with a delay. We don't want to block startup with a somewhat
  233. // expensive db call.
  234. FIRMessaging_WEAKIFY(self);
  235. dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC));
  236. dispatch_after(time, dispatch_get_main_queue(), ^{
  237. FIRMessaging_STRONGIFY(self);
  238. [self.syncMessageManager removeExpiredSyncMessages];
  239. });
  240. }
  241. - (void)teardown {
  242. _FIRMessagingDevAssert([NSThread isMainThread],
  243. @"FIRMessaging should be called from main thread only.");
  244. [self.client teardown];
  245. self.pubsub = nil;
  246. self.syncMessageManager = nil;
  247. self.rmq2Manager = nil;
  248. self.dataMessageManager = nil;
  249. self.client = nil;
  250. self.isClientSetup = NO;
  251. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging001, @"Did successfully teardown");
  252. }
  253. #pragma mark - Messages
  254. - (FIRMessagingMessageInfo *)appDidReceiveMessage:(NSDictionary *)message {
  255. if (!message.count) {
  256. return [[FIRMessagingMessageInfo alloc] initWithStatus:FIRMessagingMessageStatusUnknown];
  257. }
  258. // For downstream messages that go via MCS we should strip out this key before sending
  259. // the message to the device.
  260. BOOL isOldMessage = NO;
  261. NSString *messageID = message[kFIRMessagingMessageIDKey];
  262. if ([messageID length]) {
  263. [self.rmq2Manager saveS2dMessageWithRmqId:messageID];
  264. BOOL isSyncMessage = [[self class] isAPNSSyncMessage:message];
  265. if (isSyncMessage) {
  266. isOldMessage = [self.syncMessageManager didReceiveAPNSSyncMessage:message];
  267. }
  268. }
  269. // Prevent duplicates by keeping a cache of all the logged messages during each session.
  270. // The duplicates only happen when the 3P app calls `appDidReceiveMessage:` along with
  271. // us swizzling their implementation to call the same method implicitly.
  272. if (!isOldMessage && messageID.length) {
  273. isOldMessage = [self.loggedMessageIDs containsObject:messageID];
  274. if (!isOldMessage) {
  275. [self.loggedMessageIDs addObject:messageID];
  276. }
  277. }
  278. if (!isOldMessage) {
  279. Class firMessagingLogClass = NSClassFromString(@"FIRMessagingLog");
  280. SEL logMessageSelector = NSSelectorFromString(@"logMessage:");
  281. if ([firMessagingLogClass respondsToSelector:logMessageSelector]) {
  282. #pragma clang diagnostic push
  283. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  284. [firMessagingLogClass performSelector:logMessageSelector
  285. withObject:message];
  286. }
  287. #pragma clang diagnostic pop
  288. [self handleContextManagerMessage:message];
  289. [self handleIncomingLinkIfNeededFromMessage:message];
  290. }
  291. return [[FIRMessagingMessageInfo alloc] initWithStatus:FIRMessagingMessageStatusNew];
  292. }
  293. - (BOOL)handleContextManagerMessage:(NSDictionary *)message {
  294. if ([FIRMessagingContextManagerService isContextManagerMessage:message]) {
  295. return [FIRMessagingContextManagerService handleContextManagerMessage:message];
  296. }
  297. return NO;
  298. }
  299. + (BOOL)isAPNSSyncMessage:(NSDictionary *)message {
  300. if ([message[kFIRMessagingMessageViaAPNSRootKey] isKindOfClass:[NSDictionary class]]) {
  301. NSDictionary *aps = message[kFIRMessagingMessageViaAPNSRootKey];
  302. return [aps[kFIRMessagingMessageAPNSContentAvailableKey] boolValue];
  303. }
  304. return NO;
  305. }
  306. - (void)handleIncomingLinkIfNeededFromMessage:(NSDictionary *)message {
  307. NSURL *url = [self linkURLFromMessage:message];
  308. if (url == nil) {
  309. return;
  310. }
  311. if (![NSThread isMainThread]) {
  312. dispatch_async(dispatch_get_main_queue(), ^{
  313. [self handleIncomingLinkIfNeededFromMessage:message];
  314. });
  315. return;
  316. }
  317. UIApplication *application = [UIApplication sharedApplication];
  318. id<UIApplicationDelegate> appDelegate = application.delegate;
  319. SEL continueUserActivitySelector =
  320. @selector(application:continueUserActivity:restorationHandler:);
  321. SEL openURLWithOptionsSelector = @selector(application:openURL:options:);
  322. SEL openURLWithSourceApplicationSelector =
  323. @selector(application:openURL:sourceApplication:annotation:);
  324. SEL handleOpenURLSelector = @selector(application:handleOpenURL:);
  325. // Due to FIRAAppDelegateProxy swizzling, this selector will most likely get chosen, whether or
  326. // not the actual application has implemented
  327. // |application:continueUserActivity:restorationHandler:|. A warning will be displayed to the user
  328. // if they haven't implemented it.
  329. if ([NSUserActivity class] != nil &&
  330. [appDelegate respondsToSelector:continueUserActivitySelector]) {
  331. NSUserActivity *userActivity =
  332. [[NSUserActivity alloc] initWithActivityType:NSUserActivityTypeBrowsingWeb];
  333. userActivity.webpageURL = url;
  334. [appDelegate application:application
  335. continueUserActivity:userActivity
  336. restorationHandler:^(NSArray * _Nullable restorableObjects) {
  337. // Do nothing, as we don't support the app calling this block
  338. }];
  339. } else if ([appDelegate respondsToSelector:openURLWithOptionsSelector]) {
  340. #pragma clang diagnostic push
  341. #pragma clang diagnostic ignored "-Wunguarded-availability"
  342. [appDelegate application:application openURL:url options:@{}];
  343. #pragma clang diagnostic pop
  344. // Similarly, |application:openURL:sourceApplication:annotation:| will also always be called, due
  345. // to the default swizzling done by FIRAAppDelegateProxy in Firebase Analytics
  346. } else if ([appDelegate respondsToSelector:openURLWithSourceApplicationSelector]) {
  347. [appDelegate application:application
  348. openURL:url
  349. sourceApplication:FIRMessagingAppIdentifier()
  350. annotation:@{}];
  351. } else if ([appDelegate respondsToSelector:handleOpenURLSelector]) {
  352. [appDelegate application:application handleOpenURL:url];
  353. }
  354. }
  355. - (NSURL *)linkURLFromMessage:(NSDictionary *)message {
  356. NSString *urlString = message[kFIRMessagingMessageLinkKey];
  357. if (urlString == nil || ![urlString isKindOfClass:[NSString class]] || urlString.length == 0) {
  358. return nil;
  359. }
  360. NSURL *url = [NSURL URLWithString:urlString];
  361. return url;
  362. }
  363. #pragma mark - APNS
  364. - (NSData *)APNSToken {
  365. return self.apnsTokenData;
  366. }
  367. - (void)setAPNSToken:(NSData *)APNSToken {
  368. [self setAPNSToken:APNSToken type:FIRMessagingAPNSTokenTypeUnknown];
  369. }
  370. - (void)setAPNSToken:(NSData *)apnsToken type:(FIRMessagingAPNSTokenType)type {
  371. if ([apnsToken isEqual:self.apnsTokenData]) {
  372. return;
  373. }
  374. self.apnsTokenData = apnsToken;
  375. // Notify InstanceID that APNS Token has been set.
  376. NSDictionary *userInfo = @{kFIRMessagingAPNSTokenType : @(type)};
  377. NSNotification *notification =
  378. [NSNotification notificationWithName:kFIRMessagingAPNSTokenNotification
  379. object:[apnsToken copy]
  380. userInfo:userInfo];
  381. [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
  382. }
  383. #pragma mark - FCM
  384. - (BOOL)isAutoInitEnabled {
  385. // Check storage
  386. id isAutoInitEnabledObject =
  387. [_messagingUserDefaults objectForKey:kFIRMessagingUserDefaultsKeyAutoInitEnabled];
  388. if (isAutoInitEnabledObject) {
  389. return [isAutoInitEnabledObject boolValue];
  390. }
  391. // Check Info.plist
  392. isAutoInitEnabledObject =
  393. [[NSBundle mainBundle] objectForInfoDictionaryKey:kFIRMessagingPlistAutoInitEnabled];
  394. if (isAutoInitEnabledObject) {
  395. return [isAutoInitEnabledObject boolValue];
  396. }
  397. // If none of above exists, we default assume FCM auto init is enabled.
  398. return YES;
  399. }
  400. - (void)setAutoInitEnabled:(BOOL)autoInitEnabled {
  401. BOOL isFCMAutoInitEnabled = [self isAutoInitEnabled];
  402. [_messagingUserDefaults setBool:autoInitEnabled
  403. forKey:kFIRMessagingUserDefaultsKeyAutoInitEnabled];
  404. if (!isFCMAutoInitEnabled && autoInitEnabled) {
  405. self.defaultFcmToken = self.instanceID.token;
  406. }
  407. }
  408. - (NSString *)FCMToken {
  409. NSString *token = self.defaultFcmToken;
  410. if (!token) {
  411. // We may not have received it from Instance ID yet (via NSNotification), so extract it directly
  412. token = self.instanceID.token;
  413. }
  414. return token;
  415. }
  416. - (void)retrieveFCMTokenForSenderID:(nonnull NSString *)senderID
  417. completion:(nonnull FIRMessagingFCMTokenFetchCompletion)completion {
  418. if (!senderID.length) {
  419. FIRMessagingLoggerError(kFIRMessagingMessageCodeSenderIDNotSuppliedForTokenFetch,
  420. @"Sender ID not supplied. It is required for a token fetch, "
  421. @"to identify the sender.");
  422. if (completion) {
  423. NSString *description = @"Couldn't fetch token because a Sender ID was not supplied. A valid "
  424. @"Sender ID is required to fetch an FCM token";
  425. NSError *error = [NSError fcm_errorWithCode:FIRMessagingErrorInvalidRequest
  426. userInfo:@{NSLocalizedDescriptionKey : description}];
  427. completion(nil, error);
  428. }
  429. return;
  430. }
  431. NSDictionary *options = nil;
  432. if (self.APNSToken) {
  433. options = @{kFIRMessagingFCMTokenFetchAPNSOption : self.APNSToken};
  434. } else {
  435. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeAPNSTokenNotAvailableDuringTokenFetch,
  436. @"APNS device token not set before retrieving FCM Token for Sender ID "
  437. @"'%@'. Notifications to this FCM Token will not be delivered over APNS."
  438. @"Be sure to re-retrieve the FCM token once the APNS device token is "
  439. @"set.", senderID);
  440. }
  441. [self.instanceID tokenWithAuthorizedEntity:senderID
  442. scope:kFIRMessagingDefaultTokenScope
  443. options:options
  444. handler:completion];
  445. }
  446. - (void)deleteFCMTokenForSenderID:(nonnull NSString *)senderID
  447. completion:(nonnull FIRMessagingDeleteFCMTokenCompletion)completion {
  448. if (!senderID.length) {
  449. FIRMessagingLoggerError(kFIRMessagingMessageCodeSenderIDNotSuppliedForTokenDelete,
  450. @"Sender ID not supplied. It is required to delete an FCM token.");
  451. if (completion) {
  452. NSString *description = @"Couldn't delete token because a Sender ID was not supplied. A "
  453. @"valid Sender ID is required to delete an FCM token";
  454. NSError *error = [NSError fcm_errorWithCode:FIRMessagingErrorInvalidRequest
  455. userInfo:@{NSLocalizedDescriptionKey : description}];
  456. completion(error);
  457. }
  458. return;
  459. }
  460. [self.instanceID deleteTokenWithAuthorizedEntity:senderID
  461. scope:kFIRMessagingDefaultTokenScope
  462. handler:completion];
  463. }
  464. #pragma mark - FIRMessagingDelegate helper methods
  465. - (void)setDelegate:(id<FIRMessagingDelegate>)delegate {
  466. _delegate = delegate;
  467. [self validateDelegateConformsToTokenAvailabilityMethods];
  468. }
  469. // Check if the delegate conforms to |didReceiveRegistrationToken:|
  470. // and display a warning to the developer if not.
  471. // NOTE: Once |didReceiveRegistrationToken:| can be made a required method, this
  472. // check can be removed.
  473. - (void)validateDelegateConformsToTokenAvailabilityMethods {
  474. if (self.delegate &&
  475. ![self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) {
  476. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTokenDelegateMethodsNotImplemented,
  477. @"The object %@ does not respond to "
  478. @"-messaging:didReceiveRegistrationToken:. Please implement "
  479. @"-messaging:didReceiveRegistrationToken: to be provided with an FCM "
  480. @"token.", self.delegate.description);
  481. }
  482. }
  483. - (void)notifyDelegateOfFCMTokenAvailability {
  484. __weak FIRMessaging *weakSelf = self;
  485. if (![NSThread isMainThread]) {
  486. dispatch_async(dispatch_get_main_queue(), ^{
  487. [weakSelf notifyDelegateOfFCMTokenAvailability];
  488. });
  489. return;
  490. }
  491. if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) {
  492. [self.delegate messaging:self didReceiveRegistrationToken:self.defaultFcmToken];
  493. }
  494. }
  495. #pragma mark - Application State Changes
  496. - (void)applicationStateChanged {
  497. if (self.shouldEstablishDirectChannel) {
  498. [self updateAutomaticClientConnection];
  499. }
  500. }
  501. #pragma mark - Direct Channel
  502. - (void)setShouldEstablishDirectChannel:(BOOL)shouldEstablishDirectChannel {
  503. if (_shouldEstablishDirectChannel == shouldEstablishDirectChannel) {
  504. return;
  505. }
  506. _shouldEstablishDirectChannel = shouldEstablishDirectChannel;
  507. [self updateAutomaticClientConnection];
  508. }
  509. - (BOOL)isDirectChannelEstablished {
  510. return self.client.isConnectionActive;
  511. }
  512. - (BOOL)shouldBeConnectedAutomatically {
  513. // We require a token from Instance ID
  514. NSString *token = self.defaultFcmToken;
  515. // Only on foreground connections
  516. UIApplicationState applicationState = [UIApplication sharedApplication].applicationState;
  517. BOOL shouldBeConnected = _shouldEstablishDirectChannel &&
  518. (token.length > 0) &&
  519. applicationState == UIApplicationStateActive;
  520. return shouldBeConnected;
  521. }
  522. - (void)updateAutomaticClientConnection {
  523. if (![NSThread isMainThread]) {
  524. // Call this method from the main thread
  525. dispatch_async(dispatch_get_main_queue(), ^{
  526. [self updateAutomaticClientConnection];
  527. });
  528. return;
  529. }
  530. BOOL shouldBeConnected = [self shouldBeConnectedAutomatically];
  531. if (shouldBeConnected && !self.client.isConnected) {
  532. [self.client connectWithHandler:^(NSError *error) {
  533. if (!error) {
  534. // It means we connected. Fire connection change notification
  535. [self notifyOfDirectChannelConnectionChange];
  536. }
  537. }];
  538. } else if (!shouldBeConnected && self.client.isConnected) {
  539. [self.client disconnect];
  540. [self notifyOfDirectChannelConnectionChange];
  541. }
  542. }
  543. - (void)notifyOfDirectChannelConnectionChange {
  544. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  545. [center postNotificationName:FIRMessagingConnectionStateChangedNotification object:self];
  546. }
  547. #pragma mark - Connect
  548. - (void)connectWithCompletion:(FIRMessagingConnectCompletion)handler {
  549. _FIRMessagingDevAssert([NSThread isMainThread],
  550. @"FIRMessaging connect should be called from main thread only.");
  551. _FIRMessagingDevAssert(self.isClientSetup, @"FIRMessaging client not setup.");
  552. [self.client connectWithHandler:^(NSError *error) {
  553. if (handler) {
  554. handler(error);
  555. }
  556. if (!error) {
  557. // It means we connected. Fire connection change notification
  558. [self notifyOfDirectChannelConnectionChange];
  559. }
  560. }];
  561. }
  562. - (void)disconnect {
  563. _FIRMessagingDevAssert([NSThread isMainThread],
  564. @"FIRMessaging should be called from main thread only.");
  565. if ([self.client isConnected]) {
  566. [self.client disconnect];
  567. [self notifyOfDirectChannelConnectionChange];
  568. }
  569. }
  570. #pragma mark - Topics
  571. + (NSString *)normalizeTopic:(NSString *)topic {
  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 (self.defaultFcmToken.length && topic.length) {
  586. NSString *normalizeTopic = [[self class ] normalizeTopic:topic];
  587. if (normalizeTopic.length) {
  588. [self.pubsub subscribeToTopic:normalizeTopic handler:completion];
  589. } else {
  590. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging009,
  591. @"Cannot parse topic name %@. Will not subscribe.", topic);
  592. }
  593. } else {
  594. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging010,
  595. @"Cannot subscribe to topic: %@ with token: %@", topic,
  596. self.defaultFcmToken);
  597. }
  598. }
  599. - (void)unsubscribeFromTopic:(NSString *)topic {
  600. [self unsubscribeFromTopic:topic completion:nil];
  601. }
  602. - (void)unsubscribeFromTopic:(NSString *)topic
  603. completion:(nullable FIRMessagingTopicOperationCompletion)completion {
  604. if (self.defaultFcmToken.length && topic.length) {
  605. NSString *normalizeTopic = [[self class] normalizeTopic:topic];
  606. if (normalizeTopic.length) {
  607. [self.pubsub unsubscribeFromTopic:normalizeTopic handler:completion];
  608. } else {
  609. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging011,
  610. @"Cannot parse topic name %@. Will not unsubscribe.", topic);
  611. }
  612. } else {
  613. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging012,
  614. @"Cannot unsubscribe to topic: %@ with token: %@", topic,
  615. self.defaultFcmToken);
  616. }
  617. }
  618. #pragma mark - Send
  619. - (void)sendMessage:(NSDictionary *)message
  620. to:(NSString *)to
  621. withMessageID:(NSString *)messageID
  622. timeToLive:(int64_t)ttl {
  623. _FIRMessagingDevAssert([to length] != 0, @"Invalid receiver id for FIRMessaging-message");
  624. NSMutableDictionary *fcmMessage = [[self class] createFIRMessagingMessageWithMessage:message
  625. to:to
  626. withID:messageID
  627. timeToLive:ttl
  628. delay:0];
  629. FIRMessagingLoggerInfo(kFIRMessagingMessageCodeMessaging013, @"Sending message: %@ with id: %@",
  630. message, messageID);
  631. [self.dataMessageManager sendDataMessageStanza:fcmMessage];
  632. }
  633. + (NSMutableDictionary *)createFIRMessagingMessageWithMessage:(NSDictionary *)message
  634. to:(NSString *)to
  635. withID:(NSString *)msgID
  636. timeToLive:(int64_t)ttl
  637. delay:(int)delay {
  638. NSMutableDictionary *fcmMessage = [NSMutableDictionary dictionary];
  639. fcmMessage[kFIRMessagingSendTo] = [to copy];
  640. fcmMessage[kFIRMessagingSendMessageID] = msgID ? [msgID copy] : @"";
  641. fcmMessage[kFIRMessagingSendTTL] = @(ttl);
  642. fcmMessage[kFIRMessagingSendDelay] = @(delay);
  643. fcmMessage[KFIRMessagingSendMessageAppData] =
  644. [NSMutableDictionary dictionaryWithDictionary:message];
  645. return fcmMessage;
  646. }
  647. #pragma mark - IID dependencies
  648. + (NSString *)FIRMessagingSDKVersion {
  649. return FIRMessagingCurrentLibraryVersion();
  650. }
  651. + (NSString *)FIRMessagingSDKCurrentLocale {
  652. return [self currentLocale];
  653. }
  654. #pragma mark - FIRMessagingReceiverDelegate
  655. - (void)receiver:(FIRMessagingReceiver *)receiver
  656. receivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
  657. if ([self.delegate respondsToSelector:@selector(messaging:didReceiveMessage:)]) {
  658. #pragma clang diagnostic push
  659. #pragma clang diagnostic ignored "-Wunguarded-availability"
  660. [self.delegate messaging:self didReceiveMessage:remoteMessage];
  661. #pragma pop
  662. } else {
  663. // Delegate methods weren't implemented, so messages are being dropped, log a warning
  664. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeRemoteMessageDelegateMethodNotImplemented,
  665. @"FIRMessaging received data-message, but FIRMessagingDelegate's"
  666. @"-messaging:didReceiveMessage: not implemented");
  667. }
  668. }
  669. #pragma mark - FIRReachabilityDelegate
  670. - (void)reachability:(FIRReachabilityChecker *)reachability
  671. statusChanged:(FIRReachabilityStatus)status {
  672. [self onNetworkStatusChanged];
  673. }
  674. #pragma mark - Network
  675. - (void)onNetworkStatusChanged {
  676. if (![self.client isConnected] && [self isNetworkAvailable]) {
  677. if (self.client.shouldStayConnected) {
  678. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging014,
  679. @"Attempting to establish direct channel.");
  680. [self.client retryConnectionImmediately:YES];
  681. }
  682. [self.pubsub scheduleSync:YES];
  683. }
  684. }
  685. - (BOOL)isNetworkAvailable {
  686. FIRReachabilityStatus status = self.reachability.reachabilityStatus;
  687. return (status == kFIRReachabilityViaCellular || status == kFIRReachabilityViaWifi);
  688. }
  689. - (FIRMessagingNetworkStatus)networkType {
  690. FIRReachabilityStatus status = self.reachability.reachabilityStatus;
  691. if (![self isNetworkAvailable]) {
  692. return kFIRMessagingReachabilityNotReachable;
  693. } else if (status == kFIRReachabilityViaCellular) {
  694. return kFIRMessagingReachabilityReachableViaWWAN;
  695. } else {
  696. return kFIRMessagingReachabilityReachableViaWiFi;
  697. }
  698. }
  699. #pragma mark - Notifications
  700. - (void)didReceiveDefaultInstanceIDToken:(NSNotification *)notification {
  701. if (notification.object && ![notification.object isKindOfClass:[NSString class]]) {
  702. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging015,
  703. @"Invalid default FCM token type %@",
  704. NSStringFromClass([notification.object class]));
  705. return;
  706. }
  707. NSString *oldToken = self.defaultFcmToken;
  708. self.defaultFcmToken = [(NSString *)notification.object copy];
  709. if (self.defaultFcmToken && ![self.defaultFcmToken isEqualToString:oldToken]) {
  710. [self notifyDelegateOfFCMTokenAvailability];
  711. }
  712. [self.pubsub scheduleSync:YES];
  713. if (self.shouldEstablishDirectChannel) {
  714. [self updateAutomaticClientConnection];
  715. }
  716. }
  717. - (void)defaultInstanceIDTokenWasRefreshed:(NSNotification *)notification {
  718. // Retrieve the Instance ID default token, and if it is non-nil, post it
  719. NSString *token = self.instanceID.token;
  720. // Sometimes Instance ID doesn't yet have a token, so wait until the default
  721. // token is fetched, and then notify. This ensures that this token should not
  722. // be nil when the developer accesses it.
  723. if (token != nil) {
  724. NSString *oldToken = self.defaultFcmToken;
  725. self.defaultFcmToken = [token copy];
  726. if (self.defaultFcmToken && ![self.defaultFcmToken isEqualToString:oldToken]) {
  727. [self notifyDelegateOfFCMTokenAvailability];
  728. }
  729. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  730. [center postNotificationName:FIRMessagingRegistrationTokenRefreshedNotification object:nil];
  731. }
  732. }
  733. #pragma mark - Application Support Directory
  734. + (BOOL)hasApplicationSupportSubDirectory:(NSString *)subDirectoryName {
  735. NSString *subDirectoryPath = [self pathForApplicationSupportSubDirectory:subDirectoryName];
  736. BOOL isDirectory;
  737. if (![[NSFileManager defaultManager] fileExistsAtPath:subDirectoryPath
  738. isDirectory:&isDirectory]) {
  739. return NO;
  740. } else if (!isDirectory) {
  741. return NO;
  742. }
  743. return YES;
  744. }
  745. + (NSString *)pathForApplicationSupportSubDirectory:(NSString *)subDirectoryName {
  746. NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
  747. NSUserDomainMask, YES);
  748. NSString *applicationSupportDirPath = directoryPaths.lastObject;
  749. NSArray *components = @[applicationSupportDirPath, subDirectoryName];
  750. return [NSString pathWithComponents:components];
  751. }
  752. + (BOOL)createApplicationSupportSubDirectory:(NSString *)subDirectoryName {
  753. NSString *subDirectoryPath = [self pathForApplicationSupportSubDirectory:subDirectoryName];
  754. BOOL hasSubDirectory;
  755. if (![[NSFileManager defaultManager] fileExistsAtPath:subDirectoryPath
  756. isDirectory:&hasSubDirectory]) {
  757. NSError *error;
  758. [[NSFileManager defaultManager] createDirectoryAtPath:subDirectoryPath
  759. withIntermediateDirectories:YES
  760. attributes:nil
  761. error:&error];
  762. if (error) {
  763. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging017,
  764. @"Cannot create directory %@, error: %@", subDirectoryPath, error);
  765. return NO;
  766. }
  767. } else {
  768. if (!hasSubDirectory) {
  769. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging018,
  770. @"Found file instead of directory at %@", subDirectoryPath);
  771. return NO;
  772. }
  773. }
  774. return YES;
  775. }
  776. #pragma mark - Locales
  777. + (NSString *)currentLocale {
  778. NSArray *locales = [self firebaseLocales];
  779. NSArray *preferredLocalizations =
  780. [NSBundle preferredLocalizationsFromArray:locales
  781. forPreferences:[NSLocale preferredLanguages]];
  782. NSString *legalDocsLanguage = [preferredLocalizations firstObject];
  783. // Use en as the default language
  784. return legalDocsLanguage ? legalDocsLanguage : @"en";
  785. }
  786. + (NSArray *)firebaseLocales {
  787. NSMutableArray *locales = [NSMutableArray array];
  788. NSDictionary *localesMap = [self firebaselocalesMap];
  789. for (NSString *key in localesMap) {
  790. [locales addObjectsFromArray:localesMap[key]];
  791. }
  792. return locales;
  793. }
  794. + (NSDictionary *)firebaselocalesMap {
  795. return @{
  796. // Albanian
  797. @"sq" : @[ @"sq_AL" ],
  798. // Belarusian
  799. @"be" : @[ @"be_BY" ],
  800. // Bulgarian
  801. @"bg" : @[ @"bg_BG" ],
  802. // Catalan
  803. @"ca" : @[ @"ca", @"ca_ES" ],
  804. // Croatian
  805. @"hr" : @[ @"hr", @"hr_HR" ],
  806. // Czech
  807. @"cs" : @[ @"cs", @"cs_CZ" ],
  808. // Danish
  809. @"da" : @[ @"da", @"da_DK" ],
  810. // Estonian
  811. @"et" : @[ @"et_EE" ],
  812. // Finnish
  813. @"fi" : @[ @"fi", @"fi_FI" ],
  814. // Hebrew
  815. @"he" : @[ @"he", @"iw_IL" ],
  816. // Hindi
  817. @"hi" : @[ @"hi_IN" ],
  818. // Hungarian
  819. @"hu" : @[ @"hu", @"hu_HU" ],
  820. // Icelandic
  821. @"is" : @[ @"is_IS" ],
  822. // Indonesian
  823. @"id" : @[ @"id", @"in_ID", @"id_ID" ],
  824. // Irish
  825. @"ga" : @[ @"ga_IE" ],
  826. // Korean
  827. @"ko" : @[ @"ko", @"ko_KR", @"ko-KR" ],
  828. // Latvian
  829. @"lv" : @[ @"lv_LV" ],
  830. // Lithuanian
  831. @"lt" : @[ @"lt_LT" ],
  832. // Macedonian
  833. @"mk" : @[ @"mk_MK" ],
  834. // Malay
  835. @"ms" : @[ @"ms_MY" ],
  836. // Maltese
  837. @"ms" : @[ @"mt_MT" ],
  838. // Polish
  839. @"pl" : @[ @"pl", @"pl_PL", @"pl-PL" ],
  840. // Romanian
  841. @"ro" : @[ @"ro", @"ro_RO" ],
  842. // Russian
  843. @"ru" : @[ @"ru_RU", @"ru", @"ru_BY", @"ru_KZ", @"ru-RU" ],
  844. // Slovak
  845. @"sk" : @[ @"sk", @"sk_SK" ],
  846. // Slovenian
  847. @"sl" : @[ @"sl_SI" ],
  848. // Swedish
  849. @"sv" : @[ @"sv", @"sv_SE", @"sv-SE" ],
  850. // Turkish
  851. @"tr" : @[ @"tr", @"tr-TR", @"tr_TR" ],
  852. // Ukrainian
  853. @"uk" : @[ @"uk", @"uk_UA" ],
  854. // Vietnamese
  855. @"vi" : @[ @"vi", @"vi_VN" ],
  856. // The following are groups of locales or locales that sub-divide a
  857. // language).
  858. // Arabic
  859. @"ar" : @[
  860. @"ar",
  861. @"ar_DZ",
  862. @"ar_BH",
  863. @"ar_EG",
  864. @"ar_IQ",
  865. @"ar_JO",
  866. @"ar_KW",
  867. @"ar_LB",
  868. @"ar_LY",
  869. @"ar_MA",
  870. @"ar_OM",
  871. @"ar_QA",
  872. @"ar_SA",
  873. @"ar_SD",
  874. @"ar_SY",
  875. @"ar_TN",
  876. @"ar_AE",
  877. @"ar_YE",
  878. @"ar_GB",
  879. @"ar-IQ",
  880. @"ar_US"
  881. ],
  882. // Simplified Chinese
  883. @"zh_Hans" : @[ @"zh_CN", @"zh_SG", @"zh-Hans" ],
  884. // Traditional Chinese
  885. @"zh_Hant" : @[ @"zh_HK", @"zh_TW", @"zh-Hant", @"zh-HK", @"zh-TW" ],
  886. // Dutch
  887. @"nl" : @[ @"nl", @"nl_BE", @"nl_NL", @"nl-NL" ],
  888. // English
  889. @"en" : @[
  890. @"en",
  891. @"en_AU",
  892. @"en_CA",
  893. @"en_IN",
  894. @"en_IE",
  895. @"en_MT",
  896. @"en_NZ",
  897. @"en_PH",
  898. @"en_SG",
  899. @"en_ZA",
  900. @"en_GB",
  901. @"en_US",
  902. @"en_AE",
  903. @"en-AE",
  904. @"en_AS",
  905. @"en-AU",
  906. @"en_BD",
  907. @"en-CA",
  908. @"en_EG",
  909. @"en_ES",
  910. @"en_GB",
  911. @"en-GB",
  912. @"en_HK",
  913. @"en_ID",
  914. @"en-IN",
  915. @"en_NG",
  916. @"en-PH",
  917. @"en_PK",
  918. @"en-SG",
  919. @"en-US"
  920. ],
  921. // French
  922. @"fr" : @[
  923. @"fr",
  924. @"fr_BE",
  925. @"fr_CA",
  926. @"fr_FR",
  927. @"fr_LU",
  928. @"fr_CH",
  929. @"fr-CA",
  930. @"fr-FR",
  931. @"fr_MA"
  932. ],
  933. // German
  934. @"de" : @[ @"de", @"de_AT", @"de_DE", @"de_LU", @"de_CH", @"de-DE" ],
  935. // Greek
  936. @"el" : @[ @"el", @"el_CY", @"el_GR" ],
  937. // Italian
  938. @"it" : @[ @"it", @"it_IT", @"it_CH", @"it-IT" ],
  939. // Japanese
  940. @"ja" : @[ @"ja", @"ja_JP", @"ja_JP_JP", @"ja-JP" ],
  941. // Norwegian
  942. @"no" : @[ @"nb", @"no_NO", @"no_NO_NY", @"nb_NO" ],
  943. // Brazilian Portuguese
  944. @"pt_BR" : @[ @"pt_BR", @"pt-BR" ],
  945. // European Portuguese
  946. @"pt_PT" : @[ @"pt", @"pt_PT", @"pt-PT" ],
  947. // Serbian
  948. @"sr" : @[
  949. @"sr_BA",
  950. @"sr_ME",
  951. @"sr_RS",
  952. @"sr_Latn_BA",
  953. @"sr_Latn_ME",
  954. @"sr_Latn_RS"
  955. ],
  956. // European Spanish
  957. @"es_ES" : @[ @"es", @"es_ES", @"es-ES" ],
  958. // Mexican Spanish
  959. @"es_MX" : @[ @"es-MX", @"es_MX", @"es_US", @"es-US" ],
  960. // Latin American Spanish
  961. @"es_419" : @[
  962. @"es_AR",
  963. @"es_BO",
  964. @"es_CL",
  965. @"es_CO",
  966. @"es_CR",
  967. @"es_DO",
  968. @"es_EC",
  969. @"es_SV",
  970. @"es_GT",
  971. @"es_HN",
  972. @"es_NI",
  973. @"es_PA",
  974. @"es_PY",
  975. @"es_PE",
  976. @"es_PR",
  977. @"es_UY",
  978. @"es_VE",
  979. @"es-AR",
  980. @"es-CL",
  981. @"es-CO"
  982. ],
  983. // Thai
  984. @"th" : @[ @"th", @"th_TH", @"th_TH_TH" ],
  985. };
  986. }
  987. @end