30 lines
773 B
TypeScript
30 lines
773 B
TypeScript
|
|
import { useEffect, useRef } from 'react';
|
||
|
|
import { Platform } from 'react-native';
|
||
|
|
|
||
|
|
import { jpushService } from '../services/notification/jpushService';
|
||
|
|
|
||
|
|
export function useRegisterPushDevice(isAuthenticated: boolean, userID?: string) {
|
||
|
|
const deviceRegistered = useRef(false);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
if (Platform.OS === 'web' || !isAuthenticated || !userID) return;
|
||
|
|
|
||
|
|
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]);
|
||
|
|
}
|