AppDelegate.mm 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 blackColor];
  57. }
  58. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  59. self.window.backgroundColor = [UIColor blackColor];
  60. UIViewController *rootViewController = [self.reactDelegate createRootViewController];
  61. rootViewController.view = rootView;
  62. rootViewController.view.backgroundColor = [UIColor blackColor];
  63. self.window.rootViewController = rootViewController;
  64. [self.window makeKeyAndVisible];
  65. [super application:application didFinishLaunchingWithOptions:launchOptions];
  66. [self registerForPushNotifications];
  67. return YES;
  68. }
  69. - (void)registerForPushNotifications {
  70. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  71. [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
  72. if (granted) {
  73. dispatch_async(dispatch_get_main_queue(), ^{
  74. [[UIApplication sharedApplication] registerForRemoteNotifications];
  75. });
  76. } else {
  77. // 用户拒绝通知或发生错误
  78. }
  79. }];
  80. }
  81. /// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
  82. ///
  83. /// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
  84. /// @note: This requires to be rendering on Fabric (i.e. on the New Architecture).
  85. /// @return: `true` if the `concurrentRoot` feture is enabled. Otherwise, it returns `false`.
  86. - (BOOL)concurrentRootEnabled
  87. {
  88. // Switch this bool to turn on and off the concurrent root
  89. return true;
  90. }
  91. - (NSDictionary *)prepareInitialProps
  92. {
  93. NSMutableDictionary *initProps = [NSMutableDictionary new];
  94. #ifdef RCT_NEW_ARCH_ENABLED
  95. initProps[kRNConcurrentRoot] = @([self concurrentRootEnabled]);
  96. #endif
  97. return initProps;
  98. }
  99. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  100. {
  101. // return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  102. #if DEBUGaaa
  103. return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
  104. #else
  105. return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  106. #endif
  107. }
  108. #if RCT_NEW_ARCH_ENABLED
  109. #pragma mark - RCTCxxBridgeDelegate
  110. - (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:(RCTBridge *)bridge
  111. {
  112. _turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
  113. delegate:self
  114. jsInvoker:bridge.jsCallInvoker];
  115. return RCTAppSetupDefaultJsExecutorFactory(bridge, _turboModuleManager);
  116. }
  117. #pragma mark RCTTurboModuleManagerDelegate
  118. - (Class)getModuleClassFromName:(const char *)name
  119. {
  120. return RCTCoreModulesClassProvider(name);
  121. }
  122. - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
  123. jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
  124. {
  125. return nullptr;
  126. }
  127. - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
  128. initParams:
  129. (const facebook::react::ObjCTurboModule::InitParams &)params
  130. {
  131. return nullptr;
  132. }
  133. - (id<RCTTurboModule>)getModuleInstanceFromClass:(Class)moduleClass
  134. {
  135. return RCTAppSetupDefaultModuleFromClass(moduleClass);
  136. }
  137. #endif
  138. //************************************************JPush start************************************************
  139. //注册 APNS 成功并上报 DeviceToken
  140. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  141. [JPUSHService registerDeviceToken:deviceToken];
  142. }
  143. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
  144. NSLog(@"get notification error");
  145. }
  146. //iOS 7 APNS
  147. - (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  148. // iOS 10 以下 Required
  149. NSLog(@"iOS 7 APNS");
  150. [JPUSHService handleRemoteNotification:userInfo];
  151. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  152. completionHandler(UIBackgroundFetchResultNewData);
  153. }
  154. //iOS 10 前台收到消息
  155. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  156. NSDictionary * userInfo = notification.request.content.userInfo;
  157. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  158. // Apns
  159. NSLog(@"iOS 10 APNS 前台收到消息");
  160. [JPUSHService handleRemoteNotification:userInfo];
  161. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  162. }
  163. else {
  164. // 本地通知 todo
  165. NSLog(@"iOS 10 本地通知 前台收到消息");
  166. [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  167. }
  168. //需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
  169. completionHandler(UNNotificationPresentationOptionAlert);
  170. }
  171. //iOS 10 消息事件回调
  172. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler: (void (^)(void))completionHandler {
  173. NSDictionary * userInfo = response.notification.request.content.userInfo;
  174. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  175. // Apns
  176. NSLog(@"iOS 10 APNS 消息事件回调");
  177. [JPUSHService handleRemoteNotification:userInfo];
  178. // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  179. [[RCTJPushEventQueue sharedInstance]._notificationQueue insertObject:userInfo atIndex:0];
  180. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT object:userInfo];
  181. }
  182. else {
  183. // 本地通知
  184. NSLog(@"iOS 10 本地通知 消息事件回调");
  185. // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  186. [[RCTJPushEventQueue sharedInstance]._localNotificationQueue insertObject:userInfo atIndex:0];
  187. [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_OPENED_EVENT object:userInfo];
  188. }
  189. // 系统要求执行这个方法
  190. completionHandler();
  191. }
  192. //自定义消息
  193. - (void)networkDidReceiveMessage:(NSNotification *)notification {
  194. NSDictionary * userInfo = [notification userInfo];
  195. [[NSNotificationCenter defaultCenter] postNotificationName:J_CUSTOM_NOTIFICATION_EVENT object:userInfo];
  196. }
  197. @end