AppDelegate.mm 5.9 KB

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