【发布时间】:2019-01-12 11:20:57
【问题描述】:
我已经在我的 react native 应用程序上安装了OneSignal package,并希望将通知插入到我的状态中,以便可以在类中访问通知。
所以,到目前为止,我尝试这样做:
import OneSignal from "react-native-onesignal";
export default class SuperScreen extends Component {
constructor(props) {
super(props);
this.state = {
showPopup: false,
pushNotification: null
};
OneSignal.init("<mykey>", {kOSSettingsKeyAutoPrompt: true});
OneSignal.inFocusDisplaying(0);
OneSignal.addEventListener("opened", this.onOpened);
OneSignal.addEventListener("ids", this.onIds);
}
componentWillUnmount() {
OneSignal.removeEventListener("opened", this.onOpened);
}
onOpened(openResult) {
console.log("Message: ", openResult.notification.payload.body);
console.log("Data: ", openResult.notification.payload.additionalData);
console.log("isActive: ", openResult.notification.isAppInFocus);
console.log("openResult: ", openResult);
this.setState({ pushNotification: openResult});
}
但我总是得到 this.setState(...) 不是函数。所以我添加了修改这一行:
this.setState({ pushNotification: openResult}).bind(this);
但是,我仍然得到相同的结果.. 我只想更新状态。你们能解释一下为什么我会收到此错误消息以及如何解决它吗?
诚挚的问候和感谢!
【问题讨论】:
标签: reactjs react-native onesignal setstate