单击通知时反应本机导航到特定屏幕

2024-05-26

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

假设您正在使用react-navigation, the NavigationContainer接受正常的ref prop:

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

see 导航容器文档 https://reactnavigation.org/docs/navigation-container

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

单击通知时反应本机导航到特定屏幕 的相关文章

随机推荐

  • 为什么这段代码不会产生编译错误?

    template
  • MySQL使用long类型数字过滤varchar类型时返回额外记录

    一个简单的表格 CREATE TABLE tbl type test uid varchar 31 NOT NULL DEFAULT 0 value varchar 15 NOT NULL DEFAULT PRIMARY KEY uid E
  • 在unity3D中显示数学方程

    我想使用它的 GUI 系统统一显示数学方程 有办法吗 我正在使用 C 语言在 Unity 中进行编程 如果我还可以使用 C 代码显示数学符号 这对我来说会很有用 谢谢 自 2016 年起 您可以使用TEXDraw https assetst
  • 将整数值从数据库列转换为Oracle中的文本

    我对数据库有一个要求 1 表ABC 列 check amount number number 18 4 这基本上包含例如支票金额 3000 50 支付给雇员 现在签发了一张支票 该支票包含数字和文本形式的 check amount 例如 支
  • 如何使用 HttpClient 验证 Pardot API

    我花了大约一天的时间尝试对 Pardot API 进行身份验证 它不喜欢我尝试发布消息正文的方式 所以我想发布对我有用的解决方案 如果您有任何建议或替代方案 我想听听 ServicePointManager SecurityProtocol
  • 导入错误:没有名为 app_name 的模块

    我是 python 新手 我在 SO 上看到过类似的问题 但无法弄清楚我的 Django 应用程序出了什么问题 在 settings py 中 我以点线模块形式添加了我的应用程序 INSTALLED APPS django contrib
  • IntelliJ Play2 设置未保存

    在 IntelliJ 中 我尝试在语言和框架中保存 Play 2 编译器的以下设置 但它没有被保存 文件 设置 语言和框架 Play 2 激活 使用Play 2 确定当我随后按照上述步骤查找此设置时 它没有被激活 有谁知道为什么它没有被保存
  • PHP 指针与引用

    在 PHP 中 使用指针有什么区别 例如 function foo var var 3 a 0 foo a 以及参考 function foo var var 3 a 0 foo a 它们都修改了原始变量的值 但是它们内部的表示方式不同吗
  • 不同的 IntentServices 可以并行运行吗?

    我有点难以理解有关 IntentServices 的一条规则 From 创建后台服务 https developer android com training run background service create service ht
  • 我可以使用本机系统窗口作为父窗口使 JDialog 成为模式吗?

    我有一个 JDialog 窗口 我需要使其成为模态窗口 但父窗口不是 Java 窗口 而是本机 Windows 操作系统窗口 是否可以 不 你不能 您甚至无法不仅引用本机窗口 甚至无法引用运行在其他 JVM 中的 java 应用程序创建的窗
  • 使用 git filter-branch 然后 git push --force 清除大文件后,远程(Gitlab)上的 Git 存储库大小仍然很大

    我已按照 Gitlab 文档中有关 使用 Git 减少存储库大小 的说明进行操作 通过从历史记录中清除大文件 https docs gitlab com ee user project repository reducing the rep
  • 获取RFC返回的嵌套结构的值?

    我是 C 新手 我有 rfc 它以嵌套结构的形式从 SAP 系统返回数据 但是当我使用以下方式获取该数据时 IrfcTable table rfc getTable exporting parameter et customer 它仅返回第
  • 在 WPF 中以编程方式设置 ComboBox SelectedItem (3.5sp1)

    在安装了 Net Framework 3 5 sp1 的 wpf 应用程序中以编程方式设置 SelectedItem 时 我感到很困惑 我仔细阅读了数百篇文章 主题 但仍然感到困惑 我的xaml
  • 自定义 UITableViewCell 中按钮上的 IBAction

    使用 iOS 5 我有一个场景 我必须使用自定义单元格创建一个 tableView 自定义单元格有一个名为 TainingCellController 的控制器 UITableViewCell 的子类和一个 NIB 文件 TrainingC
  • 有没有办法降低 scipy/numpy 精度以减少内存消耗?

    在我的 64 位 Debian Lenny 系统 4GByte RAM 4GByte 交换分区 上 我可以成功执行以下操作 v array 10000 random 512 512 512 dtype np int16 f fftn v 但
  • 可重复部分中重复的 Orbeon 指数

    我有可重复部分 不是中继器 其中有输出字段 如何将重复索引放入此输出 我想将输出与当前迭代的索引绑定 例如 如果我添加 5 个重复 我希望输出显示它们的索引 1 2 3 4 5 我知道有像 xxf index 这样的函数 但它们只在中继器中
  • 扩展一个类

    编辑回答 虽然我最初的问题并没有完全按照康拉德 鲁道夫提供的答案所解决的方式解释我的需求 但他 无意或有意 基本上为我写了我想写的内容 类本身不会被扩展 但通过使类了解新函数来扩展其功能 这些新函数允许它 类 处理更广泛的问题 我非常感谢您
  • OpenMP 和 C++:this 指针

    Is thisOpenMP 中始终共享指针 尽管编译器不会抱怨以下代码default none pragma omp parallel for default none shared n for SInt i 0 i lt n i f i
  • 发送fulfillmentText并使用followupEventInput转移到另一个意图

    我使用 Python Flask 设置了一个简单的 Webhook 来处理各种 Dialogflow 功能 在这一点上一切都进展顺利 该机器人通过 DialogFlow API V2 集成到 Facebook Messenger 问题是 关
  • 单击通知时反应本机导航到特定屏幕

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