curl - 使用 GCM 推送通知发布数据

标签 curl google-cloud-messaging push service-worker

我正在试用 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/

相关文章:

notifications - 带有 PHP 后端的 Web 推送通知示例

github - 无法推送到github,远程端意外挂了

curl - 您如何正确索引 elasticsearch 中的数据,找到它,然后删除它,所有这些都使用 CURL?

shell - 来自 shell 的 curl ,带空格的路径

php - 如何使用 PHP DOM 从网页中提取关键字

android - 如何模拟从 Google Cloud Messaging 发送的消息?

iphone - iOS 5.0.1 : SpringBoard crashed after touching the push notification banner

python - 给定一个主机名,如何确定其正确的方案?

android - INSTALL_FAILED_DUPLICATE_PERMISSION... C2D_MESSAGE

swift - GCM 和 Swift 不显示在 iphone 上