【问题标题】:Posting data with GCM push notification使用 GCM 推送通知发布数据
【发布时间】:2016-10-24 07:57:53
【问题描述】:

我正在试用 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(我的服务人员)

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?还是我不应该这样做?

感谢阅读!

【问题讨论】:

  • 您可以在推送通知中发布其他数据,但存在数据大小限制。
  • 但是我如何实际发布它并在我的推送事件中阅读它?
  • 服务器端你使用的语言
  • 我的意思是,我在问题中发布了curl 请求。我尝试添加additionalData
  • 参考 Android hive gcm 示例您将获得正确的解决方案

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


【解决方案1】:

我建议使用https://github.com/web-push-libs 中的库之一,它们隐藏了与推送有效负载相关的复杂性,并且它们同时支持标准网络推送协议(Firefox 目前支持并很快在 Chrome 中默认使用)和专有 GCM协议(迟早会在 Chrome 中弃用)。

目前有适用于 Node.js、PHP、Python 和 Java 的库。

【讨论】:

    【解决方案2】:

    我关注了这个step 并没有改变任何东西。

    我用 cURL 尝试了POST

    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"}}'
    

    对我来说效果很好。 :)

    【讨论】:

      猜你喜欢
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      • 2012-11-27
      • 2016-07-24
      • 1970-01-01
      • 2017-01-08
      • 1970-01-01
      相关资源
      最近更新 更多