ソースを参照

local notification

leon 1 年間 前
コミット
7fe0bd51f6

+ 10 - 1
ios/AppDelegate.mm

@@ -94,6 +94,8 @@ static NSString *const kRNConcurrentRoot = @"concurrentRoot";
                                      selector:@selector(timerFired:)
                                      userInfo:nil
                                       repeats:NO];
+  
+  [self clearAllDeliveredNotifications];
   return YES;
 }
 
@@ -142,7 +144,7 @@ static NSString *const kRNConcurrentRoot = @"concurrentRoot";
 - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
 {
 //  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
-#if DEBUG
+#if DEBUGAAA
   return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
 #else
   return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
@@ -304,6 +306,7 @@ static NSString *const kRNConcurrentRoot = @"concurrentRoot";
     NSString *categoryIdentifier = response.notification.request.content.categoryIdentifier;
     NSString *identifier = response.notification.request.identifier;
     [self.nativeBridge.bridge.eventDispatcher sendAppEventWithName:@"notificationReceive" body:@{@"category_id":categoryIdentifier,@"action_id":response.actionIdentifier,@"id":identifier}];
+    [self clearAllDeliveredNotifications];
   }
   else {
     
@@ -378,4 +381,10 @@ static NSString *const kRNConcurrentRoot = @"concurrentRoot";
 }
 
 
+- (void)clearAllDeliveredNotifications {
+    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
+    [center removeAllDeliveredNotifications];
+}
+
+
 @end

ファイルの差分が大きいため隠しています
+ 1 - 1
ios/main.jsbundle


+ 31 - 31
src/app.tsx

@@ -38,37 +38,37 @@ const App: React.FC<PropsWithChildren> = ({ children }) => {
 
 
 
-  if (process.env.TARO_ENV == 'rn') {
-    global.language = 'en'
-    PushNotification.configure({
-      // onRegister: function (token) {
-      //     console.log('TOKEN:', token);
-      // },
-
-      onNotification: function (notification) {
-        console.log('NOTIFICATION:', notification);
-        setTimeout(() => {
-          uploadLocalPushInfo(notification)
-        }, 3000)
-
-        // notification.finish(PushNotificationIOS.FetchResult.NoData);
-      },
-
-      // permissions: {
-      //     alert: true,
-      //     badge: true,
-      //     sound: true,
-      // },
-
-      // popInitialNotification: true,
-      // requestPermissions: true,
-    });
-
-    // messaging().setBackgroundMessageHandler(async remoteMessage => {
-    //   console.log('Message is handled in the background!', remoteMessage);
-    // });
-    // alert(Intl.DateTimeFormat().resolvedOptions().timeZone)
-  }
+  // if (process.env.TARO_ENV == 'rn') {
+  //   global.language = 'en'
+  //   PushNotification.configure({
+  //     // onRegister: function (token) {
+  //     //     console.log('TOKEN:', token);
+  //     // },
+
+  //     onNotification: function (notification) {
+  //       console.log('NOTIFICATION:', notification);
+  //       setTimeout(() => {
+  //         uploadLocalPushInfo(notification)
+  //       }, 3000)
+
+  //       // notification.finish(PushNotificationIOS.FetchResult.NoData);
+  //     },
+
+  //     // permissions: {
+  //     //     alert: true,
+  //     //     badge: true,
+  //     //     sound: true,
+  //     // },
+
+  //     // popInitialNotification: true,
+  //     // requestPermissions: true,
+  //   });
+
+  //   // messaging().setBackgroundMessageHandler(async remoteMessage => {
+  //   //   console.log('Message is handled in the background!', remoteMessage);
+  //   // });
+  //   // alert(Intl.DateTimeFormat().resolvedOptions().timeZone)
+  // }
   if (process.env.TARO_ENV == 'weapp') {
     loadWXCache()
     global.isDebug = Taro.getStorageSync('isDebug')

+ 4 - 4
src/features/trackTimeDuration/actions/TrackTimeActions.tsx

@@ -27,7 +27,7 @@ export const startFast = (start_time: number, duration: number) => {
         }).then(res => {
             resolve(res)
             trackTimeService.send({ type: 'START_FAST' });
-            getLocalPush()
+            // getLocalPush()
         });
     });
 }
@@ -47,7 +47,7 @@ export const endFast = (start_time: number) => {
             trackTimeService.send({ type: 'RESET' });
             trackTimeService.send({ type: global.scenario });
             resolve(res)
-            getLocalPush()
+            // getLocalPush()
         }).catch(e => {
             reject(e)
         });
@@ -70,7 +70,7 @@ export const startSleep = (start_time: number, duration: number) => {
         }).then(res => {
             trackTimeService.send({ type: 'START_SLEEP' });
             resolve(res)
-            getLocalPush()
+            // getLocalPush()
         }).catch(e => {
             reject(e)
         });
@@ -89,7 +89,7 @@ export const endSleep = (start_time: number) => {
         }).then(res => {
             trackTimeService.send({ type: 'END_SLEEP' });
             resolve(res)
-            getLocalPush()
+            // getLocalPush()
         }).catch(e => {
             reject(e)
         });

+ 7 - 1
src/features/trackTimeDuration/components/IndexConsole.tsx

@@ -2,7 +2,7 @@ import { View, Text, Image, PageContainer } from '@tarojs/components'
 import './IndexConsole.scss'
 import { useTranslation } from 'react-i18next'
 import { useDispatch, useSelector } from 'react-redux';
-import { endFast, endSleep, startFast, startSleep, uploadLocalPushInfo } from "../actions/TrackTimeActions";
+import { endFast, endSleep, getLocalPush, startFast, startSleep, uploadLocalPushInfo } from "../actions/TrackTimeActions";
 import { jumpPage } from '../hooks/Common';
 import { useContext, useEffect, useRef, useState } from 'react';
 import ConsolePicker from './ConsolePicker';
@@ -549,6 +549,7 @@ export default function IndexConsole(props: { record: any, count: number }) {
                     else {
                         JPush.isNotificationEnabled((res) => {
                             if (res) {
+                                getLocalPush()
                                 showAlert({
                                     title: t('feature.track_time_duration.reminders.fast_end_title'),
                                     content:
@@ -619,6 +620,7 @@ export default function IndexConsole(props: { record: any, count: number }) {
                     else {
                         JPush.isNotificationEnabled((res) => {
                             if (res) {
+                                getLocalPush()
                                 showAlert({
                                     title: t('feature.track_time_duration.reminders.wake_title'),
                                     content:
@@ -659,6 +661,7 @@ export default function IndexConsole(props: { record: any, count: number }) {
                 break
             case 'endSleep':
                 endSleep(t1).then(res => {
+                    getLocalPush()
                     setBtnDisable(false)
                     global.indexPageRefresh()
                     setFirstEnter(false)
@@ -671,6 +674,7 @@ export default function IndexConsole(props: { record: any, count: number }) {
 
                     }
                 }).catch((e) => {
+                    
                     setBtnDisable(false)
                     var picker = limitPickerRef.current;
                     (picker as any).resetPickerData()
@@ -680,6 +684,7 @@ export default function IndexConsole(props: { record: any, count: number }) {
                 break
             case 'endFast':
                 endFast(t1).then(res => {
+                    getLocalPush()
                     setBtnDisable(false)
                     global.indexPageRefresh()
                     setFirstEnter(false)
@@ -698,6 +703,7 @@ export default function IndexConsole(props: { record: any, count: number }) {
                     global.checkAccess((res as any).access)
                     // checkAccessProvisional((res as any).access,showFastAlert)
                 }).catch((e) => {
+                    
                     setBtnDisable(false)
                     var picker = limitPickerRef.current;
                     (picker as any).resetPickerData()

+ 0 - 1
src/features/trackTimeDuration/components/SetSchedule.rn.tsx

@@ -108,7 +108,6 @@ export default function Component() {
         setHours(durationTime(scheduleObj.start_time, scheduleObj.end_time)[0])
         setMinutes(durationTime(scheduleObj.start_time, scheduleObj.end_time)[1])
 
-
         if (Taro.getSystemInfoSync().platform == 'ios') {
             var Jto = require('react-native').NativeModules.NativeBridge;
             Jto.getNotificationAuthStatus()

+ 0 - 4
src/pages/clock/Clock.tsx

@@ -67,8 +67,6 @@ let Linking;
 let JPush;
 let checkNotification, uploadPermissions, NativeAppEventEmitter;
 let messaging;
-let PushNotification;
-let Importance;
 
 if (process.env.TARO_ENV == 'rn') {
     messaging = require('@react-native-firebase/messaging').default
@@ -80,8 +78,6 @@ if (process.env.TARO_ENV == 'rn') {
     useNavigation = require("@react-navigation/native").useNavigation
     checkNotification = require('@/utils/native_permission_check').checkNotification;
     uploadPermissions = require('@/utils/native_permission_check').uploadPermissions;
-    PushNotification = require('react-native-push-notification')
-    Importance = require('react-native-push-notification').Importance
 }
 
 const defaultValue = `[{"scenario":{"name":"FAST_SLEEP","count":0,"schedule":{"fast":{"start_time":"15:00","end_time":"08:00"},"sleep":{"start_time":"22:00","end_time":"07:00"}}},"current_record":{"scenario":"FAST_SLEEP","status":"WAIT_FOR_START","fast":{"status":"WAIT_FOR_START","target_start_time":1710514800000,"target_end_time":1710576000000},"sleep":{"status":"WAIT_FOR_START","target_start_time":1710540000000,"target_end_time":1710572400000}},"wx_pub_followed":false,"time_input_schema":{"min":60,"max":1380,"step":5},"theme_color":{"fast":"#00FFFF","sleep":"#8183FF"}},{"scenarios":[{"name":"FAST","count":0,"schedule":{"fast":{"start_time":"15:00","end_time":"08:00"}}},{"name":"SLEEP","count":0,"schedule":{"sleep":{"start_time":"22:00","end_time":"07:00"}}},{"name":"FAST_SLEEP","count":0,"schedule":{"fast":{"start_time":"15:00","end_time":"08:00"},"sleep":{"start_time":"22:00","end_time":"07:00"}}}],"select_count":0,"theme_color":{"fast":"#00FFFF","sleep":"#8183FF"}}]`

+ 169 - 51
src/pages/clock/SetGoal.tsx

@@ -14,6 +14,19 @@ import { updateFast, updateSleep } from "@/store/set_target";
 import { setPlan } from "@/services/trackTimeDuration";
 import showAlert from "@/components/basic/Alert";
 import Taro from "@tarojs/taro";
+import { getLocalPush } from "@/features/trackTimeDuration/actions/TrackTimeActions";
+
+let OneSignal
+let NativeAppEventEmitter
+let Jto
+let uploadPermissions,checkNotification
+if (process.env.TARO_ENV == 'rn') {
+    OneSignal = require('react-native-onesignal').OneSignal
+    NativeAppEventEmitter = require('react-native').NativeAppEventEmitter
+    Jto = require('react-native').NativeModules.NativeBridge;
+    uploadPermissions = require('@/utils/native_permission_check').uploadPermissions;
+    checkNotification = require('@/utils/native_permission_check').checkNotification;
+}
 
 export default function SetGoal() {
 
@@ -37,14 +50,60 @@ export default function SetGoal() {
     const [showTimePicker, setShowTimePicker] = useState(false)
     const [isFast, setIsFast] = useState(true)
     const [isStart, setIsStart] = useState(true)
+    const [authStatus, setAuthStatus] = useState('')
     const { t } = useTranslation()
     const dispatch = useDispatch();
 
+    useEffect(() => {
+        if (process.env.TARO_ENV == 'rn') {
+            rnNotification()
+        }
+    }, [])
+
     useEffect(() => {
         getFastDuration()
         getSleepDuration()
     }, [target.fast, target.sleep])
 
+
+    function rnNotification() {
+        if (Taro.getSystemInfoSync().platform == 'ios') {
+            Jto.getNotificationAuthStatus()
+            NativeAppEventEmitter.addListener('notificationResult', (data) => {
+                setAuthStatus(data)
+                global.notification = data
+                uploadPermissions()
+            })
+            NativeAppEventEmitter.addListener('operateNotificationResult', (data) => {
+                getLocalPush()
+                global.notification = data
+                navigation.popToTop()
+                uploadPermissions()
+            })
+        }
+        else {
+            OneSignal.Notifications.canRequestPermission().then((status) => {
+                if (status) {
+                    OneSignal.Notifications.getPermissionAsync().then((res) => {
+                        if (res) {
+                            global.notification = 'authorized'
+                            uploadPermissions()
+                        }
+                        else {
+                            global.notification = 'denied'
+                            uploadPermissions()
+                        }
+                    })
+                }
+                else {
+                    global.notification = 'not_determined'
+                    uploadPermissions()
+                }
+            })
+
+        }
+    }
+
     function getFastDuration() {
         var obj = target.fast.schedule.fast
         var duration = obj.end_time.split(':')[0] * 60 + obj.end_time.split(':')[1] * 1 - (obj.start_time.split(':')[0] * 60 + obj.start_time.split(':')[1] * 1)
@@ -183,6 +242,7 @@ export default function SetGoal() {
     }
 
     function confirm() {
+        debugger
         var params: any = {
             method: parseInt(router.params.isSelf + '') == 1 ? 'USER_SET' : 'SUGGEST',
             schedule: {
@@ -233,13 +293,13 @@ export default function SetGoal() {
                 global.indexPageRefresh()
             }
 
-            // if (process.env.TARO_ENV === 'rn') {
-            //     popRNAlert()
-            // }
-            // else {
-            //     popAlert()
-            // }
-            popAlert()
+            if (process.env.TARO_ENV === 'rn') {
+                popRNAlert()
+            }
+            else {
+                popAlert()
+            }
+            // popAlert()
 
 
         }).catch(e => {
@@ -271,50 +331,108 @@ export default function SetGoal() {
         }
     }
 
-    function popRNAlert() {
-        // if (target.isMixed) {
-
-        // }
-        // else {
-        //     const JPush = require('jpush-react-native').default;
-        //     JPush.isNotificationEnabled((res) => {
-        //         if (res) {
-        //             showAlert({
-        //                 title: t('feature.track_time_duration.reminders.schedule_title'),
-        //                 content:
-        //                     t('feature.track_time_duration.reminders.enable_schedule_fast_content', { time: fastTarget.start_time }),
-        //                 showCancel: false,
-        //                 confirmText: t('feature.track_time_duration.reminders.ok'),
-        //                 cancel: () => {
-        //                     navigation.popToTop()
-        //                 },
-        //                 confirm: () => {
-        //                     navigation.popToTop()
-        //                 },
-        //             })
-        //         }
-        //         else {
-        //             showAlert({
-        //                 title: t('feature.track_time_duration.reminders.schedule_title'),
-        //                 content:
-        //                     t('feature.track_time_duration.reminders.schedule_fast_content', { time: fastTarget.start_time }),
-        //                 cancelText: t('feature.track_time_duration.reminders.later'),
-        //                 confirmText: t('feature.track_time_duration.reminders.open'),
-        //                 showCancel: true,
-        //                 cancel: () => {
-        //                     navigation.popToTop()
-        //                 },
-        //                 confirm: () => {
-        //                     // if (authStatus != 'not_determined') {
-        //                     //     navigation.popToTop()
-        //                     // }
-        //                     // checkNotification()
-        //                     // Linking.openURL('app-settings:notifications')
-        //                 }
-        //             })
-        //         }
-        //     })
-        // }
+    function popRNAlert(){
+        debugger
+        if (target.isMixed) {
+            popMixScheduleAlert(fastTarget.start_time, sleepTarget.start_time)
+        }
+        else {
+            popScheduleAlert({
+                name: 'FAST'
+            }, fastTarget.start_time)
+        }
+    }
+
+    function popScheduleAlert(scenario, startTime) {
+        if (process.env.TARO_ENV === 'rn') {
+            const JPush = require('jpush-react-native').default;
+            JPush.isNotificationEnabled((res) => {
+                if (res) {
+                    getLocalPush();
+                    showAlert({
+                        title: t('feature.track_time_duration.reminders.schedule_title'),
+                        content: scenario.name == 'FAST' ?
+                            t('feature.track_time_duration.reminders.enable_schedule_fast_content', { time: startTime }) :
+                            t('feature.track_time_duration.reminders.enable_schedule_sleep_content', { time: startTime }),
+                        showCancel: false,
+                        confirmText: t('feature.track_time_duration.reminders.ok'),
+                        cancel: () => {
+                            navigation.popToTop()
+                        },
+                        confirm: () => {
+                            navigation.popToTop()
+                        },
+                    })
+                }
+                else {
+                    showAlert({
+                        title: t('feature.track_time_duration.reminders.schedule_title'),
+                        content: scenario.name == 'FAST' ?
+                            t('feature.track_time_duration.reminders.schedule_fast_content', { time: startTime }) :
+                            t('feature.track_time_duration.reminders.schedule_sleep_content', { time: startTime }),
+                        cancelText: t('feature.track_time_duration.reminders.later'),
+                        confirmText: t('feature.track_time_duration.reminders.open'),
+                        showCancel: true,
+                        cancel: () => {
+                            navigation.popToTop()
+                        },
+                        confirm: () => {
+                            if (authStatus != 'not_determined') {
+                                navigation.popToTop()
+                            }
+                            checkNotification()
+                            // Linking.openURL('app-settings:notifications')
+                        }
+                    })
+                }
+            })
+        }
+    }
+
+    function popMixScheduleAlert(time1, time2) {
+        console.log(time1, time2)
+        if (process.env.TARO_ENV === 'rn') {
+            const JPush = require('jpush-react-native').default;
+            const checkNotification = require('@/utils/native_permission_check').checkNotification;
+            JPush.isNotificationEnabled((res) => {
+                if (res) {
+                    getLocalPush();
+                    showAlert({
+                        title: t('feature.track_time_duration.reminders.schedule_title'),
+                        content: t('feature.track_time_duration.reminders.enable_schedule_mix_content', { time1: time1, time2: time2 }),
+                        showCancel: false,
+                        confirmText: t('feature.track_time_duration.reminders.ok'),
+                        cancel: () => {
+                            navigation.popToTop()
+                        },
+                        confirm: () => {
+                            navigation.popToTop()
+                        },
+                    })
+                }
+                else {
+                    showAlert({
+                        title: t('feature.track_time_duration.reminders.schedule_title'),
+                        content:
+                            t('feature.track_time_duration.reminders.schedule_mix_content'),
+                        cancelText: t('feature.track_time_duration.reminders.later'),
+                        confirmText: t('feature.track_time_duration.reminders.open'),
+                        showCancel: true,
+                        cancel: () => {
+                            navigation.popToTop()
+                        },
+                        confirm: () => {
+                            if (authStatus != 'not_determined') {
+                                navigation.popToTop()
+                            }
+                            checkNotification()
+                            // Linking.openURL('app-settings:notifications')
+                        }
+
+                    })
+                }
+            })
+        }
     }
 
     function isNextDay(isFast: boolean, isStart: boolean) {

+ 1 - 1
src/services/trackTimeDuration.tsx

@@ -22,7 +22,7 @@ export const setPlan = (params: Record<string, any> | undefined) => {
             url: API_FAST_PLANS, method: 'POST', data: { ...params }
         }).then(res => {
             resolve(res);
-            getLocalPush();
+            // getLocalPush();
         }).catch(e => {
             reject(e)
         })

+ 2 - 0
src/utils/native_permission_check.tsx

@@ -1,3 +1,4 @@
+import { getLocalPush } from '@/features/trackTimeDuration/actions/TrackTimeActions';
 import { clientInfo } from '@/services/common';
 import Taro from '@tarojs/taro';
 import JPush from 'jpush-react-native';
@@ -85,6 +86,7 @@ export const uploadPermissions = () => {
             JPush.isNotificationEnabled((res) => {
                 console.log(res)
                 if (res) {
+                    getLocalPush()
                     clientInfo({
                         perm: {
                             album: statuses[PERMISSIONS.IOS.PHOTO_LIBRARY],

この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません