我已成功安装了cordova和离子原生插件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';
我需要在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.
}
我怎样才能做到这一点?
答案 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)