react-native – 如何在React Native with One Signal中打开通知屏幕?

前端之家收集整理的这篇文章主要介绍了react-native – 如何在React Native with One Signal中打开通知屏幕?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的代码,如何在点击通知中的通知或按钮时将用户导航到所需的屏幕.
componentWillMount() {
    OneSignal.addEventListener('received',this.onReceived);
    OneSignal.addEventListener('opened',this.onOpened);
    OneSignal.addEventListener('registered',this.onRegistered);
    OneSignal.addEventListener('ids',this.onIds);

    OneSignal.inFocusDisplaying(2);
    OneSignal.requestPermissions({
        alert: true,badge: true,sound: true
    });
}

componentWillUnmount() {
    this.isUnmounted = true;

    OneSignal.removeEventListener('received',this.onReceived);
    OneSignal.removeEventListener('opened',this.onOpened);
    OneSignal.removeEventListener('registered',this.onRegistered);
    OneSignal.removeEventListener('ids',this.onIds);
}

onReceived(notification) {
    console.log("Notification received: ",notification);
}

onOpened(openResult) { // HERE I WANT TO NAVIGATE TO ANOTHER SCREEN INSTEAD OF HOME SCREEN
    this.isNotification = true;

    let data = openResult.notification.payload.additionalData;
    let inFocus = openResult.notification.isAppInFocus;

    console.log('Message: ',openResult.notification.payload.body);
    console.log('Data: ',openResult.notification.payload.additionalData);
    console.log('isActive: ',openResult.notification.isAppInFocus);
    console.log('openResult: ',openResult);
}

onRegistered(notifData) {
    console.log("Device had been registered for push notifications!",notifData);
}

onIds(device) {
    try {
        AsyncStorage.setItem("@SC:deviceInfo",JSON.stringify(device));
    } catch (error) {
        console.log(error);
    }
}

有没有人知道所有这些,React Native OneSignal React Navigation Redux.请帮忙!

为了达到理想的行为,你可以做几件事.您可以手动检查路由器的通知和状态,如果需要将用户重定向到屏幕,或者您可以使用 Deep Linking功能.

要使用深度链接,请在发送时将url参数附加到通知中.要将用户定向到应用中的正确屏幕,您可以使用react-navigation deep linking功能.

From One Signal Documentation

url string The URL to open in the browser when a user clicks on the
notification. Example: 07003

Note: iOS needs https or updated NSAppTransportSecurity in plist

From React Navigation Documentation

Deep Linking

In this guide we will set up our app to handle external URIs. Let’s start with the SimpleApp that we created in the
07004. In this example,we want a URI like
mychat://chat/Taylor to open our app and link straight into Taylor’s chat page.

原文链接:https://www.f2er.com/react/301226.html

猜你在找的React相关文章