import { PushNotification as PushNotificationType } from 'react-native-push-notification' ; import { NavigationContainerRef } from '@react-navigation/native' ; export const notificationHooks = { } ; interface Token { os : string ; token : string ; } export interface NotificationHooks { handleNotification ? : ( notification : PushNotificationType ) => void ; handleRegister ? : ( token : Token ) => void ; handleAction ? : ( notification : PushNotificationType ) => void ; navigation ? : NavigationContainerRef ; } const MAX_ATTEMPTS = 50 ; const ATTEMPTS_DELAY = 200 ; export const navigateWhenReady = ( action , attempts = 0 ) => { if ( notificationHooks ?. navigation ?. getRootState ( ) ) { action ( notificationHooks . navigation ) ; } else if ( attempts < MAX_ATTEMPTS ) { const retry = ( ) => navigateWhenReady ( action , attempts + 1 ) ; setTimeout ( retry , ATTEMPTS_DELAY ) ; } } ;