Leon 1 rok pred
rodič
commit
b991cd4f31

+ 39 - 0
ios/NativeBridge.m

@@ -74,6 +74,45 @@ RCT_EXPORT_METHOD(authNotification){
   });
 }
 
+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
 

+ 18 - 16
src/features/trackTimeDuration/actions/TrackTimeActions.tsx

@@ -5,7 +5,7 @@ import trackTimeService, { machine } from "@/store/trackTimeMachine"
 import Taro from "@tarojs/taro";
 import dayjs from 'dayjs'
 
-let PushNotification, Importance,NativeModules;
+let PushNotification, Importance, NativeModules;
 if (process.env.TARO_ENV == 'rn') {
     PushNotification = require('react-native-push-notification')
     Importance = require('react-native-push-notification').Importance
@@ -156,19 +156,21 @@ export const getLocalPush = () => {
                 else if (item.mode == 'RECURRING') {
                     const { time, repeat_type } = item.recurring;
                     const date = dayjs().format('YYYY-MM-DD')
-                    PushNotification.localNotificationSchedule({
-                        //... You can use all the options from localNotifications
-                        channelId: item.id,
-                        title: item.title,
-                        message: item.content + '  ', // (required)
-                        date: new Date(date + 'T' + time), // in 60 secs
-                        allowWhileIdle: true, // (optional) set notification to work while on doze, default: false
-                        messageId: item.id,
-                        repeatType: repeat_type,
-                        // repeatType:'minute',
-                        /* Android Only Properties */
-
-                    })
+                    var Jto = require('react-native').NativeModules.NativeBridge;
+                    Jto.addLocalPush(item.title,item.content,time)
+                    // PushNotification.localNotificationSchedule({
+                    //     //... You can use all the options from localNotifications
+                    //     channelId: item.id,
+                    //     title: item.title,
+                    //     message: item.content + '  ', // (required)
+                    //     date: new Date(date + 'T' + time), // in 60 secs
+                    //     allowWhileIdle: true, // (optional) set notification to work while on doze, default: false
+                    //     messageId: item.id,
+                    //     repeatType: repeat_type,
+                    //     // repeatType:'minute',
+                    //     /* Android Only Properties */
+
+                    // })
                 }
             }
             else {
@@ -198,9 +200,9 @@ export const getLocalPush = () => {
                     else if (item.mode == 'RECURRING') {
                         const { time, repeat_type } = item.recurring;
                         const date = dayjs().format('YYYY-MM-DD')
-                        const {HolaModule} = NativeModules
+                        const { HolaModule } = NativeModules
                         debugger
-                        HolaModule.addLocalPush(item.title, item.content,time)
+                        HolaModule.addLocalPush(item.title, item.content, time)
                         return;
 
                         PushNotification.localNotificationSchedule({