FIRMessaging.m 40 KB

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