【发布时间】:2019-08-10 05:16:54
【问题描述】:
我正在使用 Phonegap 和 Firebase (fcm) 来接收推送通知。 我正在使用 Android 8 进行测试。
如果应用程序在前台运行,我希望我的设备接收推送通知(作为应用程序中的弹出消息)。 如果应用程序已关闭或在后台,我希望将消息显示为通知,当我单击通知时,它会在前台打开应用程序并调用通知事件函数。
到目前为止发生的情况如下:
当应用打开时,推送通知会出现并显示在应用中 - 正确
当应用程序最小化时,顶部栏中会出现一个推送通知 - 正确...但是
当我单击通知消息时,它会打开我的应用程序/将其置于前台,但未调用“通知”事件侦听器 - 不正确
有很多关于这个问题的帮助请求,我已经处理了很多,但没有一个为我的应用程序提供有效的解决方案(大多数是在 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) 作为“通知”。如果应用程序当前正在运行,这将在前台工作,如果应用程序已关闭或最小化(并且应用程序的图标上方有一个小徽章,指示消息),则在顶部栏中的后台工作,但在后台时,当点击消息时,应用程序加载但未调用通知功能。
2) 作为“数据”。这似乎只在前台工作,没有顶部栏消息,应用程序的图标上方没有小徽章。如果应用程序在后台,则根本没有推送通知。
3) 作为“通知”和“数据”。在后台时,应用程序图标上方有一个徽章。单击顶部栏消息时,应用程序已加载,但未调用通知功能。
此外,即使应用程序在后台,如果它收到推送通知并将应用程序置于前台,也不会显示通知消息。只有在收到通知时应用已经在前台时才有效。
这是我的事件监听器:
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 的答案有答案吗?
【问题讨论】:
-
有些人报告说,如果您从有效负载中排除“通知”对象,它将在前台、后台和作为通知的点击中工作。然而,这似乎对 Android 8 没有任何影响,并且排除通知有效负载也会阻止它在 IOS 中工作。
-
你能告诉我们你是如何创建和设置事件监听器来接收通知的吗?
-
嗨,是的,现在会更新问题。
标签: cordova push-notification push phonegap cordova-plugin-fcm