使用GCM推送通知发布数据

3
我正在尝试使用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?或者我不应该这样做吗?
谢谢阅读!

您可以在推送通知中发布其他数据,但是有数据大小限制。 - Yogesh Nikam
但是我应该如何在我的推送事件中实际发布并读取它? - Koeno
你在使用哪种服务器端语言? - Yogesh Nikam
我的意思是,使用我在问题中发布的curl请求。我尝试添加additionalData - Koeno
参考 Android Hive GCM 示例,您将得到适当的解决方案。 - Yogesh Nikam
2个回答

2
我建议使用https://github.com/web-push-libs中的一个库,它们可以隐藏与推送有效负载相关的复杂性,并支持标准的Web推送协议(目前由Firefox支持并很快将成为Chrome的默认选项)和专有的GCM协议(在Chrome中迟早会被弃用)。

目前,已经有针对Node.js、PHP、Python和Java的库可供使用。


0

我按照这个步骤操作,没有做任何更改。

我尝试使用 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"}}'

它对我来说运作得很好。:)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接