2026-06-12 23:42:09 +08:00
|
|
|
import { useEffect, useRef } from 'react';
|
|
|
|
|
import { Platform } from 'react-native';
|
|
|
|
|
|
|
|
|
|
import { jpushService } from '../services/notification/jpushService';
|
2026-06-15 20:43:15 +08:00
|
|
|
import { isAutoStartAllowed } from '../services/consent/autoStartConsent';
|
2026-06-12 23:42:09 +08:00
|
|
|
|
|
|
|
|
export function useRegisterPushDevice(isAuthenticated: boolean, userID?: string) {
|
|
|
|
|
const deviceRegistered = useRef(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (Platform.OS === 'web' || !isAuthenticated || !userID) return;
|
|
|
|
|
|
2026-06-15 20:43:15 +08:00
|
|
|
// 仅在用户同意自启动后才初始化 JPush
|
|
|
|
|
if (!isAutoStartAllowed()) {
|
|
|
|
|
console.log('[useRegisterPushDevice] 用户未同意自启动,跳过 JPush 初始化');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-12 23:42:09 +08:00
|
|
|
if (!deviceRegistered.current) {
|
|
|
|
|
deviceRegistered.current = true;
|
|
|
|
|
|
|
|
|
|
jpushService.initialize().then((ok) => {
|
|
|
|
|
if (ok && userID) {
|
|
|
|
|
jpushService.setAliasForUser(userID);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
if (!isAuthenticated) {
|
|
|
|
|
deviceRegistered.current = false;
|
|
|
|
|
jpushService.cleanup();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}, [isAuthenticated, userID]);
|
|
|
|
|
}
|