[英]How can I send a Firebase Cloud Messaging notification without use the Firebase Console?
[英]How can I use notification actions with Firebase Messaging Web SDK
人們嘗試這樣做時會遇到一些常見的陷阱。
時間為例。
有效負載的格式應如下所示:
{
"data": {
"some-data": "Im a string",
"some-other-data": "Im also a string",
"json-data": "{\"actions\": [{\"action\":\"yes\", \"title\":\"Yes\"},{\"action\":\"no\",\"title\":\"No\"}]}"
},
"to": "YOUR-IID-TOKEN"
}
您可以像這樣發送curl:
curl -X POST -H "Authorization: key=YOUR-SERVER-KEY" -H "Content-Type: application/json" -d '{
"data": {
"some-data": "Im a string",
"some-other-data": "Im also a string",
"json-data": "{\"actions\": [{\"action\":\"yes\", \"title\":\"Yes\"},{\"action\":\"no\",\"title\":\"No\"}]}"
},
"to": "YOUR-IID-TOKEN"
}' "https://fcm.googleapis.com/fcm/send"
這樣,您就可以在服務工作者的onBackgroundMessage
回調中獲取數據。
在服務工作者中,我們可能具有以下代碼:
messaging.setBackgroundMessageHandler(function(payload) {
console.log('Message received: ', payload);
});
它將在控制台中打印出以下內容:
請注意,JSON數據仍然只是一個字符串,而不是一個對象。
接下來,我們可以解析JSON數據並檢查其正確格式以用作通知動作。
我們可以將代碼更改為以下內容:
messaging.setBackgroundMessageHandler(function(payload) {
console.log('Message received: ', payload);
const parsedJSON = JSON.parse(payload.data['json-data']);
console.log('Actions:', parsedJSON);
});
這將給出以下日志:
這樣,我們最終可以使用以下代碼創建通知:
messaging.setBackgroundMessageHandler(function(payload) {
console.log('Message received: ', payload);
const parsedJSON = JSON.parse(payload.data['json-data']);
console.log('Actions:', parsedJSON);
// Customize notification here
const notificationTitle = 'Actions Title';
const notificationOptions = {
body: 'Actions body.',
actions: parsedJSON.actions,
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
現在,您應該收到有關操作的通知:
正如Meggin在評論中所指出的那樣,如何測試它並不明顯,因此有一些指導原則。
最大的痛點是,如果您的Web服務器為服務工作程序文件設置了緩存頭,則刷新之間不會更新,這是一種修復方法,可以在新標簽頁中打開服務工作文件並刷新該頁面,直到服務人員是最新的(正在查看服務人員的實際源代碼)。 然后,當您刷新網頁時,服務人員將是最新的頁面,並且可以通過服務人員遞增旁邊的數字告訴它已更新。
另外,只需注銷服務工作者的服務工作者並刷新頁面-這應該為您提供最新的服務工作者。
要測試您的通知,您需要在發送推送消息之前單擊用於其他網頁的標簽。
這樣做的原因是,如果用戶當前在您的頁面之一上,則將推送消息發送到頁面onMessage()
回調而不是onBackgroundMessage()
回調。
遵循Matt的建議,我能夠從我的firebase函數中獲取正確的通知,並將其內容傳遞給我的service worker(包括動作),但是我必須將我的所有數據都傳遞給一個json對象,否則它將無法正常工作我。
我的firebase函數代碼如下所示:
function sendPayload(tokenArray) {
const payload = {
"data": {
"jsondata": "{\"body\":\"Meggin needs help\", \"title\":\"Can you help her make the code work?\",\"actions\": [{\"action\":\"yes\", \"title\":\"Yes\"},{\"action\":\"no\",\"title\":\"No\"}]}"
}
};
admin.messaging().sendToDevice(tokenArray, payload)
.then(function(response) {
// See the MessagingDevicesResponse reference documentation for
// the contents of response.
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
}
這是我的代碼在服務工作者中的樣子:
messaging.setBackgroundMessageHandler(function(payload) {
console.log('Payload received: ', payload);
const parsedJSON = JSON.parse(payload.data.jsondata);
console.log("What does actions look like? " + parsedJSON.actions);
console.log("What does title look like? " + parsedJSON.title);
const notificationTitle = parsedJSON.title;
const parsedBody = parsedJSON.body;
const parsedActions = parsedJSON.actions;
// Customize notification here
const notificationOptions = {
body: parsedBody,
actions: parsedActions,
};
return self.registration.showNotification(notificationTitle, notificationOptions);
});
值得注意的是,幫助我通過的一個主要障礙是了解如何測試推送通知和服務人員!
除非關閉瀏覽器,否則您實際上看不到我的通知,因此很明顯,您無法觀看控制台。
但是,一旦您推送了通知,便進入控制台,並將控制台頂部的文件更改為專門用於服務工作者的文件。
然后您可以看到控制台日志!
我意識到這對許多人來說似乎是顯而易見的,但對我而言並非如此,這對於理解如何解析有效負載並使其完成所需的工作至關重要!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.