使用 GCM 推送通知发布数据
Posting data with GCM push notification
我正在试用 GCM 推送通知 API。到目前为止一切正常,但我不确定如何 post 附加数据。
我遵循了此页面上的步骤: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
(我的服务人员)
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 中的库之一,它们隐藏了与推送有效负载相关的复杂性,并且它们支持标准的网络推送协议(目前由 Firefox 支持,很快就会在 Chrome) 和专有的 GCM 协议(迟早会在 Chrome 中弃用)。
目前有 Node.js、PHP、Python 和 Java 的库。
我遵循了这个step并且没有改变任何东西。
我尝试 POST
使用 cURL:
curl --request POST --url localhost:8080/api/send-push-msg --header 'content-type: application/json' --data '{"subscription":{"endpoint":"https://fcm.googleapis.com/fcm/send/fKr7EsvRiyA:APA.....[Changes with your data]","keys":{"p256dh":"Your Key","auth":"Your Auth"}},"data":"Your Messages Here","applicationKeys":{"public":"Your Public Key Server","private":"Private Key Server"}}'
对我来说效果很好。 :)
我正在试用 GCM 推送通知 API。到目前为止一切正常,但我不确定如何 post 附加数据。
我遵循了此页面上的步骤: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
(我的服务人员)
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 中的库之一,它们隐藏了与推送有效负载相关的复杂性,并且它们支持标准的网络推送协议(目前由 Firefox 支持,很快就会在 Chrome) 和专有的 GCM 协议(迟早会在 Chrome 中弃用)。
目前有 Node.js、PHP、Python 和 Java 的库。
我遵循了这个step并且没有改变任何东西。
我尝试 POST
使用 cURL:
curl --request POST --url localhost:8080/api/send-push-msg --header 'content-type: application/json' --data '{"subscription":{"endpoint":"https://fcm.googleapis.com/fcm/send/fKr7EsvRiyA:APA.....[Changes with your data]","keys":{"p256dh":"Your Key","auth":"Your Auth"}},"data":"Your Messages Here","applicationKeys":{"public":"Your Public Key Server","private":"Private Key Server"}}'
对我来说效果很好。 :)