使用GCM推送通知发布数据 [英] Posting data with GCM push notification

查看:171
本文介绍了使用GCM推送通知发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试用GCM推送通知API。到目前为止,它工作正常,但我不知道如何发布额外的数据。



我按照此页面上的步骤: https://developers.google.com/web/fundamentals/getting-started/push-notifications/因此我最后写了一个 curl 请求,如下所示:



<

curl --headerAuthorization:key = myKey--headerContent-Type:application / jsonhttps://android.googleapis.com/ gcm / send -d{\registration_ids\:[myRegistrationId],\add​​itionalData\:{\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 在这个事件?



感谢您阅读!

解决方案

div>

我建议您在 https://github.com/web-push中使用其中一个库-libs ,它们隐藏与推送有效负载相关的复杂性,并且它们支持标准Web推送协议(目前由Firefox支持,Chrome中默认使用)和专有的GCM协议(Chrome将尽快弃用)或以后)。



目前有Node.js,PHP,Python和Java的库。


I'm trying out the GCM push notification API. So far it works fine, but I'm not sure how to post additional data.

I followed the steps on this page: https://developers.google.com/web/fundamentals/getting-started/push-notifications/step-07

So I ended up writing a curl request like this:

curl --header "Authorization: key=myKey" --header "Content-Type: application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[myRegistrationId], \"additionalData\": {\"user_id\":\"1\"}}"

And then my sw.js (my 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'
        }));
});

Is there a way to read out the additionalData in this event? Or am I not supposed to do it like this?

Thanks for reading!

解决方案

I would suggest using one of the libraries in https://github.com/web-push-libs, they hide the complexity associated with push payloads and they support both the standard web push protocol (currently supported by Firefox and soon used by default in Chrome) and the proprietary GCM protocol (which will be deprecated in Chrome sooner or later).

Currently there are libraries for Node.js, PHP, Python and Java.

这篇关于使用GCM推送通知发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆