설명 없음

FIRInstanceIDUtilities.m 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright 2019 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. #import "FIRInstanceIDUtilities.h"
  17. #if TARGET_OS_IOS || TARGET_OS_TV
  18. #import <UIKit/UIKit.h>
  19. #endif
  20. #import <sys/utsname.h>
  21. #import <FirebaseCore/FIROptions.h>
  22. #import <GoogleUtilities/GULUserDefaults.h>
  23. #import "FIRInstanceID.h"
  24. #import "FIRInstanceIDConstants.h"
  25. #import "FIRInstanceIDLogger.h"
  26. // Convert the macro to a string
  27. #define STR_EXPAND(x) #x
  28. #define STR(x) STR_EXPAND(x)
  29. static NSString *const kFIRInstanceIDAPNSSandboxPrefix = @"s_";
  30. static NSString *const kFIRInstanceIDAPNSProdPrefix = @"p_";
  31. /// FIRMessaging Class that responds to the FIRMessaging SDK version selector.
  32. /// Verify at runtime if the class exists and implements the required method.
  33. NSString *const kFIRInstanceIDFCMSDKClassString = @"FIRMessaging";
  34. /// FIRMessaging selector that returns the current FIRMessaging library version.
  35. static NSString *const kFIRInstanceIDFCMSDKVersionSelectorString = @"FIRMessagingSDKVersion";
  36. /// FIRMessaging selector that returns the current device locale.
  37. static NSString *const kFIRInstanceIDFCMSDKLocaleSelectorString = @"FIRMessagingSDKCurrentLocale";
  38. NSString *const kFIRInstanceIDUserDefaultsKeyLocale =
  39. @"com.firebase.instanceid.user_defaults.locale"; // locale key stored in GULUserDefaults
  40. /// Static values which will be populated once retrieved using
  41. /// |FIRInstanceIDRetrieveEnvironmentInfoFromFirebaseCore|.
  42. static NSString *operatingSystemVersion;
  43. static NSString *hardwareDeviceModel;
  44. #pragma mark - URL Helpers
  45. NSString *FIRInstanceIDRegisterServer() {
  46. return @"https://fcmtoken.googleapis.com/register";
  47. }
  48. #pragma mark - Time
  49. int64_t FIRInstanceIDCurrentTimestampInSeconds() {
  50. return (int64_t)[[NSDate date] timeIntervalSince1970];
  51. }
  52. int64_t FIRInstanceIDCurrentTimestampInMilliseconds() {
  53. return (int64_t)(FIRInstanceIDCurrentTimestampInSeconds() * 1000.0);
  54. }
  55. #pragma mark - App Info
  56. NSString *FIRInstanceIDCurrentAppVersion() {
  57. NSString *version = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
  58. if (![version length]) {
  59. return @"";
  60. }
  61. return version;
  62. }
  63. NSString *FIRInstanceIDAppIdentifier() {
  64. NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
  65. if (!bundleIdentifier.length) {
  66. FIRInstanceIDLoggerError(kFIRInstanceIDMessageCodeUtilitiesMissingBundleIdentifier,
  67. @"The mainBundle's bundleIdentifier returned '%@'. Bundle identifier "
  68. @"expected to be non-empty.",
  69. bundleIdentifier);
  70. return @"";
  71. }
  72. return bundleIdentifier;
  73. }
  74. NSString *FIRInstanceIDFirebaseAppID() {
  75. return [FIROptions defaultOptions].googleAppID;
  76. }
  77. #pragma mark - Device Info
  78. // Get the device model from Firebase Core's App Environment Util
  79. NSString *FIRInstanceIDDeviceModel() {
  80. static dispatch_once_t once;
  81. dispatch_once(&once, ^{
  82. struct utsname systemInfo;
  83. if (uname(&systemInfo) == 0) {
  84. hardwareDeviceModel = [NSString stringWithUTF8String:systemInfo.machine];
  85. }
  86. });
  87. return hardwareDeviceModel;
  88. }
  89. // Get the system version from Firebase Core's App Environment Util
  90. NSString *FIRInstanceIDOperatingSystemVersion() {
  91. #if TARGET_OS_IOS || TARGET_OS_TV
  92. return [UIDevice currentDevice].systemVersion;
  93. #elif TARGET_OS_OSX
  94. return [NSProcessInfo processInfo].operatingSystemVersionString;
  95. #endif
  96. }
  97. BOOL FIRInstanceIDHasLocaleChanged() {
  98. NSString *lastLocale =
  99. [[GULUserDefaults standardUserDefaults] stringForKey:kFIRInstanceIDUserDefaultsKeyLocale];
  100. NSString *currentLocale = FIRInstanceIDCurrentLocale();
  101. if (lastLocale) {
  102. if ([currentLocale isEqualToString:lastLocale]) {
  103. return NO;
  104. }
  105. }
  106. return YES;
  107. }
  108. #pragma mark - Helpers
  109. BOOL FIRInstanceIDIsValidGCMScope(NSString *scope) {
  110. return [scope compare:kFIRInstanceIDScopeFirebaseMessaging
  111. options:NSCaseInsensitiveSearch] == NSOrderedSame;
  112. }
  113. NSString *FIRInstanceIDStringForAPNSDeviceToken(NSData *deviceToken) {
  114. NSMutableString *APNSToken = [NSMutableString string];
  115. unsigned char *bytes = (unsigned char *)[deviceToken bytes];
  116. for (int i = 0; i < (int)deviceToken.length; i++) {
  117. [APNSToken appendFormat:@"%02x", bytes[i]];
  118. }
  119. return APNSToken;
  120. }
  121. NSString *FIRInstanceIDAPNSTupleStringForTokenAndServerType(NSData *deviceToken, BOOL isSandbox) {
  122. if (deviceToken == nil) {
  123. // A nil deviceToken leads to an invalid tuple string, so return nil.
  124. return nil;
  125. }
  126. NSString *prefix = isSandbox ? kFIRInstanceIDAPNSSandboxPrefix : kFIRInstanceIDAPNSProdPrefix;
  127. NSString *APNSString = FIRInstanceIDStringForAPNSDeviceToken(deviceToken);
  128. NSString *APNSTupleString = [NSString stringWithFormat:@"%@%@", prefix, APNSString];
  129. return APNSTupleString;
  130. }
  131. #pragma mark - GCM Helpers
  132. NSString *FIRInstanceIDCurrentGCMVersion() {
  133. Class versionClass = NSClassFromString(kFIRInstanceIDFCMSDKClassString);
  134. SEL versionSelector = NSSelectorFromString(kFIRInstanceIDFCMSDKVersionSelectorString);
  135. if ([versionClass respondsToSelector:versionSelector]) {
  136. IMP getVersionIMP = [versionClass methodForSelector:versionSelector];
  137. NSString *(*getVersion)(id, SEL) = (void *)getVersionIMP;
  138. return getVersion(versionClass, versionSelector);
  139. }
  140. return nil;
  141. }
  142. NSString *FIRInstanceIDCurrentLocale() {
  143. Class localeClass = NSClassFromString(kFIRInstanceIDFCMSDKClassString);
  144. SEL localeSelector = NSSelectorFromString(kFIRInstanceIDFCMSDKLocaleSelectorString);
  145. if ([localeClass respondsToSelector:localeSelector]) {
  146. IMP getLocaleIMP = [localeClass methodForSelector:localeSelector];
  147. NSString *(*getLocale)(id, SEL) = (void *)getLocaleIMP;
  148. NSString *fcmLocale = getLocale(localeClass, localeSelector);
  149. if (fcmLocale != nil) {
  150. return fcmLocale;
  151. }
  152. }
  153. NSString *systemLanguage = [[NSLocale preferredLanguages] firstObject];
  154. if (systemLanguage != nil) {
  155. return systemLanguage;
  156. }
  157. if (@available(macOS 10.12, iOS 10.0, *)) {
  158. return [NSLocale currentLocale].languageCode;
  159. } else {
  160. return nil;
  161. }
  162. }