Cordova 中的后台 Firebase 推送通知
Background Firebase Push Notifications in Cordova
我正在使用 phonegap-plugin-push 在 Cordova 应用程序中接收通知。我正在 android Marsh-mellow 上部署进行测试。
我想在应用程序处于后台时查看并存储通过 Firebase Console 收到的通知的内容。
这是我的代码 -
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady(){
var push = PushNotification.init({ "android": {"senderID": "91254247XXXX"}});
push.on('registration', function(data) {
console.log(data.registrationId);
//document.getElementById("gcm_id").innerHTML = data.registrationId;
});
push.on('notification', function(data) {
alert("On Notification function!!");
// data.message,
// data.title,
// data.count,
// data.sound,
// data.image,
// data.additionalData
console.log("notification event");
console.log(JSON.stringify(data));
alert(JSON.stringify(data));
//Do something
});
push.on('error', function(e) {
alert(e);
});
}
当应用程序处于前台(在屏幕上)时,我会正确接收内容(标题、消息、数据等),并且能够在警告框中看到它们直接在应用程序中。
但是当应用程序处于背景(不在屏幕上但运行在背景中)时,我会在通知区域收到通知。当我点击收到的通知时,它会将我重定向到应用程序中上次打开的页面。
函数push.on('notification', function(data) {}
没有被调用。不显示任何警报消息。有人可以帮助我如何访问通知消息和数据吗?
经过 2 天的研究,我找到了答案。我们必须在数据字段中将 "content-available" 设置为 1。否则,如果应用程序在后台,它不会调用通知块。
目前通过 Google Firebase Console 这是不可能的。
我们必须send our custom payload messages (data or notification or both) seperately using any one of the firebase servers。
详细信息可以在后台通知的plugin's GitHub Page中找到。
我使用了 NodeJs firebase-admin. I followed these setup guidelines and these steps for sending messages,它对我有用。
这是我的工作代码 (NodeJS) -
var admin = require("firebase-admin");
var serviceAccount = require("pushnotificationapp-xxxxx.json"); //this is the service account details generated from server
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://pushnotificationappxxxxx.firebaseio.com/" //your database URL here.
});
var registrationToken = "eYjYT0_r8Hg:APA91bG0BqWbT7XXXXX....."; //registration token of the device
var payload ={
"data": {
"title": 'Test Push',
"message": 'Push number 1',
"info": 'super secret info',
"content-available": '1' //FOR CALLING ON NOTIFACATION FUNCTION EVEN IF THE APP IS IN BACKGROUND
}
};
//send the notification or message
admin.messaging().sendToDevice(registrationToken, payload)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
更新 :
我也使用 php 服务器实现了相同的功能。
这是我使用 HTTP POST 向 FCM 服务器发送通知的工作 php 代码-
<?php
$url = 'https://fcm.googleapis.com/fcm/send';
$data =
array(
"to" => "eYjYT0_r8Hg:APA91bG0BqWbT7XXXXX.....",
"data" => array(
"title"=> 'Test Push',
"message"=> 'Push number 1',
"info"=> 'super secret info',
"content-available"=> '1')
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => array("Content-Type:application/json",
"Authorization:key=AAAA1HfFM64:APA91XXXX...."), //this is the server authorization key from project settings tab in your Firebase Project
'method' => 'POST',
'content' => json_encode($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { echo "Caught an error"; }
else{
echo $result;
}
?>
我遇到了同样的问题,但我添加了 "content-available": '1' 但我的 phone 没有收到任何后台推送。
最后我发现我的 phone 有问题,如下所述:https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#huawei-and-xiaomi-phones
华为和小米手机
这些 phone 有一个特别的怪癖,当应用程序被强制关闭时,您将无法再收到通知,直到应用程序重新启动。为了让您收到后台通知:
在您的华为设备上,转到“设置”>“受保护的应用”> 检查 "My App" 在哪里。
在您的小米上,确保您的 phone 为您的应用启用了 "Auto-start" 属性。
在您的 Asus 上,确保您的 phone 为您的应用启用了 "Auto-start" 属性。
希望对大家有所帮助。
我正在使用 phonegap-plugin-push 在 Cordova 应用程序中接收通知。我正在 android Marsh-mellow 上部署进行测试。
我想在应用程序处于后台时查看并存储通过 Firebase Console 收到的通知的内容。
这是我的代码 -
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady(){
var push = PushNotification.init({ "android": {"senderID": "91254247XXXX"}});
push.on('registration', function(data) {
console.log(data.registrationId);
//document.getElementById("gcm_id").innerHTML = data.registrationId;
});
push.on('notification', function(data) {
alert("On Notification function!!");
// data.message,
// data.title,
// data.count,
// data.sound,
// data.image,
// data.additionalData
console.log("notification event");
console.log(JSON.stringify(data));
alert(JSON.stringify(data));
//Do something
});
push.on('error', function(e) {
alert(e);
});
}
当应用程序处于前台(在屏幕上)时,我会正确接收内容(标题、消息、数据等),并且能够在警告框中看到它们直接在应用程序中。
但是当应用程序处于背景(不在屏幕上但运行在背景中)时,我会在通知区域收到通知。当我点击收到的通知时,它会将我重定向到应用程序中上次打开的页面。
函数push.on('notification', function(data) {}
没有被调用。不显示任何警报消息。有人可以帮助我如何访问通知消息和数据吗?
经过 2 天的研究,我找到了答案。我们必须在数据字段中将 "content-available" 设置为 1。否则,如果应用程序在后台,它不会调用通知块。
目前通过 Google Firebase Console 这是不可能的。
我们必须send our custom payload messages (data or notification or both) seperately using any one of the firebase servers。
详细信息可以在后台通知的plugin's GitHub Page中找到。
我使用了 NodeJs firebase-admin. I followed these setup guidelines and these steps for sending messages,它对我有用。
这是我的工作代码 (NodeJS) -
var admin = require("firebase-admin");
var serviceAccount = require("pushnotificationapp-xxxxx.json"); //this is the service account details generated from server
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://pushnotificationappxxxxx.firebaseio.com/" //your database URL here.
});
var registrationToken = "eYjYT0_r8Hg:APA91bG0BqWbT7XXXXX....."; //registration token of the device
var payload ={
"data": {
"title": 'Test Push',
"message": 'Push number 1',
"info": 'super secret info',
"content-available": '1' //FOR CALLING ON NOTIFACATION FUNCTION EVEN IF THE APP IS IN BACKGROUND
}
};
//send the notification or message
admin.messaging().sendToDevice(registrationToken, payload)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
更新 :
我也使用 php 服务器实现了相同的功能。 这是我使用 HTTP POST 向 FCM 服务器发送通知的工作 php 代码-
<?php
$url = 'https://fcm.googleapis.com/fcm/send';
$data =
array(
"to" => "eYjYT0_r8Hg:APA91bG0BqWbT7XXXXX.....",
"data" => array(
"title"=> 'Test Push',
"message"=> 'Push number 1',
"info"=> 'super secret info',
"content-available"=> '1')
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => array("Content-Type:application/json",
"Authorization:key=AAAA1HfFM64:APA91XXXX...."), //this is the server authorization key from project settings tab in your Firebase Project
'method' => 'POST',
'content' => json_encode($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { echo "Caught an error"; }
else{
echo $result;
}
?>
我遇到了同样的问题,但我添加了 "content-available": '1' 但我的 phone 没有收到任何后台推送。
最后我发现我的 phone 有问题,如下所述:https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#huawei-and-xiaomi-phones
华为和小米手机
这些 phone 有一个特别的怪癖,当应用程序被强制关闭时,您将无法再收到通知,直到应用程序重新启动。为了让您收到后台通知:
在您的华为设备上,转到“设置”>“受保护的应用”> 检查 "My App" 在哪里。 在您的小米上,确保您的 phone 为您的应用启用了 "Auto-start" 属性。 在您的 Asus 上,确保您的 phone 为您的应用启用了 "Auto-start" 属性。
希望对大家有所帮助。