AppDelegate.mm 6.4 KB

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