[英]Service worker data with curl
在Chrome推送通知API中,我想設置一個可自定義的通知。
我說的是服務工作者。
這是示例服務工作者的代碼。
self.addEventListener('push', function(event) {
var data = {};
if (event.data) {
data = event.data.json();
}
var title = data.message || "Something Has Happened";
var body = 'We have received a push message !';
var icon = '/images/icon-192x192.png';
var tag = 'simple-push-demo-notification-tag';
console.log(event);
event.waitUntil(
self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag
})
);
});
我該如何從curl消息中捕獲數據,如下所示:
curl --header "Authorization: key=---personnal_key---" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"---ID---\"],\"title\":\"ok\"}"
我確實讀過示例,但是看不到有人在談論CURL和服務工作者的結合。 那有可能嗎?
在推送通知api的當前版本中,您可能不會發送數據(請參閱: http : //updates.html5rocks.com/2015/03/push-notificatons-on-the-open-web )。 目前,發送特定數據的唯一方法是將其存儲在服務器上的某個數據庫中,然后讓服務工作者從服務器中獲取它。 但是,正如您期望的那樣,這可能會導致許多安全問題。 一種選擇是使用訂閱進行驗證。 您可以通過以下方式從服務工作者獲取訂閱ID:
registration.pushManager.getSubscription().then(function(ps) {
console.log(ps.subscriptionId)
})
然后,假設您是如何設置服務器的,則可以從服務工作者獲取到服務器。 它可能看起來像這樣
fetch("/"+subscriptionId+"/getLatest").then(function(res) {
res.json().then(function(data) {
self.registration.showNotification(data.title, {
body: data.body,
icon: data.icon,
tag: data.tag
})
})
})
但是,所有這些都需要您設置外部服務器。 您無法通過cURL請求發送數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.