- 添加推送通知管理器和设备令牌管理 - 实现推送通知权限请求和令牌注册 - 新增推送通知设置页面 - 集成推送通知初始化到应用启动流程 - 添加推送通知API服务和本地存储管理 - 更新个人页面添加推送通知设置入口
138 lines
3.5 KiB
TypeScript
138 lines
3.5 KiB
TypeScript
import { notificationService } from '@/services/notifications';
|
|
import { pushNotificationManager, TokenStatus } from '@/services/pushNotificationManager';
|
|
import { useEffect, useState } from 'react';
|
|
|
|
/**
|
|
* 推送通知Hook
|
|
* 用于管理推送通知的初始化和状态
|
|
*/
|
|
export function usePushNotifications() {
|
|
const [isInitialized, setIsInitialized] = useState(false);
|
|
const [tokenStatus, setTokenStatus] = useState<TokenStatus>(TokenStatus.UNKNOWN);
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
initializePushNotifications();
|
|
}, []);
|
|
|
|
/**
|
|
* 初始化推送通知
|
|
*/
|
|
const initializePushNotifications = async () => {
|
|
try {
|
|
setIsLoading(true);
|
|
|
|
// 初始化本地通知服务
|
|
await notificationService.initialize();
|
|
|
|
// 初始化推送通知管理器
|
|
const success = await pushNotificationManager.initialize({
|
|
onTokenReceived: (token) => {
|
|
console.log('Hook: 设备令牌已接收:', token.substring(0, 20) + '...');
|
|
updateTokenStatus();
|
|
},
|
|
onTokenRefresh: (token) => {
|
|
console.log('Hook: 设备令牌已刷新:', token.substring(0, 20) + '...');
|
|
updateTokenStatus();
|
|
},
|
|
onError: (error) => {
|
|
console.error('Hook: 推送通知管理器错误:', error);
|
|
}
|
|
});
|
|
|
|
setIsInitialized(success);
|
|
await updateTokenStatus();
|
|
} catch (error) {
|
|
console.error('Hook: 初始化推送通知失败:', error);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 更新令牌状态
|
|
*/
|
|
const updateTokenStatus = async () => {
|
|
try {
|
|
const status = await pushNotificationManager.getTokenStatus();
|
|
setTokenStatus(status);
|
|
} catch (error) {
|
|
console.error('Hook: 获取令牌状态失败:', error);
|
|
setTokenStatus(TokenStatus.FAILED);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 手动注册设备令牌
|
|
*/
|
|
const registerToken = async () => {
|
|
try {
|
|
const success = await pushNotificationManager.registerDeviceToken();
|
|
if (success) {
|
|
await updateTokenStatus();
|
|
}
|
|
return success;
|
|
} catch (error) {
|
|
console.error('Hook: 注册设备令牌失败:', error);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 获取当前设备令牌
|
|
*/
|
|
const getCurrentToken = () => {
|
|
return pushNotificationManager.getCurrentToken();
|
|
};
|
|
|
|
/**
|
|
* 清除所有推送通知数据
|
|
*/
|
|
const clearAllData = async () => {
|
|
try {
|
|
await pushNotificationManager.clearAllData();
|
|
await updateTokenStatus();
|
|
setIsInitialized(false);
|
|
} catch (error) {
|
|
console.error('Hook: 清除推送通知数据失败:', error);
|
|
}
|
|
};
|
|
|
|
return {
|
|
isInitialized,
|
|
tokenStatus,
|
|
isLoading,
|
|
initializePushNotifications,
|
|
registerToken,
|
|
getCurrentToken,
|
|
updateTokenStatus,
|
|
clearAllData,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 推送通知状态描述
|
|
*/
|
|
export function getTokenStatusDescription(status: TokenStatus): string {
|
|
switch (status) {
|
|
case TokenStatus.UNKNOWN:
|
|
return '状态未知';
|
|
case TokenStatus.GRANTED:
|
|
return '权限已授予,但未注册';
|
|
case TokenStatus.DENIED:
|
|
return '权限被拒绝';
|
|
case TokenStatus.REGISTERED:
|
|
return '设备令牌已注册';
|
|
case TokenStatus.FAILED:
|
|
return '初始化失败';
|
|
default:
|
|
return '未知状态';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查推送通知是否可用
|
|
*/
|
|
export function isPushNotificationAvailable(status: TokenStatus): boolean {
|
|
return status === TokenStatus.REGISTERED || status === TokenStatus.GRANTED;
|
|
} |