NativeBridge.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // NativeBridge.m
  3. // hola
  4. //
  5. // Created by Leon on 2024/4/28.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "NativeBridge.h"
  9. #import "AppDelegate.h"
  10. #import <CoreLocation/CoreLocation.h>
  11. @interface NativeBridge()
  12. @end
  13. @implementation NativeBridge
  14. @synthesize bridge = _bridge;
  15. RCT_EXPORT_MODULE()
  16. - (instancetype)init {
  17. self = [super init];
  18. if (self) {
  19. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  20. appDelegate.nativeBridge = self;
  21. // 在这里进行初始化操作
  22. }
  23. return self;
  24. }
  25. -(NSArray<NSString *>*)supportedEvents
  26. {
  27. return @[@"receive"];
  28. //EventReminder 是监听的标识,类似 iOS 发通知 需要一个标识去识别,通过这个标识发送通知调用 RN方法
  29. }
  30. RCT_EXPORT_METHOD(rnPageLoaded){
  31. dispatch_async(dispatch_get_main_queue(), ^{
  32. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  33. appDelegate.rnLoaded = YES;
  34. });
  35. }
  36. RCT_EXPORT_METHOD(getLocationAuthStatus:(RCTResponseSenderBlock)callback){
  37. dispatch_async(dispatch_get_main_queue(), ^{
  38. BOOL isLocation = [CLLocationManager locationServicesEnabled];
  39. CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
  40. switch (status) {
  41. case kCLAuthorizationStatusNotDetermined:
  42. callback(@[
  43. @{
  44. @"location_services_enabled":isLocation?@"true":@"false",
  45. @"authorization_status":@"not_determined"
  46. }
  47. ]);
  48. break;
  49. case kCLAuthorizationStatusDenied:
  50. callback(@[
  51. @{
  52. @"location_services_enabled":isLocation?@"true":@"false",
  53. @"authorization_status":@"denied"
  54. }
  55. ]);
  56. break;
  57. case kCLAuthorizationStatusAuthorizedAlways:
  58. callback(@[
  59. @{
  60. @"location_services_enabled":isLocation?@"true":@"false",
  61. @"authorization_status":@"always"
  62. }
  63. ]);
  64. break;
  65. case kCLAuthorizationStatusAuthorizedWhenInUse:
  66. callback(@[
  67. @{
  68. @"location_services_enabled":isLocation?@"true":@"false",
  69. @"authorization_status":@"when_in_use"
  70. }
  71. ]);
  72. break;
  73. default:
  74. callback(@[
  75. @{
  76. @"location_services_enabled":isLocation?@"true":@"false",
  77. @"authorization_status":@"other"
  78. }
  79. ]);
  80. break;
  81. }
  82. });
  83. }
  84. RCT_EXPORT_METHOD(getNotificationAuthStatus){
  85. dispatch_async(dispatch_get_main_queue(), ^{
  86. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  87. [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  88. UNAuthorizationStatus status = settings.authorizationStatus;
  89. switch (status) {
  90. case UNAuthorizationStatusAuthorized:
  91. NSLog(@"用户已授权通知权限");
  92. [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"authorized"];
  93. break;
  94. case UNAuthorizationStatusDenied:
  95. NSLog(@"用户已拒绝通知权限");
  96. [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"denied"];
  97. break;
  98. case UNAuthorizationStatusNotDetermined:
  99. NSLog(@"通知权限未确定");
  100. [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"not_determined"];
  101. break;
  102. default:
  103. break;
  104. }
  105. }];
  106. });
  107. }
  108. RCT_EXPORT_METHOD(authNotification){
  109. dispatch_async(dispatch_get_main_queue(), ^{
  110. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  111. [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
  112. if (granted) {
  113. [self.bridge.eventDispatcher sendAppEventWithName:@"operateNotificationResult" body:@"authorized"];
  114. dispatch_async(dispatch_get_main_queue(), ^{
  115. [[UIApplication sharedApplication] registerForRemoteNotifications];
  116. });
  117. } else {
  118. // 用户拒绝通知或发生错误
  119. [self.bridge.eventDispatcher sendAppEventWithName:@"operateNotificationResult" body:@"denied"];
  120. }
  121. }];
  122. });
  123. }
  124. RCT_EXPORT_METHOD(addLocalPush2:(id)detail){
  125. dispatch_async(dispatch_get_main_queue(), ^{
  126. //设置通知内容
  127. UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
  128. content.title = @"a1";
  129. content.body = @"b1";
  130. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW1" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground];
  131. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START2" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground];
  132. NSArray *actions = @[action1,action2];
  133. UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:@"c1" actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  134. // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]];
  135. content.categoryIdentifier = @"c1";
  136. UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
  137. NSString * identifier = @"e1";
  138. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  139. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  140. }];
  141. UNMutableNotificationContent * content2 = [[UNMutableNotificationContent alloc] init];
  142. content2.title = @"a12";
  143. content2.body = @"b12";
  144. NSArray *actions2;
  145. UNNotificationAction *action12 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground];
  146. UNNotificationAction *action22 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground];
  147. UNNotificationAction *action32 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
  148. actions2 = @[action12,action22,action32];
  149. UNNotificationCategory *categroy2 = [UNNotificationCategory categoryWithIdentifier:@"c12" actions:actions2 intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  150. // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy2]];
  151. [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithArray:@[categroy,categroy2]]];
  152. content2.categoryIdentifier = @"c12";
  153. UNTimeIntervalNotificationTrigger * trigger2 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:15 repeats:NO];
  154. NSString * identifier2 = @"e12";
  155. UNNotificationRequest * request2 = [UNNotificationRequest requestWithIdentifier:identifier2 content:content2 trigger:trigger2];
  156. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request2 withCompletionHandler:^(NSError * _Nullable error) {
  157. }];
  158. });
  159. }
  160. RCT_EXPORT_METHOD(addLocalPush:(id)array){
  161. dispatch_async(dispatch_get_main_queue(), ^{
  162. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  163. [center removeAllPendingNotificationRequests];
  164. NSMutableArray *list = [NSMutableArray new];
  165. for (int i=0;i<[array count];i++){
  166. id detail = array[i];
  167. NSString *mode = detail[@"mode"];
  168. if (![mode isEqualToString:@"RECURRING"]){
  169. NSTimeInterval timestamp = [detail[@"once"] doubleValue]/1000; // 毫秒级时间戳
  170. // 获取当前时间
  171. NSDate *currentDate = [NSDate date];
  172. // 计算当前时间与给定时间戳之间的时间差(秒)
  173. NSTimeInterval timeInterval = timestamp - [currentDate timeIntervalSince1970];
  174. if (timeInterval<0){
  175. continue;
  176. }
  177. }
  178. //设置通知内容
  179. UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
  180. content.title = detail[@"title"];
  181. content.body = [NSString stringWithFormat:@"%@\nPress for actions >>",detail[@"body"]];
  182. NSString *category_id = detail[@"category_id"];
  183. NSString *message_id = detail[@"id"];
  184. NSArray *actions;
  185. if ([category_id isEqualToString:@"REMINDER_FS_START_FAST"]||[category_id isEqualToString:@"REMINDER_FS_START_SLEEP"]){
  186. //消息的处理按钮
  187. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground];
  188. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground];
  189. UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
  190. actions = @[action1,action2,action3];
  191. }
  192. else if([category_id isEqualToString:@"REMINDER_FS_END_FAST"]||[category_id isEqualToString:@"REMINDER_FS_END_SLEEP"]){
  193. //消息的处理按钮
  194. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"END_TIMER_NOW" title:@"End timer now with 1-tap" options:UNNotificationActionOptionForeground];
  195. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_END" title:@"Pick an earlier end" options:UNNotificationActionOptionForeground];
  196. UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
  197. actions = @[action1,action2,action3];
  198. }
  199. UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:category_id actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  200. [list addObject:categroy];
  201. // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]];
  202. content.categoryIdentifier = category_id;
  203. if ([mode isEqualToString:@"RECURRING"]){
  204. //设置推送的触发机制
  205. NSArray *timeArray = [detail[@"recurring"][@"time"] componentsSeparatedByString:@":"];
  206. NSInteger hour = [[timeArray objectAtIndex:0] integerValue];
  207. NSInteger minute = [[timeArray objectAtIndex:1] integerValue];
  208. NSInteger second = [[timeArray objectAtIndex:2] integerValue];
  209. // 2. 设置触发条件
  210. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  211. dateComponents.hour = hour;
  212. dateComponents.minute = minute;
  213. dateComponents.second = second;
  214. UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
  215. NSString * identifier = message_id;
  216. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  217. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  218. }];
  219. }
  220. else {
  221. NSTimeInterval timestamp = [detail[@"once"] doubleValue]/1000; // 毫秒级时间戳
  222. // 获取当前时间
  223. NSDate *currentDate = [NSDate date];
  224. // 计算当前时间与给定时间戳之间的时间差(秒)
  225. NSTimeInterval timeInterval = timestamp - [currentDate timeIntervalSince1970];
  226. UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO];
  227. NSString * identifier = message_id;
  228. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  229. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  230. }];
  231. }
  232. }
  233. [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithArray:list]];
  234. });
  235. }
  236. RCT_EXPORT_METHOD(addLocalPush3:(id)detail){
  237. dispatch_async(dispatch_get_main_queue(), ^{
  238. //设置通知内容
  239. UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
  240. content.title = detail[@"title"];
  241. content.body = detail[@"body"];
  242. NSString *category_id = detail[@"category_id"];
  243. NSString *message_id = detail[@"id"];
  244. NSString *mode = detail[@"mode"];
  245. NSArray *actions;
  246. if ([category_id isEqualToString:@"REMINDER_FS_START_FAST"]||[category_id isEqualToString:@"REMINDER_FS_START_SLEEP"]){
  247. //消息的处理按钮
  248. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground];
  249. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground];
  250. UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
  251. actions = @[action1,action2,action3];
  252. }
  253. else if([category_id isEqualToString:@"REMINDER_FS_END_FAST"]||[category_id isEqualToString:@"REMINDER_FS_END_SLEEP"]){
  254. //消息的处理按钮
  255. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"END_TIMER_NOW" title:@"End timer now with 1-tap" options:UNNotificationActionOptionForeground];
  256. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_END" title:@"Pick an earlier end" options:UNNotificationActionOptionForeground];
  257. UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
  258. actions = @[action1,action2,action3];
  259. }
  260. UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:category_id actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  261. [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]];
  262. content.categoryIdentifier = category_id;
  263. if ([mode isEqualToString:@"RECURRING"]){
  264. //设置推送的触发机制
  265. NSArray *timeArray = [detail[@"recurring"][@"time"] componentsSeparatedByString:@":"];
  266. NSInteger hour = [[timeArray objectAtIndex:0] integerValue];
  267. NSInteger minute = [[timeArray objectAtIndex:1] integerValue];
  268. NSInteger second = [[timeArray objectAtIndex:2] integerValue];
  269. // 2. 设置触发条件
  270. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  271. dateComponents.hour = hour;
  272. dateComponents.minute = minute;
  273. dateComponents.second = second;
  274. UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
  275. NSString * identifier = message_id;
  276. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  277. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  278. }];
  279. }
  280. else {
  281. NSTimeInterval timestamp = [detail[@"once"] doubleValue]/1000; // 毫秒级时间戳
  282. // 获取当前时间
  283. NSDate *currentDate = [NSDate date];
  284. // 计算当前时间与给定时间戳之间的时间差(秒)
  285. NSTimeInterval timeInterval = timestamp - [currentDate timeIntervalSince1970];
  286. UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO];
  287. NSString * identifier = message_id;
  288. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  289. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  290. }];
  291. }
  292. });
  293. }
  294. @end