|
|
@@ -20,6 +20,16 @@
|
|
|
|
|
|
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"];
|
|
|
@@ -27,31 +37,38 @@ RCT_EXPORT_MODULE()
|
|
|
|
|
|
}
|
|
|
|
|
|
+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;
|
|
|
- }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
}];
|
|
|
});
|
|
|
}
|
|
|
@@ -61,59 +78,241 @@ 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"];
|
|
|
- }
|
|
|
+ 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:(NSString *)title
|
|
|
withBody:(NSString *)body
|
|
|
- withTime:(NSString *)time){
|
|
|
+ withTime:(NSString *)time
|
|
|
+ withDetail:(id)detail){
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
- // 1. 设置 notification 内容
|
|
|
- UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
|
|
|
+ //设置通知内容
|
|
|
+ UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
|
|
|
content.title = title;
|
|
|
content.body = body;
|
|
|
- content.sound = [UNNotificationSound defaultSound];
|
|
|
|
|
|
- // 按冒号分割字符串为数组
|
|
|
- NSArray *timeArray = [time componentsSeparatedByString:@":"];
|
|
|
-
|
|
|
- // 从数组中取出时分秒的值
|
|
|
+ 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;
|
|
|
+
|
|
|
+ //设置推送的触发机制
|
|
|
+ NSArray *timeArray = [@"19:07: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. 添加 notification request 到通知中心
|
|
|
- UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
|
|
- [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
|
|
|
- if (error != nil) {
|
|
|
- NSLog(@"Error adding notification request: %@", error);
|
|
|
- }
|
|
|
+
|
|
|
+
|
|
|
+ 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);
|
|
|
+ // }
|
|
|
+ // }];
|
|
|
});
|
|
|
}
|
|
|
|
|
|
|
|
|
+//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
|
|
|
|
|
|
|