NativeBridge.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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(addLocalPush:(id)detail){
  76. dispatch_async(dispatch_get_main_queue(), ^{
  77. //设置通知内容
  78. UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
  79. content.title = detail[@"title"];
  80. content.body = detail[@"body"];
  81. NSString *category_id = detail[@"category_id"];
  82. NSString *message_id = detail[@"id"];
  83. NSString *mode = detail[@"mode"];
  84. NSArray *actions;
  85. if ([category_id isEqualToString:@"REMINDER_FS_START_FAST"]||[category_id isEqualToString:@"REMINDER_FS_START_SLEEP"]){
  86. //消息的处理按钮
  87. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground];
  88. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground];
  89. UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
  90. actions = @[action1,action2,action3];
  91. }
  92. else if([category_id isEqualToString:@"REMINDER_FS_END_FAST"]||[category_id isEqualToString:@"REMINDER_FS_END_SLEEP"]){
  93. //消息的处理按钮
  94. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"END_TIMER_NOW" title:@"End timer now with 1-tap" options:UNNotificationActionOptionForeground];
  95. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_END" title:@"Pick an earlier end" options:UNNotificationActionOptionForeground];
  96. UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"Skip" title:@"SKIP" options:UNNotificationActionOptionAuthenticationRequired];
  97. actions = @[action1,action2,action3];
  98. }
  99. UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:category_id actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  100. [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]];
  101. content.categoryIdentifier = category_id;
  102. if ([mode isEqualToString:@"RECURRING"]){
  103. //设置推送的触发机制
  104. NSArray *timeArray = [detail[@"recurring"][@"time"] componentsSeparatedByString:@":"];
  105. NSInteger hour = [[timeArray objectAtIndex:0] integerValue];
  106. NSInteger minute = [[timeArray objectAtIndex:1] integerValue];
  107. NSInteger second = [[timeArray objectAtIndex:2] integerValue];
  108. // 2. 设置触发条件
  109. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  110. dateComponents.hour = hour;
  111. dateComponents.minute = minute;
  112. dateComponents.second = second;
  113. UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
  114. NSString * identifier = message_id;
  115. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  116. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  117. }];
  118. }
  119. else {
  120. NSTimeInterval timestamp = [detail[@"once"] doubleValue]/1000; // 毫秒级时间戳
  121. // 获取当前时间
  122. NSDate *currentDate = [NSDate date];
  123. // 计算当前时间与给定时间戳之间的时间差(秒)
  124. NSTimeInterval timeInterval = timestamp - [currentDate timeIntervalSince1970];
  125. UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO];
  126. NSString * identifier = message_id;
  127. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  128. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  129. }];
  130. }
  131. });
  132. }
  133. //RCT_EXPORT_METHOD(addLocalPush:(NSString *)title
  134. // withBody:(NSString *)body
  135. // withTime:(NSString *)time
  136. // withDetail:(id)detail){
  137. // dispatch_async(dispatch_get_main_queue(), ^{
  138. // //设置通知内容
  139. // UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
  140. // content.title = title;
  141. // content.body = body;
  142. // // content.subtitle = @"推送的子标题";
  143. // // content.badge = [NSNumber numberWithInteger:0];// 红标
  144. // // content.sound = [UNNotificationSound defaultSound];
  145. // //[UNNotificationSound soundNamed:@""]; 自定义推送声音
  146. //
  147. // // //给通知添加本地图片或者视频,写法同下
  148. // // NSString *path = [[NSBundle mainBundle] pathForResource:@"rank2" ofType:@"png"];
  149. // // NSError *error = nil;
  150. // // /*
  151. // // UNNotificationAttachment是指可以包含音频,图像或视频内容。使用本地通知时,可以在通知创建时,将附件加入即可。对于远程通知,则必须实现使用UNNotificationServiceExtension类通知服务扩展。
  152. // // */
  153. // // UNNotificationAttachment *img_attachment = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:nil error:&error];
  154. // // if (error) {
  155. // // NSLog(@"%@", error);
  156. // // }
  157. // // content.attachments = @[img_attachment];//默认只显示第一个
  158. // // content.launchImageName = @"launch2";
  159. //
  160. // //消息的处理按钮
  161. // UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"START_TIMER_NOW" options:UNNotificationActionOptionForeground];
  162. //
  163. // UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"PICK_EARLIER_START" options:UNNotificationActionOptionForeground];
  164. //
  165. // UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"SKIP" options:UNNotificationActionOptionAuthenticationRequired];
  166. //
  167. // UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:@"REMINDER_FS_START_FAST" actions:@[action1,action2,action3] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  168. //
  169. // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]];
  170. // content.categoryIdentifier = @"REMINDER_FS_START_FAST";
  171. //
  172. // //设置推送的触发机制
  173. // UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
  174. //
  175. // //日历形式,
  176. // // NSDateComponents * dateComp = [[NSDateComponents alloc] init];
  177. // // dateComp.weekday = 2;//周一,默认第一天是周日
  178. // // dateComp.month = 12;//月份,您可以在这里设置具体的时分秒
  179. // // dateComp.hour = 10;//小时
  180. // // dateComp.minute = 14;//分钟
  181. // // UNCalendarNotificationTrigger * calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComp repeats:YES];
  182. //
  183. // //地理位置推送.没实际应用过
  184. // // CLLocationCoordinate2D location ;
  185. // // location.latitude = 123;
  186. // // location.longitude = 111;
  187. // //
  188. // // CLCircularRegion * region = [[CLCircularRegion alloc] initWithCenter:location radius:100.0 identifier:@"regionid"];
  189. // //
  190. // // UNLocationNotificationTrigger * locationTrigger = [UNLocationNotificationTrigger triggerWithRegion:region repeats:YES];
  191. //
  192. // NSString * identifier = @"notification_onlytextid";
  193. // UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  194. // [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  195. //
  196. // }];
  197. //
  198. // // // 1. 设置 notification 内容
  199. // // UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  200. // // content.title = title;
  201. // // content.body = body;
  202. // // content.sound = [UNNotificationSound defaultSound];
  203. // //
  204. // // // 按冒号分割字符串为数组
  205. // // NSArray *timeArray = [@"10:51:00" componentsSeparatedByString:@":"];
  206. // //
  207. // // // 从数组中取出时分秒的值
  208. // // NSInteger hour = [[timeArray objectAtIndex:0] integerValue];
  209. // // NSInteger minute = [[timeArray objectAtIndex:1] integerValue];
  210. // // NSInteger second = [[timeArray objectAtIndex:2] integerValue];
  211. // //
  212. // // // 2. 设置触发条件 - 每天 9:00 AM
  213. // // NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  214. // // dateComponents.hour = hour;
  215. // // dateComponents.minute = minute;
  216. // // dateComponents.second = second;
  217. // // UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
  218. // //
  219. // // // 3. 创建 notification request
  220. // // UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"DailyReminder" content:content trigger:trigger];
  221. // //
  222. // // // 4. 添加ations
  223. // // // 创建允许按钮动作
  224. // // UNNotificationAction *allowAction = [UNNotificationAction actionWithIdentifier:@"ALLOW_ACTION" title:@"Allow" options:UNNotificationActionOptionForeground];
  225. // //
  226. // // // 创建拒绝按钮动作
  227. // // UNNotificationAction *denyAction = [UNNotificationAction actionWithIdentifier:@"DENY_ACTION" title:@"Deny" options:UNNotificationActionOptionDestructive];
  228. // //
  229. // // // 创建动作集合
  230. // // UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"NOTIFICATION_CATEGORY" actions:@[allowAction, denyAction] intentIdentifiers:@[@"ALLOW_ACTION",@"DENY_ACTION"] options:UNNotificationCategoryOptionCustomDismissAction];
  231. // //
  232. // // // 5. 添加 notification request 到通知中心
  233. // // UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  234. // // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:category]];
  235. // //
  236. // // [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  237. // // if (error != nil) {
  238. // // NSLog(@"Error adding notification request: %@", error);
  239. // // }
  240. // // }];
  241. // });
  242. //}
  243. @end