// // NativeBridge.m // hola // // Created by Leon on 2024/4/28. // #import #import "NativeBridge.h" #import "AppDelegate.h" @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(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(addLocalPush:(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) { }]; } }); } //RCT_EXPORT_METHOD(addLocalPush:(NSString *)title // withBody:(NSString *)body // withTime:(NSString *)time // withDetail:(id)detail){ // dispatch_async(dispatch_get_main_queue(), ^{ // //设置通知内容 // UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init]; // content.title = title; // content.body = body; // // content.subtitle = @"推送的子标题"; // // content.badge = [NSNumber numberWithInteger:0];// 红标 // // content.sound = [UNNotificationSound defaultSound]; // //[UNNotificationSound soundNamed:@""]; 自定义推送声音 // // // //给通知添加本地图片或者视频,写法同下 // // NSString *path = [[NSBundle mainBundle] pathForResource:@"rank2" ofType:@"png"]; // // NSError *error = nil; // // /* // // UNNotificationAttachment是指可以包含音频,图像或视频内容。使用本地通知时,可以在通知创建时,将附件加入即可。对于远程通知,则必须实现使用UNNotificationServiceExtension类通知服务扩展。 // // */ // // UNNotificationAttachment *img_attachment = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:nil error:&error]; // // if (error) { // // NSLog(@"%@", error); // // } // // content.attachments = @[img_attachment];//默认只显示第一个 // // content.launchImageName = @"launch2"; // // //消息的处理按钮 // UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"START_TIMER_NOW" options:UNNotificationActionOptionForeground]; // // UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"PICK_EARLIER_START" options:UNNotificationActionOptionForeground]; // // UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"SKIP" options:UNNotificationActionOptionAuthenticationRequired]; // // UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:@"REMINDER_FS_START_FAST" actions:@[action1,action2,action3] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; // // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]]; // content.categoryIdentifier = @"REMINDER_FS_START_FAST"; // // //设置推送的触发机制 // UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO]; // // //日历形式, // // NSDateComponents * dateComp = [[NSDateComponents alloc] init]; // // dateComp.weekday = 2;//周一,默认第一天是周日 // // dateComp.month = 12;//月份,您可以在这里设置具体的时分秒 // // dateComp.hour = 10;//小时 // // dateComp.minute = 14;//分钟 // // UNCalendarNotificationTrigger * calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComp repeats:YES]; // // //地理位置推送.没实际应用过 // // CLLocationCoordinate2D location ; // // location.latitude = 123; // // location.longitude = 111; // // // // CLCircularRegion * region = [[CLCircularRegion alloc] initWithCenter:location radius:100.0 identifier:@"regionid"]; // // // // UNLocationNotificationTrigger * locationTrigger = [UNLocationNotificationTrigger triggerWithRegion:region repeats:YES]; // // NSString * identifier = @"notification_onlytextid"; // UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; // [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { // // }]; // // // // 1. 设置 notification 内容 // // UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; // // content.title = title; // // content.body = body; // // content.sound = [UNNotificationSound defaultSound]; // // // // // 按冒号分割字符串为数组 // // NSArray *timeArray = [@"10:51:00" componentsSeparatedByString:@":"]; // // // // // 从数组中取出时分秒的值 // // NSInteger hour = [[timeArray objectAtIndex:0] integerValue]; // // NSInteger minute = [[timeArray objectAtIndex:1] integerValue]; // // NSInteger second = [[timeArray objectAtIndex:2] integerValue]; // // // // // 2. 设置触发条件 - 每天 9:00 AM // // NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; // // dateComponents.hour = hour; // // dateComponents.minute = minute; // // dateComponents.second = second; // // UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES]; // // // // // 3. 创建 notification request // // UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"DailyReminder" content:content trigger:trigger]; // // // // // 4. 添加ations // // // 创建允许按钮动作 // // UNNotificationAction *allowAction = [UNNotificationAction actionWithIdentifier:@"ALLOW_ACTION" title:@"Allow" options:UNNotificationActionOptionForeground]; // // // // // 创建拒绝按钮动作 // // UNNotificationAction *denyAction = [UNNotificationAction actionWithIdentifier:@"DENY_ACTION" title:@"Deny" options:UNNotificationActionOptionDestructive]; // // // // // 创建动作集合 // // UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"NOTIFICATION_CATEGORY" actions:@[allowAction, denyAction] intentIdentifiers:@[@"ALLOW_ACTION",@"DENY_ACTION"] options:UNNotificationCategoryOptionCustomDismissAction]; // // // // // 5. 添加 notification request 到通知中心 // // UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; // // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:category]]; // // // // [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { // // if (error != nil) { // // NSLog(@"Error adding notification request: %@", error); // // } // // }]; // }); //} @end