FIRMessaging.m 40 KB

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