我正在试用 GCM 推送通知 API。到目前为止它工作正常,但我不确定如何发布额外的数据。
我按照此页面上的步骤操作:https://developers.google.com/web/fundamentals/getting-started/push-notifications/step-07
所以我最终写了一个像这样的 curl
请求:
curl --header "Authorization: key=myKey"--header "Content-Type: application/json"https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[myRegistrationId],\"additionalData\": {\"user_id\":\"1\"}}"
然后是我的 sw.js
(我的 service worker)
self.addEventListener('push', function(event) {
console.log('Push message', event);
var title = 'test a';
event.waitUntil(
self.registration.showNotification(title, {
body: 'The Message',
icon: '/assets/img/logo.png',
tag: 'my-tag'
}));
});
有没有办法读出这个事件中的additionalData
?还是我不应该这样做?
感谢阅读!
最佳答案
我建议使用 https://github.com/web-push-libs 中的库之一,它们隐藏了与推送有效负载相关的复杂性,并且它们同时支持标准网络推送协议(protocol)(目前由 Firefox 支持,很快就会在 Chrome 中默认使用)和专有 GCM 协议(protocol)(Chrome 迟早会弃用)。
目前有用于 Node.js、PHP、Python 和 Java 的库。
关于curl - 使用 GCM 推送通知发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37966954/