| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- //
- // 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()
- -(NSArray<NSString *>*)supportedEvents
- {
- return @[@"receive"];
- //EventReminder 是监听的标识,类似 iOS 发通知 需要一个标识去识别,通过这个标识发送通知调用 RN方法
-
- }
- 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:(NSString *)title
- withBody:(NSString *)body
- withTime:(NSString *)time){
- dispatch_async(dispatch_get_main_queue(), ^{
- // 1. 设置 notification 内容
- UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
- content.title = title;
- content.body = body;
- content.sound = [UNNotificationSound defaultSound];
-
- // 按冒号分割字符串为数组
- NSArray *timeArray = [time 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;
- 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);
- }
- }];
- });
- }
- @end
|