1

我正在尝试在用户单击他们收到的通知时实现导航。我已成功接收通知expo-notifications并接受来自 API 的数据(路由),但当用户单击通知时无法导航到另一个屏幕。

使用通知:

export default useNotifications = () => {
    ...

    useEffect(() => {
        registerForPushNotificationsAsync().then((token) => {
            setExpoPushToken(token);
            alert(token);
        });

        notificationListener.current = Notifications.addNotificationReceivedListener(
            (notification) => {
                setNotification(notification);
                console.log(notification);
            }
        );

        responseListener.current = Notifications.addNotificationResponseReceivedListener(
            (response) => {
                //notification is received OK
                console.log("opened");
                
                //here I want to navigate to another screen using rootnavigation
                navigation.navigate("Account"); 

                //alert shows fine
                alert("ok");
            }
        );

        return () => {
            Notifications.removeNotificationSubscription(notificationListener);
            Notifications.removeNotificationSubscription(responseListener);
        };
    }, []);
};

航海家:

const SettingsNavigation = ({ component }) => {
    useNotifications();

    return (
        <Stack.Navigator mode="card" screenOptions={{ headerShown: false }}>
            <Stack.Screen
                name="Main"
                component={component}
                options={{ title: "Home" }}
            />

            <Stack.Screen
                name="Timetable"
                component={TimetableScreenBoss}
                options={menuOptions("Schedule")}
            />

            <Stack.Screen
                name="Account"
                component={AccountNavigator}
                options={menuOptions("Account", false)}
            />
        </Stack.Navigator>
    );
};

根导航:

import React from "react";

export const navigationRef = React.createRef();

const navigate = (name, params) =>
    navigationRef.current?.navigate(name, params);

export default {
    navigate,
};

应用程序.js:

import { navigationRef } from "./app/navigation/rootNavigation"; //rootnavigation

<NavigationContainer navigationRef={navigationRef}>
    <CustomNavigator>   
</NavigationContainer>
4

1 回答 1

1

假设您正在使用react-navigation,则NavigationContainer接受普通ref道具:

<NavigationContainer ref={navigationRef}>
    <CustomNavigator>   
</NavigationContainer>

请参阅NavigationContainer 文档

于 2021-03-15T15:48:29.153 回答