[英]Firebase push notifications with cordova foreground and background n
[英]Background Firebase Push Notifications in Cordova
我正在使用phonegap-plugin-push在Cordova應用程序中接收通知。 我正在Android Marsh-mellow上進行測試。
當應用程序在后台運行時,我想查看和存儲通過Firebase控制台收到的通知的內容。
這是我的代碼-
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控制台進行 。
我們必須使用任何一台Firebase服務器單獨發送自定義有效負載消息 (數據或通知或兩者)。
詳細信息可以在插件的GitHub頁面上的后台通知中找到。
我使用了NodeJs firebase -admin 。 我遵循了這些設置准則和這些發送消息的步驟 ,它對我有用。
這是我的工作代碼(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服務器實現了相同的功能。 這是我的工作php代碼,用於使用HTTP POST向FCM服務器發送通知-
<?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;
}
?>
我遇到了同樣的問題,但是我添加了“內容可用”:“ 1”,但是我的手機沒有收到任何背景信息。
最終我發現這是我的手機出現的問題,如下所述: https : //github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#huawei-and-xiaomi-phones
華為和小米手機
這些電話有一個特別的怪癖,那就是在強制關閉該應用程序后,您將無法再收到通知,直到重新啟動該應用程序為止。 為了讓您接收后台通知:
在您的華為設備上,轉到設置>受保護的應用>選中“我的應用”。 在小米上,確保您的手機已為您的應用啟用了“自動啟動”屬性。 在華碩上,請確保您的手機已為您的應用啟用了“自動啟動”屬性。
希望它會幫助某人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.