简体   繁体   English

按下通知后的 React-Native 导航

[英]React-Native navigate after a notification press

Me and a co-worker have been working on a React-Native app for quite some time now.我和一位同事已经在 React-Native 应用程序上工作了一段时间。 We've been struggling with this problem for a few days now and we don't seem to find a suitable solution.我们已经为此问题苦苦挣扎了几天,但似乎没有找到合适的解决方案。

We are using a few dependencies to make this work.我们正在使用一些依赖项来完成这项工作。

We've tried using the useNavigation() in the notifee.onBackgroundEvent() but we keep getting an Invalid hook call.我们已经尝试在 notifee.onBackgroundEvent() 中使用 useNavigation() 但我们不断收到一个无效的钩子调用。 When we try to pass the navigation as a property trough the function it returns undefined.当我们尝试通过函数将导航作为属性传递时,它返回 undefined。

We want to navigate the user to a page after the user has pressed the notification when the app is closed/killed当应用程序关闭/终止时,我们希望在用户按下通知后将用户导航到一个页面

index.js索引.js

import {AppRegistry} from 'react-native';
import React, {useEffect, useState} from 'react';
import messaging from '@react-native-firebase/messaging';
import App from './App';
import {name as appName} from './app.json';
import getNotification from './src/services/RESTnotification';
import notifee, {EventType} from "@notifee/react-native";
import {useNavigation} from "@react-navigation/native";

messaging().setBackgroundMessageHandler(async remoteMessage => {
    await getNotification();
});

notifee.onBackgroundEvent(async ({type, detail}) => {
    const {notification, pressAction} = detail;

    if (type === EventType.PRESS) {
        console.log('User pressed an action with the id: ', pressAction.id);
        // navigate here
    }
    await notifee.cancelNotification(notification.id);
    console.log('background-event');
});

AppRegistry.registerComponent(appName, () => App);

RESTnotification.js RESTnotification.js

import messaging from '@react-native-firebase/messaging';
import notifee, {AndroidImportance, EventType} from "@notifee/react-native";
import AsyncStorage from "@react-native-community/async-storage";
import React from "react";

async function requestUserPermission() {
    const authStatus = await messaging().requestPermission();
    const enabled =
        authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
        authStatus === messaging.AuthorizationStatus.PROVISIONAL;
    if (enabled) {
        console.log('Firebase authorization:', authStatus);
    }
}

export const checkToken = async () => {
    const fcmToken = await messaging().getToken();
    if (fcmToken) {
        console.log(fcmToken);
    }
    await AsyncStorage.setItem("fcmToken", fcmToken);
}

export default async function getNotification() {
    const channelId = await notifee.createChannel({
        id: 'important',
        name: 'Important Notifications',
        importance: AndroidImportance.HIGH,
    });

    await notifee.displayNotification({
        title: '<p><b>Verandering bij proces: {naam}</span></p></b></p>',
        body:
            '{user} heeft het proces {procesnaam} afgekeurd',
        data: {
            processId: '12345678'
        },
        android: {
            channelId: 'important',
            importance: AndroidImportance.HIGH,
            smallIcon: "ic_notification",
            //largeIcon: require('../../assets/images/apple-touch-icon.png'),
            pressAction: {
                id: 'default',
                launchActivity: 'default'
            },
        },
    });
}

checkToken();
requestUserPermission();

Are we overlooking something?我们是否忽略了什么?

At the startup you can't use the hook useNavigation as it's still not built.在启动时,您不能使用钩子 useNavigation,因为它还没有构建。

For this kind of use case, you need to use a different approach, the deep linking: https://reactnavigation.org/docs/deep-linking .对于这种用例,您需要使用不同的方法,即深层链接: https : //reactnavigation.org/docs/deep-linking

The basic flow is that you need to add a path on your notification data (for example myapp://user/profile) and move your notification handler inside the Deep Linking configuration.基本流程是您需要在通知数据上添加路径(例如 myapp://user/profile)并将通知处理程序移动到深层链接配置中。

For a practical example you can check this tutorial: https://medium.com/cybermonkey/deep-linking-push-notifications-with-react-navigation-5fce260ccca2有关实际示例,您可以查看本教程: https : //medium.com/cybermonkey/deep-linking-push-notifications-with-react-navigation-5fce260ccca2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM