| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- //
- // NativeBridge.m
- // hola
- //
- // Created by Leon on 2024/4/28.
- //
- #import <Foundation/Foundation.h>
- #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<NSString *>*)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(addLocalPush2:(id)detail){
- dispatch_async(dispatch_get_main_queue(), ^{
- //设置通知内容
- UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
- content.title = @"a1";
- content.body = @"b1";
-
- UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW1" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground];
-
- UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START2" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground];
- NSArray *actions = @[action1,action2];
-
-
- UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:@"c1" actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
-
- // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]];
- content.categoryIdentifier = @"c1";
-
- UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
- NSString * identifier = @"e1";
- UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
- [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
-
- }];
-
- UNMutableNotificationContent * content2 = [[UNMutableNotificationContent alloc] init];
- content2.title = @"a12";
- content2.body = @"b12";
-
- NSArray *actions2;
-
- UNNotificationAction *action12 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground];
-
- UNNotificationAction *action22 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground];
-
- UNNotificationAction *action32 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
- actions2 = @[action12,action22,action32];
-
-
- UNNotificationCategory *categroy2 = [UNNotificationCategory categoryWithIdentifier:@"c12" actions:actions2 intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
-
- // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy2]];
- [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithArray:@[categroy,categroy2]]];
- content2.categoryIdentifier = @"c12";
-
- UNTimeIntervalNotificationTrigger * trigger2 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:15 repeats:NO];
- NSString * identifier2 = @"e12";
- UNNotificationRequest * request2 = [UNNotificationRequest requestWithIdentifier:identifier2 content:content2 trigger:trigger2];
- [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request2 withCompletionHandler:^(NSError * _Nullable error) {
- }];
-
- });
- }
- 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"]];
-
- 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
|