如何为推送通知生成'registrationId'设备 - IONIC

时间:2017-11-21 06:36:13

标签: cordova ionic-framework ionic2

我已成功安装了cordova和离子原生插件ionic cordova plugin add phonegap-plugin-pushnpm install --save @ionic-native/push

在此之后,我在dashboard.tsapp.module.ts

上添加了以下代码
import { Push, PushObject, PushOptions } from '@ionic-native/push';

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

现在我在构造函数中添加了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.
}

我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:0)

在下面写下你的欲望方法

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

window.alert(registration.registrationId)