>我已经成功地安装了Cordova和Ionic native插件ionic cordova plugin add phonegap-plugin-push
和npm 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)