FIRMessaging.m 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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 (![FIRMessagingPubSub hasTopicsPrefix:topic]) {
  585. topic = [FIRMessagingPubSub addPrefixToTopic:topic];
  586. }
  587. if ([FIRMessagingPubSub isValidTopicWithPrefix:topic]) {
  588. return [topic copy];
  589. }
  590. return nil;
  591. }
  592. - (void)subscribeToTopic:(NSString *)topic {
  593. [self subscribeToTopic:topic completion:nil];
  594. }
  595. - (void)subscribeToTopic:(NSString *)topic
  596. completion:(nullable FIRMessagingTopicOperationCompletion)completion {
  597. if (self.defaultFcmToken.length && topic.length) {
  598. NSString *normalizeTopic = [[self class ] normalizeTopic:topic];
  599. if ([FIRMessagingPubSub hasTopicsPrefix:topic]) {
  600. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTopicFormatIsDeprecated,
  601. @"Format '%@' is deprecated. Only '%@' should be used in "
  602. @"subscribeToTopic.", topic, normalizeTopic);
  603. }
  604. if (normalizeTopic.length) {
  605. [self.pubsub subscribeToTopic:normalizeTopic handler:completion];
  606. } else {
  607. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging009,
  608. @"Cannot parse topic name %@. Will not subscribe.", topic);
  609. }
  610. } else {
  611. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging010,
  612. @"Cannot subscribe to topic: %@ with token: %@", topic,
  613. self.defaultFcmToken);
  614. }
  615. }
  616. - (void)unsubscribeFromTopic:(NSString *)topic {
  617. [self unsubscribeFromTopic:topic completion:nil];
  618. }
  619. - (void)unsubscribeFromTopic:(NSString *)topic
  620. completion:(nullable FIRMessagingTopicOperationCompletion)completion {
  621. if (self.defaultFcmToken.length && topic.length) {
  622. NSString *normalizeTopic = [[self class] normalizeTopic:topic];
  623. if ([FIRMessagingPubSub hasTopicsPrefix:topic]) {
  624. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTopicFormatIsDeprecated,
  625. @"Format '%@' is deprecated. Only '%@' should be used in "
  626. @"unsubscribeFromTopic.", topic, normalizeTopic);
  627. }
  628. if (normalizeTopic.length) {
  629. [self.pubsub unsubscribeFromTopic:normalizeTopic handler:completion];
  630. } else {
  631. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging011,
  632. @"Cannot parse topic name %@. Will not unsubscribe.", topic);
  633. }
  634. } else {
  635. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging012,
  636. @"Cannot unsubscribe to topic: %@ with token: %@", topic,
  637. self.defaultFcmToken);
  638. }
  639. }
  640. #pragma mark - Send
  641. - (void)sendMessage:(NSDictionary *)message
  642. to:(NSString *)to
  643. withMessageID:(NSString *)messageID
  644. timeToLive:(int64_t)ttl {
  645. _FIRMessagingDevAssert([to length] != 0, @"Invalid receiver id for FIRMessaging-message");
  646. NSMutableDictionary *fcmMessage = [[self class] createFIRMessagingMessageWithMessage:message
  647. to:to
  648. withID:messageID
  649. timeToLive:ttl
  650. delay:0];
  651. FIRMessagingLoggerInfo(kFIRMessagingMessageCodeMessaging013, @"Sending message: %@ with id: %@",
  652. message, messageID);
  653. [self.dataMessageManager sendDataMessageStanza:fcmMessage];
  654. }
  655. + (NSMutableDictionary *)createFIRMessagingMessageWithMessage:(NSDictionary *)message
  656. to:(NSString *)to
  657. withID:(NSString *)msgID
  658. timeToLive:(int64_t)ttl
  659. delay:(int)delay {
  660. NSMutableDictionary *fcmMessage = [NSMutableDictionary dictionary];
  661. fcmMessage[kFIRMessagingSendTo] = [to copy];
  662. fcmMessage[kFIRMessagingSendMessageID] = msgID ? [msgID copy] : @"";
  663. fcmMessage[kFIRMessagingSendTTL] = @(ttl);
  664. fcmMessage[kFIRMessagingSendDelay] = @(delay);
  665. fcmMessage[KFIRMessagingSendMessageAppData] =
  666. [NSMutableDictionary dictionaryWithDictionary:message];
  667. return fcmMessage;
  668. }
  669. #pragma mark - IID dependencies
  670. + (NSString *)FIRMessagingSDKVersion {
  671. return FIRMessagingCurrentLibraryVersion();
  672. }
  673. + (NSString *)FIRMessagingSDKCurrentLocale {
  674. return [self currentLocale];
  675. }
  676. #pragma mark - FIRMessagingReceiverDelegate
  677. - (void)receiver:(FIRMessagingReceiver *)receiver
  678. receivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
  679. if ([self.delegate respondsToSelector:@selector(messaging:didReceiveMessage:)]) {
  680. #pragma clang diagnostic push
  681. #pragma clang diagnostic ignored "-Wunguarded-availability"
  682. [self.delegate messaging:self didReceiveMessage:remoteMessage];
  683. #pragma pop
  684. } else {
  685. // Delegate methods weren't implemented, so messages are being dropped, log a warning
  686. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeRemoteMessageDelegateMethodNotImplemented,
  687. @"FIRMessaging received data-message, but FIRMessagingDelegate's"
  688. @"-messaging:didReceiveMessage: not implemented");
  689. }
  690. }
  691. #pragma mark - FIRReachabilityDelegate
  692. - (void)reachability:(FIRReachabilityChecker *)reachability
  693. statusChanged:(FIRReachabilityStatus)status {
  694. [self onNetworkStatusChanged];
  695. }
  696. #pragma mark - Network
  697. - (void)onNetworkStatusChanged {
  698. if (![self.client isConnected] && [self isNetworkAvailable]) {
  699. if (self.client.shouldStayConnected) {
  700. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging014,
  701. @"Attempting to establish direct channel.");
  702. [self.client retryConnectionImmediately:YES];
  703. }
  704. [self.pubsub scheduleSync:YES];
  705. }
  706. }
  707. - (BOOL)isNetworkAvailable {
  708. FIRReachabilityStatus status = self.reachability.reachabilityStatus;
  709. return (status == kFIRReachabilityViaCellular || status == kFIRReachabilityViaWifi);
  710. }
  711. - (FIRMessagingNetworkStatus)networkType {
  712. FIRReachabilityStatus status = self.reachability.reachabilityStatus;
  713. if (![self isNetworkAvailable]) {
  714. return kFIRMessagingReachabilityNotReachable;
  715. } else if (status == kFIRReachabilityViaCellular) {
  716. return kFIRMessagingReachabilityReachableViaWWAN;
  717. } else {
  718. return kFIRMessagingReachabilityReachableViaWiFi;
  719. }
  720. }
  721. #pragma mark - Notifications
  722. - (void)didReceiveDefaultInstanceIDToken:(NSNotification *)notification {
  723. if (notification.object && ![notification.object isKindOfClass:[NSString class]]) {
  724. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging015,
  725. @"Invalid default FCM token type %@",
  726. NSStringFromClass([notification.object class]));
  727. return;
  728. }
  729. NSString *oldToken = self.defaultFcmToken;
  730. self.defaultFcmToken = [(NSString *)notification.object copy];
  731. if (self.defaultFcmToken && ![self.defaultFcmToken isEqualToString:oldToken]) {
  732. [self notifyDelegateOfFCMTokenAvailability];
  733. }
  734. [self.pubsub scheduleSync:YES];
  735. if (self.shouldEstablishDirectChannel) {
  736. [self updateAutomaticClientConnection];
  737. }
  738. }
  739. - (void)defaultInstanceIDTokenWasRefreshed:(NSNotification *)notification {
  740. // Retrieve the Instance ID default token, and if it is non-nil, post it
  741. #pragma clang diagnostic push
  742. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  743. NSString *token = self.instanceID.token;
  744. #pragma clang diagnostic pop
  745. // Sometimes Instance ID doesn't yet have a token, so wait until the default
  746. // token is fetched, and then notify. This ensures that this token should not
  747. // be nil when the developer accesses it.
  748. if (token != nil) {
  749. NSString *oldToken = self.defaultFcmToken;
  750. self.defaultFcmToken = [token copy];
  751. if (self.defaultFcmToken && ![self.defaultFcmToken isEqualToString:oldToken]) {
  752. [self notifyDelegateOfFCMTokenAvailability];
  753. }
  754. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  755. [center postNotificationName:FIRMessagingRegistrationTokenRefreshedNotification object:nil];
  756. }
  757. }
  758. #pragma mark - Application Support Directory
  759. + (BOOL)hasApplicationSupportSubDirectory:(NSString *)subDirectoryName {
  760. NSString *subDirectoryPath = [self pathForApplicationSupportSubDirectory:subDirectoryName];
  761. BOOL isDirectory;
  762. if (![[NSFileManager defaultManager] fileExistsAtPath:subDirectoryPath
  763. isDirectory:&isDirectory]) {
  764. return NO;
  765. } else if (!isDirectory) {
  766. return NO;
  767. }
  768. return YES;
  769. }
  770. + (NSString *)pathForApplicationSupportSubDirectory:(NSString *)subDirectoryName {
  771. NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
  772. NSUserDomainMask, YES);
  773. NSString *applicationSupportDirPath = directoryPaths.lastObject;
  774. NSArray *components = @[applicationSupportDirPath, subDirectoryName];
  775. return [NSString pathWithComponents:components];
  776. }
  777. + (BOOL)createApplicationSupportSubDirectory:(NSString *)subDirectoryName {
  778. NSString *subDirectoryPath = [self pathForApplicationSupportSubDirectory:subDirectoryName];
  779. BOOL hasSubDirectory;
  780. if (![[NSFileManager defaultManager] fileExistsAtPath:subDirectoryPath
  781. isDirectory:&hasSubDirectory]) {
  782. NSError *error;
  783. [[NSFileManager defaultManager] createDirectoryAtPath:subDirectoryPath
  784. withIntermediateDirectories:YES
  785. attributes:nil
  786. error:&error];
  787. if (error) {
  788. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging017,
  789. @"Cannot create directory %@, error: %@", subDirectoryPath, error);
  790. return NO;
  791. }
  792. } else {
  793. if (!hasSubDirectory) {
  794. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging018,
  795. @"Found file instead of directory at %@", subDirectoryPath);
  796. return NO;
  797. }
  798. }
  799. return YES;
  800. }
  801. #pragma mark - Locales
  802. + (NSString *)currentLocale {
  803. NSArray *locales = [self firebaseLocales];
  804. NSArray *preferredLocalizations =
  805. [NSBundle preferredLocalizationsFromArray:locales
  806. forPreferences:[NSLocale preferredLanguages]];
  807. NSString *legalDocsLanguage = [preferredLocalizations firstObject];
  808. // Use en as the default language
  809. return legalDocsLanguage ? legalDocsLanguage : @"en";
  810. }
  811. + (NSArray *)firebaseLocales {
  812. NSMutableArray *locales = [NSMutableArray array];
  813. NSDictionary *localesMap = [self firebaselocalesMap];
  814. for (NSString *key in localesMap) {
  815. [locales addObjectsFromArray:localesMap[key]];
  816. }
  817. return locales;
  818. }
  819. + (NSDictionary *)firebaselocalesMap {
  820. return @{
  821. // Albanian
  822. @"sq" : @[ @"sq_AL" ],
  823. // Belarusian
  824. @"be" : @[ @"be_BY" ],
  825. // Bulgarian
  826. @"bg" : @[ @"bg_BG" ],
  827. // Catalan
  828. @"ca" : @[ @"ca", @"ca_ES" ],
  829. // Croatian
  830. @"hr" : @[ @"hr", @"hr_HR" ],
  831. // Czech
  832. @"cs" : @[ @"cs", @"cs_CZ" ],
  833. // Danish
  834. @"da" : @[ @"da", @"da_DK" ],
  835. // Estonian
  836. @"et" : @[ @"et_EE" ],
  837. // Finnish
  838. @"fi" : @[ @"fi", @"fi_FI" ],
  839. // Hebrew
  840. @"he" : @[ @"he", @"iw_IL" ],
  841. // Hindi
  842. @"hi" : @[ @"hi_IN" ],
  843. // Hungarian
  844. @"hu" : @[ @"hu", @"hu_HU" ],
  845. // Icelandic
  846. @"is" : @[ @"is_IS" ],
  847. // Indonesian
  848. @"id" : @[ @"id", @"in_ID", @"id_ID" ],
  849. // Irish
  850. @"ga" : @[ @"ga_IE" ],
  851. // Korean
  852. @"ko" : @[ @"ko", @"ko_KR", @"ko-KR" ],
  853. // Latvian
  854. @"lv" : @[ @"lv_LV" ],
  855. // Lithuanian
  856. @"lt" : @[ @"lt_LT" ],
  857. // Macedonian
  858. @"mk" : @[ @"mk_MK" ],
  859. // Malay
  860. @"ms" : @[ @"ms_MY" ],
  861. // Maltese
  862. @"ms" : @[ @"mt_MT" ],
  863. // Polish
  864. @"pl" : @[ @"pl", @"pl_PL", @"pl-PL" ],
  865. // Romanian
  866. @"ro" : @[ @"ro", @"ro_RO" ],
  867. // Russian
  868. @"ru" : @[ @"ru_RU", @"ru", @"ru_BY", @"ru_KZ", @"ru-RU" ],
  869. // Slovak
  870. @"sk" : @[ @"sk", @"sk_SK" ],
  871. // Slovenian
  872. @"sl" : @[ @"sl_SI" ],
  873. // Swedish
  874. @"sv" : @[ @"sv", @"sv_SE", @"sv-SE" ],
  875. // Turkish
  876. @"tr" : @[ @"tr", @"tr-TR", @"tr_TR" ],
  877. // Ukrainian
  878. @"uk" : @[ @"uk", @"uk_UA" ],
  879. // Vietnamese
  880. @"vi" : @[ @"vi", @"vi_VN" ],
  881. // The following are groups of locales or locales that sub-divide a
  882. // language).
  883. // Arabic
  884. @"ar" : @[
  885. @"ar",
  886. @"ar_DZ",
  887. @"ar_BH",
  888. @"ar_EG",
  889. @"ar_IQ",
  890. @"ar_JO",
  891. @"ar_KW",
  892. @"ar_LB",
  893. @"ar_LY",
  894. @"ar_MA",
  895. @"ar_OM",
  896. @"ar_QA",
  897. @"ar_SA",
  898. @"ar_SD",
  899. @"ar_SY",
  900. @"ar_TN",
  901. @"ar_AE",
  902. @"ar_YE",
  903. @"ar_GB",
  904. @"ar-IQ",
  905. @"ar_US"
  906. ],
  907. // Simplified Chinese
  908. @"zh_Hans" : @[ @"zh_CN", @"zh_SG", @"zh-Hans" ],
  909. // Traditional Chinese
  910. @"zh_Hant" : @[ @"zh_HK", @"zh_TW", @"zh-Hant", @"zh-HK", @"zh-TW" ],
  911. // Dutch
  912. @"nl" : @[ @"nl", @"nl_BE", @"nl_NL", @"nl-NL" ],
  913. // English
  914. @"en" : @[
  915. @"en",
  916. @"en_AU",
  917. @"en_CA",
  918. @"en_IN",
  919. @"en_IE",
  920. @"en_MT",
  921. @"en_NZ",
  922. @"en_PH",
  923. @"en_SG",
  924. @"en_ZA",
  925. @"en_GB",
  926. @"en_US",
  927. @"en_AE",
  928. @"en-AE",
  929. @"en_AS",
  930. @"en-AU",
  931. @"en_BD",
  932. @"en-CA",
  933. @"en_EG",
  934. @"en_ES",
  935. @"en_GB",
  936. @"en-GB",
  937. @"en_HK",
  938. @"en_ID",
  939. @"en-IN",
  940. @"en_NG",
  941. @"en-PH",
  942. @"en_PK",
  943. @"en-SG",
  944. @"en-US"
  945. ],
  946. // French
  947. @"fr" : @[
  948. @"fr",
  949. @"fr_BE",
  950. @"fr_CA",
  951. @"fr_FR",
  952. @"fr_LU",
  953. @"fr_CH",
  954. @"fr-CA",
  955. @"fr-FR",
  956. @"fr_MA"
  957. ],
  958. // German
  959. @"de" : @[ @"de", @"de_AT", @"de_DE", @"de_LU", @"de_CH", @"de-DE" ],
  960. // Greek
  961. @"el" : @[ @"el", @"el_CY", @"el_GR" ],
  962. // Italian
  963. @"it" : @[ @"it", @"it_IT", @"it_CH", @"it-IT" ],
  964. // Japanese
  965. @"ja" : @[ @"ja", @"ja_JP", @"ja_JP_JP", @"ja-JP" ],
  966. // Norwegian
  967. @"no" : @[ @"nb", @"no_NO", @"no_NO_NY", @"nb_NO" ],
  968. // Brazilian Portuguese
  969. @"pt_BR" : @[ @"pt_BR", @"pt-BR" ],
  970. // European Portuguese
  971. @"pt_PT" : @[ @"pt", @"pt_PT", @"pt-PT" ],
  972. // Serbian
  973. @"sr" : @[
  974. @"sr_BA",
  975. @"sr_ME",
  976. @"sr_RS",
  977. @"sr_Latn_BA",
  978. @"sr_Latn_ME",
  979. @"sr_Latn_RS"
  980. ],
  981. // European Spanish
  982. @"es_ES" : @[ @"es", @"es_ES", @"es-ES" ],
  983. // Mexican Spanish
  984. @"es_MX" : @[ @"es-MX", @"es_MX", @"es_US", @"es-US" ],
  985. // Latin American Spanish
  986. @"es_419" : @[
  987. @"es_AR",
  988. @"es_BO",
  989. @"es_CL",
  990. @"es_CO",
  991. @"es_CR",
  992. @"es_DO",
  993. @"es_EC",
  994. @"es_SV",
  995. @"es_GT",
  996. @"es_HN",
  997. @"es_NI",
  998. @"es_PA",
  999. @"es_PY",
  1000. @"es_PE",
  1001. @"es_PR",
  1002. @"es_UY",
  1003. @"es_VE",
  1004. @"es-AR",
  1005. @"es-CL",
  1006. @"es-CO"
  1007. ],
  1008. // Thai
  1009. @"th" : @[ @"th", @"th_TH", @"th_TH_TH" ],
  1010. };
  1011. }
  1012. @end