mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
90 lines
4.6 KiB
TypeScript
90 lines
4.6 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { getMessaging, getToken } from 'firebase/messaging';
|
|
import firebaseApp from '@/lib/firebase';
|
|
import { useSession } from 'next-auth/react';
|
|
import useAxiosAuth from '@/lib/axios/useAxiosAuth';
|
|
import { getDeviceFCM } from '@/lib/helper';
|
|
|
|
const useFcmToken = () => {
|
|
const [token, setToken] = useState('');
|
|
const [notificationPermissionStatus, setNotificationPermissionStatus] = useState('');
|
|
|
|
const { data } = useSession()
|
|
const axios = useAxiosAuth()
|
|
|
|
useEffect(() => {
|
|
const retrieveToken = async () => {
|
|
try {
|
|
if (typeof window !== 'undefined' && 'serviceWorker' in navigator) {
|
|
let registration = await navigator.serviceWorker.getRegistration();
|
|
if (registration) {
|
|
console.log('Service worker already register, service config is', { registration });
|
|
} else {
|
|
registration = await navigator.serviceWorker.register('/firebase-messaging-sw.js', { scope: '/fcm' });
|
|
console.log('Success register service worker');
|
|
console.log('service config is', { registration });
|
|
}
|
|
|
|
const messaging = getMessaging(firebaseApp);
|
|
|
|
// Retrieve the notification permission status
|
|
const permission = await Notification.requestPermission();
|
|
setNotificationPermissionStatus(permission);
|
|
|
|
// // Check if permission is granted before retrieving the token
|
|
// if (permission === 'granted') {
|
|
// const localFcmToken = localStorage.getItem('fcm_token')
|
|
// if (localFcmToken && localStorage.getItem('fcm_user') === data?.user.detail.nik) {
|
|
// console.log("Token & User already registered, FCM token:", localFcmToken, data?.user.detail.nik)
|
|
// setToken(localFcmToken!)
|
|
// } else {
|
|
// const currentToken = await getToken(messaging, {
|
|
// vapidKey: 'BP2BYcyv2Mh9AMiX8ZcXaRWPzxa0UCDqzAVSU5Gxtl0ERM2qvwJEAcIsKuxeA7QsMyld55oMeC01qkdYRhGf93c',
|
|
// });
|
|
// if (currentToken) {
|
|
// console.log("Success register new token, FCM token: ", currentToken)
|
|
// localStorage.setItem('fcm_token', currentToken)
|
|
// localStorage.setItem('fcm_user', data?.user.detail.nik!)
|
|
// setToken(currentToken);
|
|
// } else {
|
|
// console.log('No registration token available. Request permission to generate one.');
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// Check if permission is granted before retrieving the token
|
|
if (permission === 'granted') {
|
|
if (!token) {
|
|
const currentToken = await getToken(messaging, {
|
|
vapidKey: 'BP2BYcyv2Mh9AMiX8ZcXaRWPzxa0UCDqzAVSU5Gxtl0ERM2qvwJEAcIsKuxeA7QsMyld55oMeC01qkdYRhGf93c',
|
|
});
|
|
if (currentToken) {
|
|
console.log("Success register new token, FCM token: ", currentToken)
|
|
localStorage.setItem('fcm_token', currentToken)
|
|
localStorage.setItem('fcm_user', data?.user.detail.nik!)
|
|
setToken(currentToken);
|
|
const device = getDeviceFCM()
|
|
setTimeout(() => {
|
|
axios.post('/user/fcm-token', {
|
|
"fcm_token": currentToken,
|
|
device
|
|
}).then((res) => console.log("Submit FCM Token", res))
|
|
}, 2000);
|
|
} else {
|
|
console.log('No registration token available. Request permission to generate one.');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.log('An error occurred while retrieving token:', error);
|
|
}
|
|
};
|
|
|
|
retrieveToken();
|
|
}, []);
|
|
|
|
return { fcmToken: token, notificationPermissionStatus };
|
|
};
|
|
|
|
export default useFcmToken; |