AppDelegate.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #import "AppDelegate.h"
  2. #import <React/RCTBridge.h>
  3. #import <React/RCTBundleURLProvider.h>
  4. #import <React/RCTRootView.h>
  5. #import <GoogleMaps/GoogleMaps.h>
  6. #import <UserNotifications/UserNotifications.h>
  7. #import <RNCPushNotificationIOS.h>
  8. #import <CodePush/CodePush.h>
  9. #ifdef FB_SONARKIT_ENABLED
  10. #import <FlipperKit/FlipperClient.h>
  11. #import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h>
  12. #import <FlipperKitUserDefaultsPlugin/FKUserDefaultsPlugin.h>
  13. #import <FlipperKitNetworkPlugin/FlipperKitNetworkPlugin.h>
  14. #import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h>
  15. #import <FlipperKitReactPlugin/FlipperKitReactPlugin.h>
  16. static void InitializeFlipper(UIApplication *application) {
  17. FlipperClient *client = [FlipperClient sharedClient];
  18. SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults];
  19. [client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application withDescriptorMapper:layoutDescriptorMapper]];
  20. [client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]];
  21. [client addPlugin:[FlipperKitReactPlugin new]];
  22. [client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]];
  23. [client start];
  24. }
  25. #endif
  26. @import Firebase;
  27. @interface AppDelegate () <FIRMessagingDelegate>
  28. @end
  29. @implementation AppDelegate
  30. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  31. {
  32. #ifdef FB_SONARKIT_ENABLED
  33. InitializeFlipper(application);
  34. #endif
  35. RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  36. RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
  37. moduleName:@"JuicePlus"
  38. initialProperties:nil];
  39. if (@available(iOS 13.0, *)) {
  40. rootView.backgroundColor = [UIColor systemBackgroundColor];
  41. } else {
  42. rootView.backgroundColor = [UIColor whiteColor];
  43. }
  44. //Firebase推送配置
  45. [FIRApp configure];
  46. [FIRMessaging messaging].delegate = self;
  47. //谷歌地图配置
  48. [GMSServices provideAPIKey:@"AIzaSyAVzs860l2Iuu1zG80IT1Zu4w7OvbVmJ4g"]; // add this line using the api key obtained from Google Console
  49. //远程推送配置
  50. // Define UNUserNotificationCenter
  51. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  52. center.delegate = self;
  53. UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
  54. UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
  55. [[UNUserNotificationCenter currentNotificationCenter]
  56. requestAuthorizationWithOptions:authOptions
  57. completionHandler:^(BOOL granted, NSError * _Nullable error) {
  58. // ...
  59. }];
  60. [application registerForRemoteNotifications];
  61. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  62. UIViewController *rootViewController = [UIViewController new];
  63. rootViewController.view = rootView;
  64. self.window.rootViewController = rootViewController;
  65. [self.window makeKeyAndVisible];
  66. return YES;
  67. }
  68. //Called when a notification is delivered to a foreground app.
  69. -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
  70. {
  71. completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
  72. }
  73. - (void) messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
  74. //NSLog(@"FCM registration token: %@", fcmToken);
  75. // Notify about received token.
  76. NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
  77. [[NSNotificationCenter defaultCenter] postNotificationName:
  78. @"FCMToken" object:nil userInfo:dataDict];
  79. // TODO: 如有必要,将令牌发送到应用程序服务器。
  80. //注意:每次应用程序启动时以及每当生成新令牌时,都会触发此回调。
  81. //NSData *deviceToken = [fcmToken dataUsingEncoding:NSUTF8StringEncoding];
  82. //或者,您也可以监听名为 kFIRMessagingRegistrationTokenRefreshNotification 的 NSNotification,而不提供委托方法。该令牌属性始终具有当前令牌值。
  83. //[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
  84. }
  85. //- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  86. // NSLog(@"error: %@", error);
  87. //}
  88. //Required for the register event.
  89. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  90. {
  91. // NSMutableString *hexString = [NSMutableString string];
  92. // NSUInteger deviceTokenLength = deviceToken.length;
  93. // const unsigned char *bytes = deviceToken.bytes;
  94. // for (NSUInteger i = 0; i < deviceTokenLength; i++) {
  95. // [hexString appendFormat:@"%02x", bytes[i]];
  96. // }
  97. // NSLog(@"APNs registration token: %@", hexString);
  98. if ([FIRMessaging messaging].FCMToken != nil) {
  99. [FIRMessaging messaging].APNSToken = deviceToken;
  100. //NSLog(@"FIRMessaging token: %@", [FIRMessaging messaging].FCMToken);
  101. //将FCMToken发送到RN端
  102. [[NSNotificationCenter defaultCenter] postNotificationName:
  103. @"RemoteNotificationsRegistered" object:self
  104. userInfo:@{@"deviceToken" : [[FIRMessaging messaging].FCMToken copy]}];
  105. //[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
  106. }
  107. }
  108. // Required for the notification event. You must call the completion handler after handling the remote notification.
  109. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  110. fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
  111. {
  112. [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
  113. }
  114. // Required for the registrationError event.
  115. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
  116. {
  117. NSLog(@"didFailToRegisterForRemoteNotificationsWithError: %@", error);
  118. [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
  119. }
  120. // Required for localNotification event
  121. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  122. didReceiveNotificationResponse:(UNNotificationResponse *)response
  123. withCompletionHandler:(void (^)(void))completionHandler
  124. {
  125. [RNCPushNotificationIOS didReceiveNotificationResponse:response];
  126. completionHandler();
  127. }
  128. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  129. {
  130. #if DEBUG
  131. return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  132. #else
  133. //return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  134. return [CodePush bundleURL];
  135. #endif
  136. }
  137. @end