所以我设置了我的推送通知处理程序:
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/