【发布时间】:2018-02-20 21:56:07
【问题描述】:
我在 Ionic2 中使用以下插件进行推送通知
http://ionicframework.com/docs/native/push/
预期行为: 当应用关闭时,收到通知,当用户点击通知时,应用打开后应该触发 on("notification") 事件。
实际行为: 我正在成功收到通知。但是当应用程序在后台或关闭时,当我收到通知并点击通知时,on("notification") 事件不会触发。
Cordova 版本 7.0.1 安卓版本 6.2.3
我的代码:
this.platform.ready().then(() => {
this.pushsetup();
});
private pushOptions: PushOptions;
private pushObject: PushObject;
pushsetup() {
// to check if we have permission
this.push.hasPermission()
.then((res: any) => {
if (res.isEnabled) {
console.log('We have permission to send push notifications');
// configuration of push notification
this.pushOptions = {
android: {
senderID: 'XXXXXXXXXXX',
icon: 'icon_notification'
},
ios: {
alert: 'true',
badge: true,
sound: 'false',
senderID: 'XXXXXXXXXXX'
},
windows: {}
};
this.pushObject = this.push.init(this.pushOptions);
// attach push events
this.storage.get('isPushRegistered')
.then(isPushRegistered => {
if( !isPushRegistered ){
this.pushObject.on('registration').subscribe((registration: any) => {
console.log('Device registered', registration)
this.storage.set('isPushRegistered', true)
});
}
})
this.pushObject.on('notification').subscribe((notification: any) => {
console.log('Received a notification', notification)
});
this.pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
}
});
}
所以,在我的代码中,您可以看到 this.pushObject.on('notification') 事件。应用关闭时不会触发。
感谢您的时间和支持。
【问题讨论】:
-
您是否在后端发送的有效负载数据中设置了“content-available”:1。
-
是的,我已经设置好了。但仍然没有成功。另外,我已经阅读了 phonegap 插件文档,其中统计如下:“如果用户杀死了应用程序,则不会对推送数据进行进一步处理。” github.com/phonegap/phonegap-plugin-push/blob/master/docs/…
-
当app关闭时,您无法执行phone-gap-push的任何功能。但是当它在后台时,您可以通过设置“content-available”来执行:1.
标签: cordova push-notification ionic2 cordova-plugins phonegap-pushplugin