Jam*_*mes 7 push push-notification cordova cordova-plugin-fcm phonegap
我正在使用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
);
Run Code Online (Sandbox Code Playgroud)
据我了解,有3种发送通知的方式:
1)作为“通知”。如果该应用当前正在运行,则在前台运行;如果关闭或最小化该应用,则在顶部栏中的后台运行(并且该应用的图标上面有一个小徽章,表示一条消息);但是在后台运行时,当在应用程序上单击该消息时,加载但未调用on通知功能。
2)作为“数据”。这似乎仅在前景中起作用,没有顶部栏消息,并且该应用程序的图标上方没有小徽章。如果应用程序在后台运行,则根本没有推送通知。
3)作为“ notificaton”和“ data”。在后台时,应用程序图标上方会带有一个徽章。单击顶部条消息时,将加载应用程序,但不会调用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,
});
}
Run Code Online (Sandbox Code Playgroud)
有人对适用于Android 7和8的上述产品有答案吗?
我的最终答案是,要让推送消息同时作为 Android 设备(包括 Android 8 和 9)的通知消息和应用内消息,请执行以下操作:
将其添加到 config.xml 或从 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>
Run Code Online (Sandbox Code Playgroud)
另外,在发送数据包数据时,请发送“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 ) );
Run Code Online (Sandbox Code Playgroud)
...最后,请务必将 google-services.json 添加到根文件夹以及 www 文件夹。否则,当 Phonegap Build 处理它时,您将收到错误“插件需要 google-services.json。没有它,Google 服务插件就无法运行”。
如果应用程序关闭或在后台运行,这将显示一条通知消息。当您单击通知时,它将打开应用程序并再次显示消息。如果您的应用程序位于后台,当您将其带到前台时也会显示该消息。如果您的应用程序已关闭并且您没有单击通知消息,那么您将永远不会在应用程序上看到该消息。
| 归档时间: |
|
| 查看次数: |
776 次 |
| 最近记录: |