Как получить идентификатор устройства для push-уведомлений OneSignal Ionic 3 - PullRequest
0 голосов
/ 05 июня 2018

Я реализовал функцию push-уведомлений в моем ионном приложении, но оно не работает.Я не получаю регистрационный идентификатор после развертывания приложения.Ниже приведен мой код.

app.component.ts

 constructor(public platform: Platform, public splashScreen: SplashScreen, public menu: MenuController,
 private storage: StorageService,private toast: ToastService, public events: Events, private push: Push,
  private alertCtrl: AlertController, public network: Network, private api: UserService) {
platform.ready().then(() => {

  debugger
  this.pushSetup();


  this.userDetails = this.storage.getData('userDetails');
  this.isAlertShown = false;
  // this.task = setInterval(() => {
  //   this.checkInternet();
  // }, 3000);
  if (this.userDetails != undefined || this.userDetails != null) {
    this.rootPage = 'welcome';
  } else {
    this.rootPage = 'login';
  }
  this.initializeApp();
});

pushSetup () {

console.log("inside pushSetup");

const options: PushOptions = {

  android: {
    senderID: '592660866764',
    forceShow: 'true'

  },
  ios: {
    alert: 'true',
    badge: true,
    sound: 'false'
  }
};
console.log("inside pushSetup 1");
const pushObject: PushObject = this.push.init(options);


pushObject.on('notification').subscribe((notification: any) => console.log('Received a notification', notification));

pushObject.on('registration').subscribe((registration: any) =>console.log('Received a notification', registration));




pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
}

1 Ответ

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

Установите плагины Cordova и Ionic Native:

 ionic cordova plugin add onesignal-cordova-plugin
 npm install --save @ionic-native/onesignal

вставьте в app.module.ts Onesignal

import { OneSignal } from '@ionic-native/onesignal';

    providers: [
    ....
      OneSignal
    ...
    ]

В компоненте приложения вам нужно Идентификатор приложения One Signal и идентификатор проекта Google

in app.component.ts:

import { OneSignal } from '@ionic-native/onesignal';
import { Platform } from 'ionic-angular';

 constructor(public oneSignal: OneSignal,
    private platform: Platform) { }

 onseSignalAppId: string = 'YOUR_ONESIGNAL_APP_ID';
 googleProjectId: string = 'YOUR_GOOGLE_PROJECT_ID';


  if(this.platform.is('cordova')) {
    if (this.platform.is('android')) {
            this.oneSignal.startInit(this.onseSignalAppId, this.googleProjectId);
      }
      else if (this.platform.is('ios')) {
            this.oneSignal.startInit(this.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();

    // AND THIS METHOD RETURN YOUR DEVICES USER_ID

    this.oneSignal.getIds().then(identity => {
      alert(identity.pushToken + ' it's PUSHTOKEN'); 
      alert(identity.userId + 'it's USERID);
    });
}
...