AppDelegate.mm 8.8 KB

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