AppDelegate.m 7.1 KB

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