2017-06-21 109 views
1

我得到this.handleNotification()/ this.setState()不是一個函數在內部使用onReceived(通知){},onOpened(openResult)時發生錯誤{} Onesignal事件我創造了。下面是我的代碼。 任何解決方案。我正在實施或不正確?#react-native - Onesignal implementation

componentWillMount() {  
    OneSignal.addEventListener('received', this.onReceived); 
    OneSignal.addEventListener('opened', this.onOpened); 
} 
componentWillUnmount() { 
    OneSignal.removeEventListener('received', this.onReceived); 
    OneSignal.removeEventListener('opened', this.onOpened); 
} 

onOpened(openResult) { 
    try {  
     this.setState({ msgBody: openResult.notification.payload.body }); 
    } catch (error) { } 
    } 
    onReceived(notification) { 
    OBJ.homeData.setNewMsgCount();  
    this.handleNotification('', '', '');  
    } 

回答

1

你必須結合上下文如下代碼:

OneSignal.addEventListener('received', this.onReceived.bind(this)); 
+1

小心一點 - 'bind'創建並返回新的功能。您需要保留傳遞給'addEventListener'的函數引用,以便您可以將相同的引用傳遞給'removeEventListener'。 'this.onReceived = this.onReceived.bind(this)' Then: 'OneSignal.addEventListener('received',this.onReceived);' –