AppDelegate.mm 8.4 KB

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