FIRMessaging.m 43 KB

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