feat: 添加用户推送通知偏好设置功能,支持开启/关闭推送通知

This commit is contained in:
richarjiang
2025-09-03 10:58:45 +08:00
parent e33a690a36
commit 8b6ef378d0
6 changed files with 265 additions and 26 deletions

View File

@@ -1,3 +1,4 @@
import { getNotificationEnabled } from '@/utils/userPreferences';
import * as Notifications from 'expo-notifications';
import { router } from 'expo-router';
@@ -180,6 +181,32 @@ export class NotificationService {
}
}
/**
* 检查用户是否允许推送通知
*/
private async isNotificationAllowed(): Promise<boolean> {
try {
// 检查用户偏好设置中的推送开关
const userPreferenceEnabled = await getNotificationEnabled();
if (!userPreferenceEnabled) {
console.log('用户已在偏好设置中关闭推送通知');
return false;
}
// 检查系统权限
const permissionStatus = await this.getPermissionStatus();
if (permissionStatus !== 'granted') {
console.log('系统推送权限未授予');
return false;
}
return true;
} catch (error) {
console.error('检查推送权限失败:', error);
return false;
}
}
/**
* 发送本地推送通知
*/
@@ -188,6 +215,13 @@ export class NotificationService {
trigger?: Notifications.NotificationTriggerInput
): Promise<string> {
try {
// 检查用户是否允许推送通知
const isAllowed = await this.isNotificationAllowed();
if (!isAllowed) {
console.log('推送通知被用户偏好设置或系统权限阻止,跳过发送');
return 'blocked_by_user_preference';
}
const notificationId = await Notifications.scheduleNotificationAsync({
content: {
title: notification.title,