微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/react/301226.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐