Friday, 17 December 2021

Ionic React: useHistory().push() not working when tapping FCM notification

So I setup my push notification handler:

export function useInitializePushNotification() {

  const nav = useHistory();

  useEffect(() => {
    PushNotifications.removeAllListeners().then(() => {
      ....

      // Method called when tapping on a notification
      PushNotifications.addListener('pushNotificationActionPerformed',
        (notification: ActionPerformed) => {
          let data = notification.notification.data;
          let url = `/application?applicationId=${data.applicationId}&app=${data.applicationName}&chatRoomId=${data.chatRoomId}&chatRoomName=${data.chatRoomName}&displayChat=true`.replace(/ /g, '-');
          nav.push(url);
        }
      );
    });

    return () => {
      PushNotifications.removeAllListeners();
    }
  }, [nav]);
}

From my App.tsx:

const App: React.FC = () => {

  const dispatch = useAppDispatch();
  const { setAuth } = useAuth(dispatch);
  const [initialized, setInitialized] = useState(false);

  useInitializePushNotification();

  ....

nav.push(url) is changing my url, but routing does not work. The page does not change even after the navigation is changed. This happens only if I tap the notification from FCM on background mode, manual nav.push() is working if my app is in foreground.

How can I fix this?



from Ionic React: useHistory().push() not working when tapping FCM notification

No comments:

Post a Comment