Cordova 当应用程序位于后台时,phonegap插件推送(“通知”)事件未触发
我在Ionic2中使用以下推送通知插件 预期行为: 当应用程序关闭且收到通知时,以及当用户点击通知时,应用程序打开后应触发打开(“通知”)事件 实际行为: 我已成功收到通知。但当应用程序处于后台或关闭时,当我收到通知并点击该通知时,on(“通知”)事件不会触发 科尔多瓦版本7.0.1 Android版本6.2.3 我的代码:Cordova 当应用程序位于后台时,phonegap插件推送(“通知”)事件未触发,cordova,push-notification,ionic2,cordova-plugins,phonegap-pushplugin,Cordova,Push Notification,Ionic2,Cordova Plugins,Phonegap Pushplugin,我在Ionic2中使用以下推送通知插件 预期行为: 当应用程序关闭且收到通知时,以及当用户点击通知时,应用程序打开后应触发打开(“通知”)事件 实际行为: 我已成功收到通知。但当应用程序处于后台或关闭时,当我收到通知并点击该通知时,on(“通知”)事件不会触发 科尔多瓦版本7.0.1 Android版本6.2.3 我的代码: this.platform.ready().then(() => { this.pushsetup(); }); private pushOptions:
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')事件。当应用程序关闭时,它不会启动
感谢您的时间和支持。下面是我的代码。当应用程序关闭时,您将收到通知。如果您想通过查看一些有效负载数据来处理click事件。然后查看以下代码:
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中发送推送通知的更多详细信息,您可以访问:
这不是客户端代码的问题。发生此问题是因为通知负载。 来自phonegap插件推送的官方文档 根据接收应用程序的前台/后台状态以及您发送给应用程序的负载,通知的行为有所不同 例如,如果您发送以下有效负载: phonegap插件推送/ 当您的应用程序位于前台时,将调用您注册的任何on(“通知”)处理程序。但是,如果您的应用程序位于后台,则通知将显示在系统托盘中。单击系统托盘中的通知将启动应用程序,但您的on('notification')处理程序将被调用,因为具有通知有效负载的消息不会导致调用plugins onMessageReceived方法 如果发送的负载混合了通知和数据对象,如下所示: 当您的应用程序位于前台时,将调用您注册的任何on(“通知”)处理程序。如果您的应用程序位于后台,通知将显示在系统托盘中。单击系统托盘中的通知将启动应用程序,您的on('notification')处理程序将被调用,因为具有通知有效负载的消息不会导致调用plugins onMessageReceived方法 使用此插件时,我推荐的推送负载格式(虽然它不同于谷歌的文档)在100%的时间内都能正常工作。: 当您的应用程序位于前台时,将调用您注册的任何on(“通知”)处理程序。如果您的应用程序位于后台,则通知将显示在系统托盘中。单击系统托盘中的通知将启动应用程序,您的on('notification')处理程序将被调用,其中包含以下数据:
您是否在从后端发送的有效负载数据中设置了“内容可用”:1。是的,我已设置。但仍然没有成功。此外,我还阅读了phonegap插件文档,其中的统计信息如下:“如果用户已终止应用程序,则推送数据将不会进一步处理。”当应用程序关闭时,则无法执行phone gap推送的任何功能。但当它在后台时,您可以通过设置“内容可用”来执行:1.感谢您的关注,您可以在github上共享您的代码吗?服务器和应用程序代码。你给我的建议我试过了,但还是不起作用。我已经创造了一个要点,看看吧。如果您有任何疑问,请随时询问。太好了!我花了一天的时间来解决这个问题,终于解决了我的问题。这对于android来说是完美的,但是更改它会破坏iOS的通知。如果应用程序完全关闭怎么办?比如说,我的通知负载同时包含通知和数据对象。我在这里问了同样的问题嘿@HiteshUpadhyay你找到解决办法了吗?我被困在这里了also@KishanOza嗨,兄弟,我验证的答案是对我有用的。注意,这里的数据结构非常重要。
{
"notification": {
"title": "Test Notification",
"body": "This offer expires at 11:30 or whatever",
"notId": 10
}
}
{
"notification": {
"title": "Test Notification",
"body": "This offer expires at 11:30 or whatever",
"notId": 10
},
"data" : {
"surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}
{
"data" : {
"title": "Test Notification",
"body": "This offer expires at 11:30 or whatever",
"notId": 10,
"surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}
{
"message": "This offer expires at 11:30 or whatever",
"title": "Test Notification",
"additionalData": {
"surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}