android - Ionic React : useHistory(). push() 在点击 FCM 通知时不起作用

标签 android ios reactjs firebase ionic-framework

所以我设置了我的推送通知处理程序:

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]);
}
来自我的App.tsx :
const App: React.FC = () => {

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

  useInitializePushNotification();

  ....
nav.push(url)正在更改我的网址,但路由不起作用。即使更改了导航,页面也不会更改。仅当我点击来自 FCM 的通知时才会发生这种情况在 background模式,手动 nav.push()如果我的应用位于 foreground 中,则可以正常工作.
我怎样才能解决这个问题?

最佳答案

经过一番苦苦挣扎,我终于找到了解决办法。我用 Redux正确导航它。
在我的 pushNotificationActionPerformed事件:

    PushNotifications.addListener('pushNotificationActionPerformed',
      (notification: ActionPerformed) => {
        let data = notification.notification.data;
        let url = `/owner/support?id=${data.chatRoomId}&name=${data.chatRoomName}`;
        dispatch(setNeedNavigateTo(url));
      }
    );
我发送一个要在 App.tsx 中触发的 url .
这是我的App.tsx :
  const needNavigateTo = useAppSelector((state) => state.support.needNavigateTo);

  const nav = useHistory();

  useEffect(() => {
    if (nav && needNavigateTo) {
      nav.push(needNavigateTo); //this works
      setNeedNavigateTo(undefined); //set the url to undefined (resetting)
    }
  }, [nav, needNavigateTo]);
对于不使用 Redux 的人来说,此解决方案并不理想。 ,但这是我找到工作的唯一途径。我不确定此解决方案是否适用于 context .

关于android - Ionic React : useHistory(). push() 在点击 FCM 通知时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70360340/

相关文章:

android - 使用 DatePickerDialog 时获取星期几

javascript - 将 Firebase 元素映射为数组

javascript - 功能 setSelectionRange 在 Android 浏览器中不起作用

android - 使用 GSON 解析多个对象

c# - Xamarin iOS - 交互式滑动

android - 在 React Native 中将图像放在 android 和 ios 的公共(public)位置

ios - 如何以编程方式覆盖 Storyboard UIView 高度属性

javascript - TypeScript:在没有显式返回类型的情况下,不会在 lambda 中检查多余的属性

javascript - 无法读取未定义卡片 Material ui 的属性

android - 我如何在 MonoDroid 中使用长按?