如何在本机状态下保存一个信号播放器 ID
How to save one signal player id in react native state
我是 React Native 的新手,我正在使用一个信号进行通知。我从 OneSignal.configure();
中获得了一个信号用户玩家 ID,并遵循了我完成的代码,但我只能在控制台上看到该玩家 ID。我正在使用 setState
保存状态,但它显示 setState is not a function
错误。我如何获取该玩家 ID 以将其保存在状态中。
代码:
componentWillMount() {
OneSignal.init("d447e6e2-0c8e-4781-8292-6e77d2e86691");
OneSignal.configure();
OneSignal.addEventListener('received', this.onReceived);
OneSignal.addEventListener('opened', this.onOpened);
OneSignal.addEventListener('ids', this.onIds);
}
componentWillUnmount() {
OneSignal.removeEventListener('received', this.onReceived);
OneSignal.removeEventListener('opened', this.onOpened);
OneSignal.removeEventListener('ids', this.onIds);
}
onReceived(notification) {
console.log("Notification received: ", notification);
}
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);
}
onIds(device) {
console.log('Device info: ', device);
console.log('player id: ', device.userId);
this.setState({
pid: device.userId,
})
console.log(this.state.pid);
}
事件侦听器函数不知道您的 this
,因此您必须使用 bind
将您的 this
放入其中。
componentWillMount() {
OneSignal.init('ONESIGNAL-APP-ID');
OneSignal.addEventListener('received', this.onReceived.bind(this));
OneSignal.addEventListener('opened', this.onOpened.bind(this));
OneSignal.addEventListener('ids', this.onIds.bind(this));
OneSignal.configure();
}
更新
你也可以声明为箭头函数来避免.bind(this):
componentWillMount() {
OneSignal.init('ONESIGNAL-APP-ID');
OneSignal.addEventListener('received', this.onReceived);
OneSignal.addEventListener('opened', this.onOpened);
OneSignal.addEventListener('ids', this.onIds);
OneSignal.configure();
}
onReceived = () => {}
onOpened = () => {}
onIds = () => {}
你也可以通过这种方式获取玩家id:
const data = await OneSignal.getDeviceState();
const player_id=data.userId;
参考:-
我是 React Native 的新手,我正在使用一个信号进行通知。我从 OneSignal.configure();
中获得了一个信号用户玩家 ID,并遵循了我完成的代码,但我只能在控制台上看到该玩家 ID。我正在使用 setState
保存状态,但它显示 setState is not a function
错误。我如何获取该玩家 ID 以将其保存在状态中。
代码:
componentWillMount() {
OneSignal.init("d447e6e2-0c8e-4781-8292-6e77d2e86691");
OneSignal.configure();
OneSignal.addEventListener('received', this.onReceived);
OneSignal.addEventListener('opened', this.onOpened);
OneSignal.addEventListener('ids', this.onIds);
}
componentWillUnmount() {
OneSignal.removeEventListener('received', this.onReceived);
OneSignal.removeEventListener('opened', this.onOpened);
OneSignal.removeEventListener('ids', this.onIds);
}
onReceived(notification) {
console.log("Notification received: ", notification);
}
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);
}
onIds(device) {
console.log('Device info: ', device);
console.log('player id: ', device.userId);
this.setState({
pid: device.userId,
})
console.log(this.state.pid);
}
事件侦听器函数不知道您的 this
,因此您必须使用 bind
将您的 this
放入其中。
componentWillMount() {
OneSignal.init('ONESIGNAL-APP-ID');
OneSignal.addEventListener('received', this.onReceived.bind(this));
OneSignal.addEventListener('opened', this.onOpened.bind(this));
OneSignal.addEventListener('ids', this.onIds.bind(this));
OneSignal.configure();
}
更新
你也可以声明为箭头函数来避免.bind(this):
componentWillMount() {
OneSignal.init('ONESIGNAL-APP-ID');
OneSignal.addEventListener('received', this.onReceived);
OneSignal.addEventListener('opened', this.onOpened);
OneSignal.addEventListener('ids', this.onIds);
OneSignal.configure();
}
onReceived = () => {}
onOpened = () => {}
onIds = () => {}
你也可以通过这种方式获取玩家id:
const data = await OneSignal.getDeviceState();
const player_id=data.userId;
参考:-