Как получить идентификатор игрока OneSignal - PullRequest
0 голосов
/ 12 июня 2018

Мне нужно сохранить идентификатор игрока, когда пользователь принимает уведомления при запуске приложения.Это на функцию initializeApp в app.component.ts.

Я могу получить его playerID (я могу показать его с console.log), но когда я пытаюсь установить его как локальное хранилище, он неРабота.Вот мой app.component.ts:

initializeApp() {
    if (this.platform.ready()) {
      this.platform.ready().then((res) => {
        console.log(res);
        if (res == 'cordova') {
                this.oneSignal.startInit('xxx-xxx-xxx', 'xxx');
                this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.InAppAlert);
                this.oneSignal.getIds().then((ids) => {
                  this.nativeStorage.setItem('playerID', ids.userId);
                });
                this.oneSignal.handleNotificationReceived().subscribe((message) => {
                    // do something when notification is received

                });
                this.oneSignal.handleNotificationOpened().subscribe(() => {
                    // do something when a notification is opened
                });

                this.oneSignal.endInit();

          // console.log(this.oneSignal.startInit('b8320a40-f565-4593-b9aa-d98b580bf8e1', '323509745034'));
          // this.oneSignal.getIds().then(response=>{
          //   console.log(response);
          //     });
          // this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.InAppAlert);
          // this.oneSignal.handleNotificationReceived().subscribe(() => {
          // });
          // this.oneSignal.handleNotificationOpened().subscribe(() => {
          // });
          // this.oneSignal.endInit();
        }
      });
    }
  }

1 Ответ

0 голосов
/ 12 июня 2018

Инициализируйте его OneSignal Когда платформа готова и когда инициализация завершена, вы можете получить идентификатор устройства:

import { Platform, } from 'ionic-angular'; // Import Platform

onseSignalAppId: string = 'xxx-xxx-xxx-xxx-xx-x-x-xxx';
googleProjectId: string = 'xxxxxxxx';

constructor(platform: Platform) {
}

platform.ready().then(() => {
          // Okay, so the platform is ready and our plugins are available.
          // Here you can do any higher level native things you might need.
 statusBar.styleDefault();
 splashScreen.hide();

if (this.platform.is('cordova')) {

  if (this.platform.is('android')) {
    this.oneSignal.startInit('onseSignalAppId', 'googleProjectId');
  }
  if (this.platform.is('ios')) {
    this.oneSignal.startInit('onseSignalAppId');
  }
this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.Notification);

this.oneSignal.handleNotificationReceived().subscribe(() => {
    // do something when notification is received
  });
this.oneSignal.handleNotificationOpened().subscribe(result => {
    // do something when a notification is opened
  });

  this.oneSignal.endInit();

 // Then You Can Get Devices ID

   this.oneSignal.getIds().then(identity => {
       alert(identity.pushToken + " It's Push Token);
       alert(identity.userId + " It's Devices ID");
     });

  });

Просмотреть его Документация

...