prabhakar
prabhakar

Reputation: 11

#react-native - Onesignal implementation

I'm getting this.handleNotification() / this.setState() is not a function error when using inside onReceived(notification) {}, onOpened(openResult) {} Onesignal events i have created. below is my code. Any solution for this. I'm implementing correctly or not ?

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('', '', '');     
  }

Upvotes: 1

Views: 309

Answers (1)

Piyush Dhomne
Piyush Dhomne

Reputation: 41

You have to bind the context as code below:

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

Upvotes: 1

Related Questions