首页 > 解决方案 > 如何在 React Native 中使用一个信号获取玩家 ID?

问题描述

我正在尝试使用oneSignal。我按照文档中的步骤进行操作。当我运行该应用程序时,它应该根据我的 index.js 中的此代码来控制台设备信息:

constructor(properties) {
    super(properties);
    // OneSignal.init("e353b33e-8b5f-4093-8a86-073b0504b5f2");

    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('registered', this.onRegistered);
    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);
}

当我打开控制台时,它没有显示来自函数 onIds() 的设备信息。

有人可以告诉我我做错了什么吗?我是新手,所以感谢您的帮助

标签: reactjsreact-nativeonesignal

解决方案


我刚刚遇到了这个问题,还不清楚,但如果你查看他们说的文档:

请注意,调用 OneSignal.configure() 会导致 ids 事件触发。

我刚刚在构造函数的末尾添加了 OneSignal.configure(),现在它触发了 onIds()。


推荐阅读