complated
This commit is contained in:
@@ -12,7 +12,7 @@ export interface IRegisterDeviceBody {
|
||||
platform: string;
|
||||
}
|
||||
|
||||
const commonRequests = {
|
||||
export const commonRequests = {
|
||||
/**
|
||||
* Register device for notification
|
||||
* @param body token
|
||||
@@ -27,35 +27,39 @@ const commonRequests = {
|
||||
return response;
|
||||
},
|
||||
};
|
||||
|
||||
export function useNotifications() {
|
||||
const notificationListener = useRef<any>(null);
|
||||
const responseListener = useRef<any>(null);
|
||||
const queryClinet = useQueryClient();
|
||||
useEffect(() => {
|
||||
registerForPushNotificationsAsync().then((token) => {
|
||||
if (!token) return null;
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const body: IRegisterDeviceBody = {
|
||||
token: token,
|
||||
platform: Platform.OS,
|
||||
};
|
||||
commonRequests.registerDevice(body);
|
||||
useEffect(() => {
|
||||
// Android channel
|
||||
if (Platform.OS === 'android') {
|
||||
Notifications.setNotificationChannelAsync('default', {
|
||||
name: 'default',
|
||||
importance: Notifications.AndroidImportance.MAX,
|
||||
vibrationPattern: [0, 250, 250, 250],
|
||||
lightColor: '#FF231F7C',
|
||||
});
|
||||
}
|
||||
|
||||
// Foreground listener
|
||||
notificationListener.current = Notifications.addNotificationReceivedListener((notification) => {
|
||||
console.log('Notification received:', notification);
|
||||
});
|
||||
|
||||
notificationListener.current = Notifications.addNotificationReceivedListener(
|
||||
(notification) => {}
|
||||
);
|
||||
|
||||
// User response listener
|
||||
responseListener.current = Notifications.addNotificationResponseReceivedListener((response) => {
|
||||
const data = response.notification.request.content.data;
|
||||
queryClinet.refetchQueries({ queryKey: ['notification-list'] });
|
||||
queryClinet.refetchQueries({ queryKey: ['notifications-list'] });
|
||||
if (data?.screen === '/profile/notification') {
|
||||
return;
|
||||
} else {
|
||||
router.push('/profile/notification');
|
||||
}
|
||||
queryClient.refetchQueries({ queryKey: ['notification-list'] });
|
||||
queryClient.refetchQueries({ queryKey: ['notifications-list'] });
|
||||
router.push('/profile/notification');
|
||||
});
|
||||
|
||||
// Token olish va serverga yuborish
|
||||
registerForPushNotificationsAsync().then((token) => {
|
||||
if (!token) return;
|
||||
commonRequests.registerDevice({ token, platform: Platform.OS });
|
||||
});
|
||||
|
||||
return () => {
|
||||
|
||||
Reference in New Issue
Block a user