当应用程序处于后台时,phonegap-plugin-push on("notification") 事件未触发
phonegap-plugin-push on("notification") event is not firing when app is in background
我在 Ionic2 中使用以下插件进行推送通知
http://ionicframework.com/docs/native/push/
预期行为:
当应用程序关闭时,收到通知,当用户点击通知时,on("notification") 事件应该在应用程序打开后触发。
实际行为:
我成功收到通知。但是当应用程序处于后台或关闭时,当我收到通知并点击通知时,on("notification") 事件没有触发。
科尔多瓦版本 7.0.1
Android版本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') 事件。关闭应用程序时不会触发。
感谢您的时间和支持。
下面是对我有用的代码。当应用程序关闭时,您会收到一条通知。如果您想通过查看一些有效负载数据来处理点击事件。然后看看下面的代码:
pushObject.on('notification').subscribe((notification: any) => {
// this method will be called when you click on notification when app is closed
pushObject.finish()
.then(e => {})
.catch(e => { console.log("ERROR NOTIFICATION",e); })
}).catch(e => {
console.log("ERROR NOTIFICATION",e);
})
您可以通过以下代码查看前景:
pushObject.on('notification').subscribe((data: any) => {
console.log('message -> ' + data.message);
//if user using app and push notification comes
if (data.additionalData.foreground) {
// if application open, show popup
}
else{
//if user NOT using app and push notification comes
}
有关在 Ionic 中发送推送通知的更多详细信息,您可以访问:
https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59
这不是客户端代码的问题。出现此问题是因为通知负载。
来自phonegap-plugin-push的官方文档
Notifications behave differently depending on the foreground/background state of the receiving app and the payload you send to the app.
For instance if you send the following payload:
phonegap-plugin-push/
{
"notification": {
"title": "Test Notification",
"body": "This offer expires at 11:30 or whatever",
"notId": 10
}
}
When your app is in the foreground, any on('notification') handlers you have registered will be called. However, if your app is in the background, the notification will show up in the system tray. Clicking on the notification in the system tray will start the app but your on('notification') handler will not be called as messages that have notification payloads will not cause the plugins onMessageReceived method to be called.
If you send a payload with a mix of notification & data objects like this:
{
"notification": {
"title": "Test Notification",
"body": "This offer expires at 11:30 or whatever",
"notId": 10
},
"data" : {
"surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}
When your app is in the foreground any on('notification') handlers you have registered will be called. If your app is in the background, the notification will show up in the system tray. Clicking on the notification in the system tray will start the app and your on('notification') handler will not be called as messages that have notification payloads will not cause the plugins onMessageReceived method to be called.
My recommended format for your push payload when using this plugin (while it differs from Google's docs) works 100% of the time:
{
"data" : {
"title": "Test Notification",
"body": "This offer expires at 11:30 or whatever",
"notId": 10,
"surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}
When your app is in the foreground any on('notification') handlers you have registered will be called. If your app is in the background, then the notification will show up in the system tray. Clicking on the notification in the system tray will start the app, and your on('notification') handler will be called with the following data:
{
"message": "This offer expires at 11:30 or whatever",
"title": "Test Notification",
"additionalData": {
"surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}
我在 Ionic2 中使用以下插件进行推送通知
http://ionicframework.com/docs/native/push/
预期行为: 当应用程序关闭时,收到通知,当用户点击通知时,on("notification") 事件应该在应用程序打开后触发。
实际行为: 我成功收到通知。但是当应用程序处于后台或关闭时,当我收到通知并点击通知时,on("notification") 事件没有触发。
科尔多瓦版本 7.0.1 Android版本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') 事件。关闭应用程序时不会触发。
感谢您的时间和支持。
下面是对我有用的代码。当应用程序关闭时,您会收到一条通知。如果您想通过查看一些有效负载数据来处理点击事件。然后看看下面的代码:
pushObject.on('notification').subscribe((notification: any) => {
// this method will be called when you click on notification when app is closed
pushObject.finish()
.then(e => {})
.catch(e => { console.log("ERROR NOTIFICATION",e); })
}).catch(e => {
console.log("ERROR NOTIFICATION",e);
})
您可以通过以下代码查看前景:
pushObject.on('notification').subscribe((data: any) => {
console.log('message -> ' + data.message);
//if user using app and push notification comes
if (data.additionalData.foreground) {
// if application open, show popup
}
else{
//if user NOT using app and push notification comes
}
有关在 Ionic 中发送推送通知的更多详细信息,您可以访问:
https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59
这不是客户端代码的问题。出现此问题是因为通知负载。
来自phonegap-plugin-push的官方文档
Notifications behave differently depending on the foreground/background state of the receiving app and the payload you send to the app.
For instance if you send the following payload: phonegap-plugin-push/
{
"notification": {
"title": "Test Notification",
"body": "This offer expires at 11:30 or whatever",
"notId": 10
}
}
When your app is in the foreground, any on('notification') handlers you have registered will be called. However, if your app is in the background, the notification will show up in the system tray. Clicking on the notification in the system tray will start the app but your on('notification') handler will not be called as messages that have notification payloads will not cause the plugins onMessageReceived method to be called.
If you send a payload with a mix of notification & data objects like this:
{
"notification": {
"title": "Test Notification",
"body": "This offer expires at 11:30 or whatever",
"notId": 10
},
"data" : {
"surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}
When your app is in the foreground any on('notification') handlers you have registered will be called. If your app is in the background, the notification will show up in the system tray. Clicking on the notification in the system tray will start the app and your on('notification') handler will not be called as messages that have notification payloads will not cause the plugins onMessageReceived method to be called.
My recommended format for your push payload when using this plugin (while it differs from Google's docs) works 100% of the time:
{
"data" : {
"title": "Test Notification",
"body": "This offer expires at 11:30 or whatever",
"notId": 10,
"surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}
When your app is in the foreground any on('notification') handlers you have registered will be called. If your app is in the background, then the notification will show up in the system tray. Clicking on the notification in the system tray will start the app, and your on('notification') handler will be called with the following data:
{
"message": "This offer expires at 11:30 or whatever",
"title": "Test Notification",
"additionalData": {
"surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}