AppDelegate.mm 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #import "AppDelegate.h"
  2. #import <React/RCTBridge.h>
  3. #import <React/RCTBundleURLProvider.h>
  4. #import <React/RCTRootView.h>
  5. #import <React/RCTAppSetupUtils.h>
  6. #import <RCTJPushModule.h>
  7. #if RCT_NEW_ARCH_ENABLED
  8. #import <React/CoreModulesPlugins.h>
  9. #import <React/RCTCxxBridgeDelegate.h>
  10. #import <React/RCTFabricSurfaceHostingProxyRootView.h>
  11. #import <React/RCTSurfacePresenter.h>
  12. #import <React/RCTSurfacePresenterBridgeAdapter.h>
  13. #import <ReactCommon/RCTTurboModuleManager.h>
  14. #import <react/config/ReactNativeConfig.h>
  15. static NSString *const kRNConcurrentRoot = @"concurrentRoot";
  16. @interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate> {
  17. RCTTurboModuleManager *_turboModuleManager;
  18. RCTSurfacePresenterBridgeAdapter *_bridgeAdapter;
  19. std::shared_ptr<const facebook::react::ReactNativeConfig> _reactNativeConfig;
  20. facebook::react::ContextContainer::Shared _contextContainer;
  21. }
  22. @end
  23. #endif
  24. @implementation AppDelegate
  25. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  26. {
  27. // APNS
  28. JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  29. if (@available(iOS 12.0, *)) {
  30. entity.types = JPAuthorizationOptionNone; //JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSou//nd|JPAuthorizationOptionProvidesAppNotificationSettings;
  31. }
  32. [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  33. RCTAppSetupPrepareApp(application);
  34. RCTBridge *bridge = [self.reactDelegate createBridgeWithDelegate:self launchOptions:launchOptions];
  35. #if RCT_NEW_ARCH_ENABLED
  36. _contextContainer = std::make_shared<facebook::react::ContextContainer const>();
  37. _reactNativeConfig = std::make_shared<facebook::react::EmptyReactNativeConfig const>();
  38. _contextContainer->insert("ReactNativeConfig", _reactNativeConfig);
  39. _bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:bridge contextContainer:_contextContainer];
  40. bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;
  41. #endif
  42. NSDictionary *initProps = [self prepareInitialProps];
  43. UIView *rootView = [self.reactDelegate createRootViewWithBridge:bridge moduleName:@"taroDemo" initialProperties:initProps];
  44. if (@available(iOS 13.0, *)) {
  45. rootView.backgroundColor = [UIColor systemBackgroundColor];
  46. } else {
  47. rootView.backgroundColor = [UIColor whiteColor];
  48. }
  49. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  50. UIViewController *rootViewController = [self.reactDelegate createRootViewController];
  51. rootViewController.view = rootView;
  52. self.window.rootViewController = rootViewController;
  53. [self.window makeKeyAndVisible];
  54. [super application:application didFinishLaunchingWithOptions:launchOptions];
  55. return YES;
  56. }
  57. /// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
  58. ///
  59. /// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
  60. /// @note: This requires to be rendering on Fabric (i.e. on the New Architecture).
  61. /// @return: `true` if the `concurrentRoot` feture is enabled. Otherwise, it returns `false`.
  62. - (BOOL)concurrentRootEnabled
  63. {
  64. // Switch this bool to turn on and off the concurrent root
  65. return true;
  66. }
  67. - (NSDictionary *)prepareInitialProps
  68. {
  69. NSMutableDictionary *initProps = [NSMutableDictionary new];
  70. #ifdef RCT_NEW_ARCH_ENABLED
  71. initProps[kRNConcurrentRoot] = @([self concurrentRootEnabled]);
  72. #endif
  73. return initProps;
  74. }
  75. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  76. {
  77. #if DEBUG
  78. return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
  79. #else
  80. return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  81. #endif
  82. }
  83. #if RCT_NEW_ARCH_ENABLED
  84. #pragma mark - RCTCxxBridgeDelegate
  85. - (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:(RCTBridge *)bridge
  86. {
  87. _turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
  88. delegate:self
  89. jsInvoker:bridge.jsCallInvoker];
  90. return RCTAppSetupDefaultJsExecutorFactory(bridge, _turboModuleManager);
  91. }
  92. #pragma mark RCTTurboModuleManagerDelegate
  93. - (Class)getModuleClassFromName:(const char *)name
  94. {
  95. return RCTCoreModulesClassProvider(name);
  96. }
  97. - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
  98. jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
  99. {
  100. return nullptr;
  101. }
  102. - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
  103. initParams:
  104. (const facebook::react::ObjCTurboModule::InitParams &)params
  105. {
  106. return nullptr;
  107. }
  108. - (id<RCTTurboModule>)getModuleInstanceFromClass:(Class)moduleClass
  109. {
  110. return RCTAppSetupDefaultModuleFromClass(moduleClass);
  111. }
  112. #endif
  113. //************************************************JPush start************************************************
  114. //注册 APNS 成功并上报 DeviceToken
  115. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  116. [JPUSHService registerDeviceToken:deviceToken];
  117. }
  118. //iOS 7 APNS
  119. - (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  120. // iOS 10 以下 Required
  121. NSLog(@"iOS 7 APNS");
  122. [JPUSHService handleRemoteNotification:userInfo];
  123. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  124. completionHandler(UIBackgroundFetchResultNewData);
  125. }
  126. //iOS 10 前台收到消息
  127. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  128. NSDictionary * userInfo = notification.request.content.userInfo;
  129. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  130. // Apns
  131. NSLog(@"iOS 10 APNS 前台收到消息");
  132. [JPUSHService handleRemoteNotification:userInfo];
  133. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  134. }
  135. else {
  136. // 本地通知 todo
  137. NSLog(@"iOS 10 本地通知 前台收到消息");
  138. [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  139. }
  140. //需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
  141. completionHandler(UNNotificationPresentationOptionAlert);
  142. }
  143. //iOS 10 消息事件回调
  144. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler: (void (^)(void))completionHandler {
  145. NSDictionary * userInfo = response.notification.request.content.userInfo;
  146. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  147. // Apns
  148. NSLog(@"iOS 10 APNS 消息事件回调");
  149. [JPUSHService handleRemoteNotification:userInfo];
  150. // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  151. [[RCTJPushEventQueue sharedInstance]._notificationQueue insertObject:userInfo atIndex:0];
  152. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT object:userInfo];
  153. }
  154. else {
  155. // 本地通知
  156. NSLog(@"iOS 10 本地通知 消息事件回调");
  157. // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  158. [[RCTJPushEventQueue sharedInstance]._localNotificationQueue insertObject:userInfo atIndex:0];
  159. [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_OPENED_EVENT object:userInfo];
  160. }
  161. // 系统要求执行这个方法
  162. completionHandler();
  163. }
  164. //自定义消息
  165. - (void)networkDidReceiveMessage:(NSNotification *)notification {
  166. NSDictionary * userInfo = [notification userInfo];
  167. [[NSNotificationCenter defaultCenter] postNotificationName:J_CUSTOM_NOTIFICATION_EVENT object:userInfo];
  168. }
  169. @end