// // NativeBridge.m // hola // // Created by Leon on 2024/4/28. // #import #import "NativeBridge.h" #import "AppDelegate.h" #import @interface NativeBridge() @end @implementation NativeBridge @synthesize bridge = _bridge; RCT_EXPORT_MODULE() - (instancetype)init { self = [super init]; if (self) { AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.nativeBridge = self; // 在这里进行初始化操作 } return self; } -(NSArray*)supportedEvents { return @[@"receive"]; //EventReminder 是监听的标识,类似 iOS 发通知 需要一个标识去识别,通过这个标识发送通知调用 RN方法 } RCT_EXPORT_METHOD(rnPageLoaded){ dispatch_async(dispatch_get_main_queue(), ^{ AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.rnLoaded = YES; }); } RCT_EXPORT_METHOD(getLocationAuthStatus:(RCTResponseSenderBlock)callback){ dispatch_async(dispatch_get_main_queue(), ^{ BOOL isLocation = [CLLocationManager locationServicesEnabled]; CLAuthorizationStatus status = [CLLocationManager authorizationStatus]; switch (status) { case kCLAuthorizationStatusNotDetermined: callback(@[ @{ @"location_services_enabled":isLocation?@"true":@"false", @"authorization_status":@"not_determined" } ]); break; case kCLAuthorizationStatusDenied: callback(@[ @{ @"location_services_enabled":isLocation?@"true":@"false", @"authorization_status":@"denied" } ]); break; case kCLAuthorizationStatusAuthorizedAlways: callback(@[ @{ @"location_services_enabled":isLocation?@"true":@"false", @"authorization_status":@"always" } ]); break; case kCLAuthorizationStatusAuthorizedWhenInUse: callback(@[ @{ @"location_services_enabled":isLocation?@"true":@"false", @"authorization_status":@"when_in_use" } ]); break; default: callback(@[ @{ @"location_services_enabled":isLocation?@"true":@"false", @"authorization_status":@"other" } ]); break; } }); } RCT_EXPORT_METHOD(getNotificationAuthStatus){ dispatch_async(dispatch_get_main_queue(), ^{ UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { UNAuthorizationStatus status = settings.authorizationStatus; switch (status) { case UNAuthorizationStatusAuthorized: NSLog(@"用户已授权通知权限"); [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"authorized"]; break; case UNAuthorizationStatusDenied: NSLog(@"用户已拒绝通知权限"); [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"denied"]; break; case UNAuthorizationStatusNotDetermined: NSLog(@"通知权限未确定"); [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"not_determined"]; break; default: break; } }]; }); } RCT_EXPORT_METHOD(authNotification){ dispatch_async(dispatch_get_main_queue(), ^{ UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { [self.bridge.eventDispatcher sendAppEventWithName:@"operateNotificationResult" body:@"authorized"]; dispatch_async(dispatch_get_main_queue(), ^{ [[UIApplication sharedApplication] registerForRemoteNotifications]; }); } else { // 用户拒绝通知或发生错误 [self.bridge.eventDispatcher sendAppEventWithName:@"operateNotificationResult" body:@"denied"]; } }]; }); } RCT_EXPORT_METHOD(clearNotification){ dispatch_async(dispatch_get_main_queue(), ^{ UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center removeAllPendingNotificationRequests]; }); } RCT_EXPORT_METHOD(addLocalPush:(id)array){ dispatch_async(dispatch_get_main_queue(), ^{ UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center removeAllPendingNotificationRequests]; NSMutableArray *list = [NSMutableArray new]; for (int i=0;i<[array count];i++){ id detail = array[i]; NSString *mode = detail[@"mode"]; if (![mode isEqualToString:@"RECURRING"]){ NSTimeInterval timestamp = [detail[@"once"] doubleValue]/1000; // 毫秒级时间戳 // 获取当前时间 NSDate *currentDate = [NSDate date]; // 计算当前时间与给定时间戳之间的时间差(秒) NSTimeInterval timeInterval = timestamp - [currentDate timeIntervalSince1970]; if (timeInterval<0){ continue; } } //设置通知内容 UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init]; content.title = detail[@"title"]; content.body = [NSString stringWithFormat:@"%@\nPress for actions >>",detail[@"body"]]; content.sound = [UNNotificationSound defaultSound]; content.badge = @([UIApplication sharedApplication].applicationIconBadgeNumber + 1); if (@available(iOS 15.0,*)){ content.interruptionLevel = UNNotificationInterruptionLevelActive; } NSString *category_id = detail[@"category_id"]; NSString *message_id = detail[@"id"]; NSArray *actions; if ([category_id isEqualToString:@"REMINDER_FS_START_FAST"]||[category_id isEqualToString:@"REMINDER_FS_START_SLEEP"]){ //消息的处理按钮 UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground]; UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground]; UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired]; actions = @[action1,action2,action3]; } else if([category_id isEqualToString:@"REMINDER_FS_END_FAST"]||[category_id isEqualToString:@"REMINDER_FS_END_SLEEP"]){ //消息的处理按钮 UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"END_TIMER_NOW" title:@"End timer now with 1-tap" options:UNNotificationActionOptionForeground]; UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_END" title:@"Pick an earlier end" options:UNNotificationActionOptionForeground]; UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired]; actions = @[action1,action2,action3]; } UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:category_id actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; [list addObject:categroy]; // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]]; content.categoryIdentifier = category_id; if ([mode isEqualToString:@"RECURRING"]){ //设置推送的触发机制 NSArray *timeArray = [detail[@"recurring"][@"time"] componentsSeparatedByString:@":"]; NSInteger hour = [[timeArray objectAtIndex:0] integerValue]; NSInteger minute = [[timeArray objectAtIndex:1] integerValue]; NSInteger second = [[timeArray objectAtIndex:2] integerValue]; // 2. 设置触发条件 NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; dateComponents.hour = hour; dateComponents.minute = minute; dateComponents.second = second; UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES]; NSString * identifier = message_id; UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { }]; } else { NSTimeInterval timestamp = [detail[@"once"] doubleValue]/1000; // 毫秒级时间戳 // 获取当前时间 NSDate *currentDate = [NSDate date]; // 计算当前时间与给定时间戳之间的时间差(秒) NSTimeInterval timeInterval = timestamp - [currentDate timeIntervalSince1970]; UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO]; NSString * identifier = message_id; UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { }]; } } [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithArray:list]]; }); } RCT_EXPORT_METHOD(addLocalPush3:(id)detail){ dispatch_async(dispatch_get_main_queue(), ^{ //设置通知内容 UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init]; content.title = detail[@"title"]; content.body = detail[@"body"]; NSString *category_id = detail[@"category_id"]; NSString *message_id = detail[@"id"]; NSString *mode = detail[@"mode"]; NSArray *actions; if ([category_id isEqualToString:@"REMINDER_FS_START_FAST"]||[category_id isEqualToString:@"REMINDER_FS_START_SLEEP"]){ //消息的处理按钮 UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground]; UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground]; UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired]; actions = @[action1,action2,action3]; } else if([category_id isEqualToString:@"REMINDER_FS_END_FAST"]||[category_id isEqualToString:@"REMINDER_FS_END_SLEEP"]){ //消息的处理按钮 UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"END_TIMER_NOW" title:@"End timer now with 1-tap" options:UNNotificationActionOptionForeground]; UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_END" title:@"Pick an earlier end" options:UNNotificationActionOptionForeground]; UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired]; actions = @[action1,action2,action3]; } UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:category_id actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]]; content.categoryIdentifier = category_id; if ([mode isEqualToString:@"RECURRING"]){ //设置推送的触发机制 NSArray *timeArray = [detail[@"recurring"][@"time"] componentsSeparatedByString:@":"]; NSInteger hour = [[timeArray objectAtIndex:0] integerValue]; NSInteger minute = [[timeArray objectAtIndex:1] integerValue]; NSInteger second = [[timeArray objectAtIndex:2] integerValue]; // 2. 设置触发条件 NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; dateComponents.hour = hour; dateComponents.minute = minute; dateComponents.second = second; UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES]; NSString * identifier = message_id; UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { }]; } else { NSTimeInterval timestamp = [detail[@"once"] doubleValue]/1000; // 毫秒级时间戳 // 获取当前时间 NSDate *currentDate = [NSDate date]; // 计算当前时间与给定时间戳之间的时间差(秒) NSTimeInterval timeInterval = timestamp - [currentDate timeIntervalSince1970]; UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO]; NSString * identifier = message_id; UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { }]; } }); } @end