这不是客户端代码的问题。出现此问题的原因是通知负载。
来自phonegap-plugin-push 的官方文档
通知的行为有所不同,具体取决于接收应用程序的前台/后台状态以及发送到应用程序的有效负载。
例如,如果您发送以下有效负载:
PhoneGap-插件-推送/
{
"notification": {
"title": "Test Notification",
"body": "This offer expires at 11:30 or whatever",
"notId": 10
}
}
当您的应用程序位于前台时,您注册的任何 on('notification') 处理程序都将被调用。但是,如果您的应用程序在后台运行,通知将显示在系统托盘中。单击系统托盘中的通知将启动应用程序,但您的 on('notification') 处理程序将not作为具有通知负载的消息被调用不会导致调用插件的 onMessageReceived 方法。
如果您发送包含通知和数据对象混合的有效负载,如下所示:
{
"notification": {
"title": "Test Notification",
"body": "This offer expires at 11:30 or whatever",
"notId": 10
},
"data" : {
"surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}
当您的应用程序位于前台时,您注册的任何 on('notification') 处理程序都将被调用。如果您的应用程序在后台运行,通知将显示在系统托盘中。单击系统托盘中的通知将启动应用程序,并且您的 on('notification') 处理程序将not作为具有通知负载的消息被调用不会导致调用插件的 onMessageReceived 方法。
使用此插件时我推荐的推送有效负载格式(虽然它与 Google 的文档不同)100% 有效的时间:
{
"data" : {
"title": "Test Notification",
"body": "This offer expires at 11:30 or whatever",
"notId": 10,
"surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}
当您的应用程序位于前台时,您注册的任何 on('notification') 处理程序都将被调用。如果您的应用程序在后台运行,则通知将显示在系统托盘中。单击系统托盘中的通知将启动应用程序和您的 on('notification') 处理程序will be使用以下数据调用:
{
"message": "This offer expires at 11:30 or whatever",
"title": "Test Notification",
"additionalData": {
"surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}
链接到文档 https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#android-behaviour