加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 20:53:02 所属栏目:百科 来源:网络整理
导读:这是我的代码,如何在点击通知中的通知或按钮时将用户导航到所需的屏幕. componentWillMount() { OneSignal.addEventListener('received',this.onReceived); OneSignal.addEventListener('opened',this.onOpened); OneSignal.addEventListener('registered',t
这是我的代码,如何在点击通知中的通知或按钮时将用户导航到所需的屏幕.
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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读