feat: 优化后台任务管理,添加系统权限和用户偏好的完整检查,增强通知功能
This commit is contained in:
@@ -182,7 +182,7 @@ export class NotificationService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查用户是否允许推送通知
|
||||
* 检查用户是否允许推送通知(不包括系统权限检查,仅检查用户偏好)
|
||||
*/
|
||||
private async isNotificationAllowed(): Promise<boolean> {
|
||||
try {
|
||||
@@ -193,16 +193,35 @@ export class NotificationService {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('检查推送权限失败:', error);
|
||||
// 如果检查失败,默认允许(避免阻塞重要的健康提醒)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 完整的权限检查(包括系统权限和用户偏好)
|
||||
*/
|
||||
private async hasFullNotificationPermission(): Promise<boolean> {
|
||||
try {
|
||||
// 检查系统权限
|
||||
const permissionStatus = await this.getPermissionStatus();
|
||||
if (permissionStatus !== 'granted') {
|
||||
console.log('系统推送权限未授予');
|
||||
console.log('系统推送权限未授予:', permissionStatus);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查用户偏好
|
||||
const userPreferenceEnabled = await this.isNotificationAllowed();
|
||||
if (!userPreferenceEnabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('检查推送权限失败:', error);
|
||||
console.error('完整权限检查失败:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -215,11 +234,11 @@ export class NotificationService {
|
||||
trigger?: Notifications.NotificationTriggerInput
|
||||
): Promise<string> {
|
||||
try {
|
||||
// 检查用户是否允许推送通知
|
||||
const isAllowed = await this.isNotificationAllowed();
|
||||
if (!isAllowed) {
|
||||
console.log('推送通知被用户偏好设置或系统权限阻止,跳过发送');
|
||||
return 'blocked_by_user_preference';
|
||||
// 检查完整权限(系统权限 + 用户偏好)
|
||||
const hasPermission = await this.hasFullNotificationPermission();
|
||||
if (!hasPermission) {
|
||||
console.log('推送通知被系统权限或用户偏好设置阻止,跳过发送');
|
||||
return 'blocked_by_permission_or_preference';
|
||||
}
|
||||
|
||||
const notificationId = await Notifications.scheduleNotificationAsync({
|
||||
@@ -234,10 +253,10 @@ export class NotificationService {
|
||||
trigger: trigger || null, // null表示立即发送
|
||||
});
|
||||
|
||||
console.log('本地通知已安排,ID:', notificationId);
|
||||
console.log('✅ 本地通知已安排,ID:', notificationId);
|
||||
return notificationId;
|
||||
} catch (error) {
|
||||
console.error('安排本地通知失败:', error);
|
||||
console.error('❌ 安排本地通知失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -246,6 +265,7 @@ export class NotificationService {
|
||||
* 发送立即通知
|
||||
*/
|
||||
async sendImmediateNotification(notification: NotificationData): Promise<string> {
|
||||
console.log('📱 准备发送立即通知:', notification.title);
|
||||
return this.scheduleLocalNotification(notification);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user