【问题标题】:How to get OneSignal Push Notification Device ID Ionic 3如何获取 OneSignal 推送通知设备 ID Ionic 3
【发布时间】:2018-11-14 16:12:53
【问题描述】:

我已在我的 ionic 应用程序中实现了推送通知功能,但它无法正常工作。部署应用程序后,我没有获得注册 ID。以下是我的代码。

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));
}

【问题讨论】:

标签: ionic-framework push-notification ionic3


【解决方案1】:

安装 Cordova 和 Ionic Native 插件:

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

插入app.module.tsOnesignal

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

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

在应用组件中你需要One Signal App IDGoogle Project ID

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);
    });
}

【讨论】:

  • 感谢您的建议和回答。我有我的谷歌项目 ID,但我没有 onesignalid。
  • 最后一个方法 this.oneSignal.getIds() 会返回一个信号ID
  • 首先你需要创建一个OneSignal App
  • @Gurbela 我可以在 home.ts 页面中获取 ID 吗?我想每次用户访问主页时检查。有可能吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 2023-03-16
  • 2017-10-29
  • 2017-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多