NativeBridge.m 15 KB

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