AppDelegate.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  67. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  68. NSDate *date1 = [dateFormatter dateFromString:@"2024-05-30 22:00:00"];
  69. [self scheduleCalendarNotificationWithTitle:@"你好" body:@"推送" date:date1 repeats:YES identifier:@"push1"];
  70. [self demo];
  71. // [self registerForPushNotifications];
  72. return YES;
  73. }
  74. - (void)registerForPushNotifications {
  75. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  76. [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
  77. if (granted) {
  78. dispatch_async(dispatch_get_main_queue(), ^{
  79. [[UIApplication sharedApplication] registerForRemoteNotifications];
  80. });
  81. } else {
  82. // 用户拒绝通知或发生错误
  83. }
  84. }];
  85. }
  86. /// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
  87. ///
  88. /// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
  89. /// @note: This requires to be rendering on Fabric (i.e. on the New Architecture).
  90. /// @return: `true` if the `concurrentRoot` feture is enabled. Otherwise, it returns `false`.
  91. - (BOOL)concurrentRootEnabled
  92. {
  93. // Switch this bool to turn on and off the concurrent root
  94. return true;
  95. }
  96. - (NSDictionary *)prepareInitialProps
  97. {
  98. NSMutableDictionary *initProps = [NSMutableDictionary new];
  99. #ifdef RCT_NEW_ARCH_ENABLED
  100. initProps[kRNConcurrentRoot] = @([self concurrentRootEnabled]);
  101. #endif
  102. return initProps;
  103. }
  104. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  105. {
  106. // return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  107. #if DEBUG
  108. return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
  109. #else
  110. return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  111. #endif
  112. }
  113. #if RCT_NEW_ARCH_ENABLED
  114. #pragma mark - RCTCxxBridgeDelegate
  115. - (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:(RCTBridge *)bridge
  116. {
  117. _turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
  118. delegate:self
  119. jsInvoker:bridge.jsCallInvoker];
  120. return RCTAppSetupDefaultJsExecutorFactory(bridge, _turboModuleManager);
  121. }
  122. #pragma mark RCTTurboModuleManagerDelegate
  123. - (Class)getModuleClassFromName:(const char *)name
  124. {
  125. return RCTCoreModulesClassProvider(name);
  126. }
  127. - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
  128. jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
  129. {
  130. return nullptr;
  131. }
  132. - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
  133. initParams:
  134. (const facebook::react::ObjCTurboModule::InitParams &)params
  135. {
  136. return nullptr;
  137. }
  138. - (id<RCTTurboModule>)getModuleInstanceFromClass:(Class)moduleClass
  139. {
  140. return RCTAppSetupDefaultModuleFromClass(moduleClass);
  141. }
  142. #endif
  143. //************************************************JPush start************************************************
  144. //注册 APNS 成功并上报 DeviceToken
  145. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  146. [JPUSHService registerDeviceToken:deviceToken];
  147. }
  148. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
  149. NSLog(@"get notification error");
  150. }
  151. //iOS 7 APNS
  152. - (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  153. // iOS 10 以下 Required
  154. NSLog(@"iOS 7 APNS");
  155. [JPUSHService handleRemoteNotification:userInfo];
  156. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  157. completionHandler(UIBackgroundFetchResultNewData);
  158. }
  159. //iOS 10 前台收到消息
  160. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  161. NSDictionary * userInfo = notification.request.content.userInfo;
  162. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  163. // Apns
  164. NSLog(@"iOS 10 APNS 前台收到消息");
  165. [JPUSHService handleRemoteNotification:userInfo];
  166. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  167. }
  168. else {
  169. // 本地通知 todo
  170. NSLog(@"iOS 10 本地通知 前台收到消息");
  171. [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  172. }
  173. //需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
  174. completionHandler(UNNotificationPresentationOptionAlert);
  175. }
  176. //iOS 10 消息事件回调
  177. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler: (void (^)(void))completionHandler {
  178. NSDictionary * userInfo = response.notification.request.content.userInfo;
  179. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  180. // Apns
  181. NSLog(@"iOS 10 APNS 消息事件回调");
  182. [JPUSHService handleRemoteNotification:userInfo];
  183. // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  184. [[RCTJPushEventQueue sharedInstance]._notificationQueue insertObject:userInfo atIndex:0];
  185. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT object:userInfo];
  186. }
  187. else {
  188. // 本地通知
  189. NSLog(@"iOS 10 本地通知 消息事件回调");
  190. // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  191. [[RCTJPushEventQueue sharedInstance]._localNotificationQueue insertObject:userInfo atIndex:0];
  192. [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_OPENED_EVENT object:userInfo];
  193. }
  194. // 系统要求执行这个方法
  195. completionHandler();
  196. }
  197. //自定义消息
  198. - (void)networkDidReceiveMessage:(NSNotification *)notification {
  199. NSDictionary * userInfo = [notification userInfo];
  200. [[NSNotificationCenter defaultCenter] postNotificationName:J_CUSTOM_NOTIFICATION_EVENT object:userInfo];
  201. }
  202. - (void)demo{
  203. // 1. 设置 notification 内容
  204. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  205. content.title = @"Daily Reminder";
  206. content.body = @"It's 9:00 AM, time to start your day!";
  207. content.sound = [UNNotificationSound defaultSound];
  208. // 2. 设置触发条件 - 每天 9:00 AM
  209. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  210. dateComponents.hour = 9;
  211. dateComponents.minute = 0;
  212. UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
  213. // 3. 创建 notification request
  214. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"DailyReminder"
  215. content:content
  216. trigger:trigger];
  217. // 4. 添加 notification request 到通知中心
  218. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  219. [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  220. if (error != nil) {
  221. NSLog(@"Error adding notification request: %@", error);
  222. }
  223. }];
  224. }
  225. - (void)scheduleCalendarNotificationWithTitle:(NSString *)title body:(NSString *)body date:(NSDate *)date repeats:(BOOL)repeats identifier:(NSString *)identifier {
  226. NSLog(@"%s", __FUNCTION__);
  227. UNMutableNotificationContent *content = [UNMutableNotificationContent new];
  228. content.title = title;
  229. content.body = body;
  230. NSCalendar *calendar = NSCalendar.currentCalendar;
  231. NSDateComponents *components = [calendar components:(NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
  232. UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:repeats];
  233. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  234. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  235. if (error) {
  236. NSLog(@"%@", error);
  237. }
  238. }];
  239. }
  240. @end