【问题标题】:React Native Navigate to Particular Screen When Notification is Clicked单击通知时反应本机导航到特定屏幕
【发布时间】:2021-03-15 13:26:41
【问题描述】:

我正在尝试在用户单击他们收到的通知时实现导航。我通过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,
};

app.js:

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

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

【问题讨论】:

  • 你好,denistepp!我是 Expo Notifications 的新手,正在研究如何在单击通知时将用户导航到屏幕。您是否碰巧有一个基于上述内容的完整示例?

标签: reactjs react-native navigation expo expo-notifications


【解决方案1】:

假设你使用react-navigationNavigationContainer 接受一个普通的ref 属性:

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

NavigationContainer docs

【讨论】:

  • 哇,这是一个愚蠢的错误,我没有正确写ref...谢谢!
  • 你好 Marek,你能看看这个问题吗:stackoverflow.com/questions/68199491/…。我使用了与 denistepp 相同的代码。不幸的是,它不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 2021-07-25
相关资源
最近更新 更多