feat: 添加用户推送通知偏好设置功能,支持开启/关闭推送通知
This commit is contained in:
@@ -5,6 +5,7 @@ import { useAppDispatch, useAppSelector } from '@/hooks/redux';
|
||||
import { useAuthGuard } from '@/hooks/useAuthGuard';
|
||||
import { useNotifications } from '@/hooks/useNotifications';
|
||||
import { DEFAULT_MEMBER_NAME, fetchActivityHistory, fetchMyProfile } from '@/store/userSlice';
|
||||
import { getNotificationEnabled, setNotificationEnabled as saveNotificationEnabled } from '@/utils/userPreferences';
|
||||
import { Ionicons } from '@expo/vector-icons';
|
||||
import { useBottomTabBarHeight } from '@react-navigation/bottom-tabs';
|
||||
import { useFocusEffect } from '@react-navigation/native';
|
||||
@@ -42,10 +43,22 @@ export default function PersonalScreen() {
|
||||
useFocusEffect(
|
||||
React.useCallback(() => {
|
||||
dispatch(fetchMyProfile());
|
||||
dispatch(fetchActivityHistory())
|
||||
dispatch(fetchActivityHistory());
|
||||
// 加载用户推送偏好设置
|
||||
loadNotificationPreference();
|
||||
}, [dispatch])
|
||||
);
|
||||
|
||||
// 加载用户推送偏好设置
|
||||
const loadNotificationPreference = async () => {
|
||||
try {
|
||||
const enabled = await getNotificationEnabled();
|
||||
setNotificationEnabled(enabled);
|
||||
} catch (error) {
|
||||
console.error('加载推送偏好设置失败:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// 数据格式化函数
|
||||
const formatHeight = () => {
|
||||
if (userProfile.height == null) return '--';
|
||||
@@ -68,22 +81,22 @@ export default function PersonalScreen() {
|
||||
// 显示名称
|
||||
const displayName = (userProfile.name?.trim()) ? userProfile.name : DEFAULT_MEMBER_NAME;
|
||||
|
||||
// 监听通知权限状态变化
|
||||
// 初始化时加载推送偏好设置
|
||||
useEffect(() => {
|
||||
if (permissionStatus === 'granted') {
|
||||
setNotificationEnabled(true);
|
||||
} else {
|
||||
setNotificationEnabled(false);
|
||||
}
|
||||
}, [permissionStatus]);
|
||||
loadNotificationPreference();
|
||||
}, []);
|
||||
|
||||
// 处理通知开关变化
|
||||
const handleNotificationToggle = async (value: boolean) => {
|
||||
if (value) {
|
||||
try {
|
||||
// 先检查系统权限
|
||||
const status = await requestPermission();
|
||||
if (status === 'granted') {
|
||||
// 系统权限获取成功,保存用户偏好设置
|
||||
await saveNotificationEnabled(true);
|
||||
setNotificationEnabled(true);
|
||||
|
||||
// 发送测试通知
|
||||
await sendNotification({
|
||||
title: '通知已开启',
|
||||
@@ -92,14 +105,29 @@ export default function PersonalScreen() {
|
||||
priority: 'normal',
|
||||
});
|
||||
} else {
|
||||
Alert.alert('权限被拒绝', '请在系统设置中开启通知权限');
|
||||
// 系统权限被拒绝,不更新用户偏好设置
|
||||
Alert.alert(
|
||||
'权限被拒绝',
|
||||
'请在系统设置中开启通知权限,然后再尝试开启推送功能',
|
||||
[
|
||||
{ text: '取消', style: 'cancel' },
|
||||
{ text: '去设置', onPress: () => Linking.openSettings() }
|
||||
]
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('开启推送通知失败:', error);
|
||||
Alert.alert('错误', '请求通知权限失败');
|
||||
}
|
||||
} else {
|
||||
setNotificationEnabled(false);
|
||||
Alert.alert('通知已关闭', '您将不会收到推送通知');
|
||||
try {
|
||||
// 关闭推送,保存用户偏好设置
|
||||
await saveNotificationEnabled(false);
|
||||
setNotificationEnabled(false);
|
||||
} catch (error) {
|
||||
console.error('关闭推送通知失败:', error);
|
||||
Alert.alert('错误', '保存设置失败');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user