如何生成用于推送通知的设备'registrationId' - IONIC



>我已经成功地安装了Cordova和Ionic native插件ionic cordova plugin add phonegap-plugin-pushnpm install --save @ionic-native/push

在此之后,我在dashboard.ts上添加了以下代码并app.module.ts

import { Push, PushObject, PushOptions } from '@ionic-native/push';

我需要在app.module.ts中添加一些imports吗?

现在我在构造函数中添加了private push: Push

现在

OnInit(){
// i need to generate a registerationID in a variable which i can see on a popup //to make sure it is generated.
}

我怎样才能做到这一点?

下面写下你的愿望方法

OnInit()
{
  const options: PushOptions = {
     android: {},
     ios: {
         alert: 'true',
         badge: true,
         sound: 'false'
     },
     windows: {},
     browser: {
         pushServiceURL: 'http://push.api.phonegap.com/v1/push'
     }
  };
  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('Device registered', registration));
  pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
}

在下面的代码中,注册变量仅在该订阅方法中有效。 无论你想用这个变量做什么,你都必须在范围内做

pushObject.on('registration').subscribe((registration: any) => {
console.log('Device registered', registration)
});
window.alert(registration.registrationId)

相关内容

  • 没有找到相关文章

最新更新