import {
DRIVER_NOTIFICATION_DELIVERY,
DRIVER_NOTIFICATION_STATUS,
USER_ROLE,
} from '@constants';
import notifee, {EventType} from '@notifee/react-native';
import messaging from '@react-native-firebase/messaging';
import actions, {_onSuccess, _onUnmount} from '@redux/actions';
import displayNotification, {
ANDROID_NOTIFICATION_CHANNEL,
} from '@utils/displayNotification';
import {useEffect} from 'react';
import {useDispatch, useSelector} from 'react-redux';
export default function useNotificationListener() {
const dispatch = useDispatch();
const userToken = useSelector(state => state.user.userToken);
const token = useSelector(state => state.appToken.data);
const userRole = useSelector(state => state.user.role);
const getDataMessage = fMessage => {
if (
Object.values(DRIVER_NOTIFICATION_STATUS).includes(fMessage?.data?.type)
) {
if (userToken && token) {
dispatch({type: actions.GET_USER_INFORMATION});
if (fMessage?.data?.type === DRIVER_NOTIFICATION_STATUS.driverCancel) {
_onUnmount({
type: actions.GET_DATA_BOOKING,
});
}
}
dispatch({
type: actions.SAVE_CURRENT_BOOKING_TRIP,
payload: fMessage?.data,
});
dispatch({
type: actions.GET_USER_INFORMATION,
});
//Get lich sử
dispatch({
type: actions.GET_HISTORY_BOOKING_ORDER,
params: {
numshow: 5,
},
});
dispatch({
type: actions.GET_BOOKING_RUNNING_CUS,
params: {
numshow: 5,
},
});
dispatch({
type: actions.GET_HISTORY_PRESENT,
params: {
numshow: 5,
},
});
} else if (
Object.values(DRIVER_NOTIFICATION_DELIVERY).includes(fMessage?.data?.type)
) {
dispatch({
type: actions.GET_USER_INFORMATION,
});
dispatch({
type: _onSuccess(actions.CHECK_DELIVERY_PICTURE),
payload: null,
});
dispatch({
type: _onSuccess(actions.CHECK_PICK_UP_PICTURE),
payload: null,
});
//Get lich sử
dispatch({
type: actions.GET_HISTORY_DELIVERY,
params: {
numshow: 5,
},
});
dispatch({
type: actions.GET_BOOKING_RUNNING_CUS,
params: {
numshow: 5,
},
});
dispatch({
type: actions.GET_HISTORY_PRESENT,
params: {
numshow: 5,
},
});
}
};
const onMessagePress = message => {};
useEffect(() => {
// Create a channel (required for Android)
notifee.createChannels(Object.values(ANDROID_NOTIFICATION_CHANNEL));
// Request permissions (required for iOS)
notifee.requestPermission({criticalAlert: true});
const unsubscribe = notifee.onForegroundEvent(({type, detail}) => {
switch (type) {
case EventType.DISMISSED:
console.log('User dismissed notification', detail.notification);
break;
case EventType.PRESS:
onMessagePress({
notification: detail.notification,
data: detail.notification?.data,
});
break;
}
});
messaging().onNotificationOpenedApp(onMessagePress);
messaging()
.getInitialNotification()
.then(message => {
if (message) {
onMessagePress(message);
}
});
return () => {
unsubscribe();
};
}, []);
useEffect(() => {
if (userToken) {
messaging().subscribeToTopic('all-customer');
if (userRole === USER_ROLE.driver) {
messaging().subscribeToTopic('all-driver');
} else {
messaging().unsubscribeFromTopic('all-driver');
}
} else {
messaging().unsubscribeFromTopic('all-customer');
messaging().unsubscribeFromTopic('all-driver');
}
messaging().unsubscribeFromTopic('all-store');
}, [userRole, userToken]);
useEffect(() => {
const onMessageListener = messaging().onMessage(message => {
if (__DEV__) {
console.log(
'%c FIREBASE_MESSAGE_FORE_GROUND: ',
'color: yellow; font-weight: bold',
message,
);
}
displayNotification(message);
getDataMessage(message);
dispatch({type: actions.SAVE_FCM_MESSAGE, data: message});
});
return onMessageListener;
}, [dispatch]);
}