服务工作者中的角度推送通知错误,位置0的JSON中的意外令牌P

Fer*_*elo 5 push-notification progressive-web-apps angular-service-worker

我正在尝试在pwa中使用推式通知,但未收到通知。

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

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

我添加了一个图像,其中包含到达服务人员的数据

如上图所示,数据字段为空,但是在发送时我正在添加文本

为了测试该应用程序,我将发布以下通知:

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

我已经很感谢您的贡献。

小智 11

如果您的有效负载采用任何 json 格式,如上面的格式,则不会出现错误,但不会在您的客户端中显示通知,也不会显示其他错误消息。您必须使用某种 json 格式。一种最小的工作负载是:

{ "notification": {"title": "message title", "body": "message body"} }
Run Code Online (Sandbox Code Playgroud)

我还没有找到这个 json 的准确和完整的规范,但在这里(搜索“有效负载”)你会找到一个更完整的例子。


das*_*dsa 2

确保您正确发送有效负载。

ngsw-worker除了无法解析有效负载之外,一切都很好。

如果您使用的是 Windows,请尝试以下操作:

--payload="{\"hello\":\"world\"}"
Run Code Online (Sandbox Code Playgroud)

不是这个:

--payload='{"hello":"world"}'
Run Code Online (Sandbox Code Playgroud)

示例(使用 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
Run Code Online (Sandbox Code Playgroud)