我正在使用Phonegap和Firebase(fcm)接收推送通知。 我正在使用Android 8进行测试。
如果应用程序在前台运行(作为应用程序中的弹出消息),我希望我的设备收到推送通知。 如果该应用已关闭或在后台运行,我希望消息显示为通知,当我单击该通知时,它将在前台打开该应用并调用通知事件功能。
这是到目前为止发生的事情:
打开应用程序时,将通过推送通知并在应用程序中显示-正确
在最小化应用程序时,顶部栏中会显示一个推送通知-正确...但是
当我单击通知消息时,它会打开我的应用程序/将其放在前台,但是未调用“通知”事件侦听器-INCORRECT
关于此问题的请求很多,我已经处理了很多请求,但是没有一个为我的应用程序提供有效的解决方案(大多数是在2016年至2018年之间,而不是在2019年之间)。
从根本上来说,这需要适用于Android。因此,我尝试删除通知有效负载,并且仅使用数据有效负载。但是,这不能解决问题。
这是我当前的有效载荷:
$notification = array
(
'body' => 'test',
'title' => 'test',
"icon" => 'notification_icon',
"content_available" => true,
);
$data = array
(
"title" => "test",
"body" => "test",
"icon" => "notification_icon",
"content_available" => true,
);
$fields = array
(
'registration_ids' => $registrationIds,
'notification' => $notification,
'data' => $data,
"priority" => "high",
"content_available" => true
);
据我了解,有3种发送通知的方式:
1)为“通知” 。如果该应用当前正在运行,则在前台运行;如果关闭或最小化该应用,则在顶部栏中的后台运行(并且该应用的图标在其上方带有一个小徽章,表示一条消息);但是在后台运行时,在应用程序上单击消息时加载,但未调用on通知功能。
2)作为“数据” 。这似乎仅在前台起作用,没有顶部栏消息,并且该应用程序的图标上方没有小徽章。如果应用程序在后台运行,则根本没有推送通知。
3)作为“通知”和“数据” 。在后台时,应用程序图标上方会带有一个徽章。单击顶部的条消息时,将加载应用程序,但不会调用on通知功能。
而且,即使应用程序在后台,即使它收到推送通知,并将该应用程序放在前台,也不会显示通知消息。仅在收到通知时应用程序已经在前台。
这是我的事件监听器:
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
console.log('Received Device Ready Event');
app.setupPush();
},
setupPush: function() {
var push = PushNotification.init({
android: {
senderID: 'XXXXXXXXXX',
iconColor: "#ccc",
alert: true,
badge: true,
icon: 'notification_icon',
sound: true,
vibrate: true,
},
browser: {},
ios: {
sound: true,
vibration: true,
badge: true
},
windows: {}
});
console.log('after init');
push.on('registration', function(data) {
//[edit - script not displayed for this example, but it registers a token to enable push notifications to the device. tested and working]
});
push.on('error', function(e) {
console.log('push error = ' + e.message);
});
push.on('notification', function(data) {
window.alert(data.additionalData); //Test line
if(data.additionalData.foreground){
window.alert(data.title+'\n'+data.message); //Test line
}
if(data.additionalData.coldstart){
window.alert("coldstart"); //Test line
}
showPopup(data,"info");
});
}
};
//Using Sweet Alert to display messages
function showPopup(data,type){
Swal.fire({
type: type,
title: data.title,
text: data.message,
showConfirmButton: true,
imageUrl: data.image,
imageWidth: 400,
imageHeight: 200,
imageAlt: data.title,
animation: true,
});
}
有人对适用于Android 7和8的上述产品有答案吗?
答案 0 :(得分:3)
您的通知发送逻辑是可靠的,但是您的顾虑也是有效的。如果您想阅读有关Google notification vs data notifications
的更多信息,有一些很好的文章对此进行了解释。
推送通知在打开的接收方上具有特殊的本机,您的WebView必须注册这些事件。但是,如果您查看source,则不会处理通知打开事件。除了派生存储库之外,您无能为力。
此外,由于该应用程序被杀死,因此WebView不会收到.on('notification')
事件。您的应用被终止后不会收到通知,Android OS会接收该通知并决定如何处理。
关于您为什么注意到Android 8上的问题的原因,最新版本的Android更加积极地在后台终止应用程序,以节省电量:Doze和Adaptive battery。较新的android应用只是很快就被杀死了。
如果可能的话,我建议您切换到cordova-plugin-firebase
,这是一个维护程度更高的插件。或者,学习一些android并编写自己的插件。
答案 1 :(得分:0)
我的最终答案是要使推送消息既作为通知消息又作为Android设备(包括Android 8和9)的应用程序内消息工作:
将其添加到config.xml或从中进行修改:
<preference name="phonegap-version" value="cli-9.0.0" />
<platform name="android">
<resource-file src="google-services.json" target="app/google-services.json" />
</platform>
<platform name="ios">
<resource-file src="GoogleService-Info.plist" />
</platform>
<plugin name="phonegap-plugin-push" spec="2.1.3">
<variable name="SENDER_ID" value="xxxxxxxxxxxxx" />
</plugin>
另外,在发送数据包数据时,发送“ data”参数和“ notification”参数,并包括“ content_available”标志,例如...
$notification = [
'body' => $message,
'title' => $titlenotification,
"icon" => 'notification_icon',
"content_available" => "1",
];
$data = [
"title" =>$titlenotification,
"message" => $message,
"content_available" => "1",
];
$fields = [
'registration_ids' => $registrationtokens,
'notification' => $notification,
'data' => $data,
"priority" => "high",
"content_available" => true,
];
//send field data like curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
...最后,请确保将google-services.json添加到根文件夹,同时还要添加到www文件夹。否则,当Phonegap Build处理该插件时,您将收到错误消息“插件需要google-services.json。没有该插件,Google Services插件将无法运行。”
如果应用已关闭或在后台,它将显示一条通知消息。当您单击通知时,它将打开应用程序并再次显示该消息。如果您的应用程序在后台,则将其带到前台时也会显示该消息。如果您的应用程序已关闭并且没有单击通知消息,那么您将永远不会在应用程序上看到该消息。