在服务人员的角度推送通知的错误,在位置0 JSON意外标记P

问题描述 投票:3回答:2

我试图使用推送通知在PWA,但未能收到通知。

在客户端,我收到以下错误

Uncaught SyntaxError: Unexpected token P in JSON at position 0
at Driver.onPush (ngsw-worker.js:1967)
at ngsw-worker.js:1871

I add an image that includes the data that reaches the service worker

正如你前面的图片中看到,数据字段是空的,但是当我把它我增加了文本

为了测试我解除与以下的通知应用程序:

https://web-push-codelab.glitch.me/

我已经明白任何贡献。

push-notification progressive-web-apps angular-service-worker
2个回答
3
投票

如果您的有效载荷是任何JSON格式,像上面的,误差不会出现,但通知没有在客户端显示并没有显示任何其他错误消息。你必须使用一定的JSON格式。其中最小的工作负载是:

{ "notification": {"title": "message title", "body": "message body"} }

我还没有找到这个JSON准确和完整的规范,但here(搜索“有效载荷”),你会发现一个更完整的例子。


1
投票

请确保您正确发送有效载荷。

一切都很好,除了ngsw-worker无法解析的有效载荷。

如果您使用的是Windows,试试这个:

--payload="{\"hello\":\"world\"}"

而不是这样的:

--payload='{"hello":"world"}'

实施例(使用webpush CLI):

web-push send-notification --endpoint="https://fcm.googleapis.com/fcm/send/xxx:xxx" --key="xxxxxxxxxxxxx" --auth="xxxxxxxxxx" --payload="{\"hello\":\"world\"}" --vapid-subject="https://localhost:8000" --vapid-pubkey=xxxxxxxxxx --vapid-pvtkey=xxxxxxxx
© www.soinside.com 2019 - 2024. All rights reserved.