离子2和Onesignal,如何将playerId保存到本地存储



我正在使用ionic2中的一个信号来进行推送通知,我已经测试了插件,并且工作正常,但是我想通过从我的应用程序服务器发送通知来进一步迈出一步。因此,我需要播放器ID,我遵循互联网上的文档,并且能够将PlayerID显示为警报,但我想将其存储在本地存储中。那是挑战

以下是我的代码

 platform.ready().then(() => {
      statusBar.styleDefault();
      splashScreen.hide();
      window["plugins"].OneSignal.getIds(function(ids) {
        alert(JSON.stringify(ids.userId));
        this.storage.set('playersID',  ids.userId);
    });
      // OneSignal Code start:
      // Enable to debug issues:
      // window["plugins"].OneSignal.setLogLevel({logLevel: 4, visualLevel: 4});
      this._OneSignal.startInit("568ed291-8d30-441f-b7be-d0d99aaca596", '342271570971');
      this._OneSignal.inFocusDisplaying(this._OneSignal.OSInFocusDisplayOption.Notification);
      this._OneSignal.setSubscription(true);
      this._OneSignal.handleNotificationReceived().subscribe(() => {
        // handle received here how you wish.
      });
      this._OneSignal.handleNotificationOpened().subscribe(() => {
        // handle opened here how you wish.
      });
      this._OneSignal.endInit();    


    })    

    }
  } 

我什至尝试使用以下代码以无效

    window["plugins"].OneSignal.getIds(function(ids) {
      alert(JSON.stringify(ids));
       window.localStorage.setItem('playerID',  ids.userId);
  });

我还评论了警报,同一故事

您需要在初始化之后保存。您不会在初始化之前获得ID。this._Onesignal.getIds()还返回带有IDS的承诺。它不使用参数。

this._OneSignal.startInit("568ed291-8d30-441f-b7be-d0d99aaca596", '342271570971');
this._OneSignal.inFocusDisplaying(this._OneSignal.OSInFocusDisplayOption.Notification);
this._OneSignal.getIds().then(ids => { //here 
        alert(JSON.stringify(ids.userId));
        this.storage.set('playersID',  ids.userId);
});

最新更新