Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Push notification error in service worker, Unexpected token P in JSON at position 0

I am trying to use push notification in a pwa, but it is failing to receive the notification.

On the client side I am receiving the following error

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

As you can see in the previous image, the data field is empty, however when I send it I am adding a text

To test the application I am releasing the notification with the following:

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

I already appreciate any contribution.

like image 792
Fernando Arbelo Avatar asked Dec 26 '18 16:12

Fernando Arbelo


People also ask

How do I fix unexpected token a JSON at position 0?

The "Unexpected token u in JSON at position 0" error occurs when we pass an undefined value to the JSON. parse or $. parseJSON methods. To solve the error, inspect the value you're trying to parse and make sure it's a valid JSON string before parsing it.

What does this mean unexpected token in JSON at position 0?

Re: Unexpected token in JSON at position 0 This usually means that an error has been returned and that's not valid JSON. Check the browser developer tools console and network tabs. Turn on Debugging and (after reproducing the error) check the web server error logs.


1 Answers

If your payload is in any json format, like the one above, the error does not appear, but the notification is not shown in your client and no other error message is shown. You have to use a certain json format. One minimal working payload is:

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

I haven't found the exact and complete spec for this json, but here (search for "payload") you'll find a more complete example.

like image 74
Leonardo Machado Avatar answered Sep 22 '22 14:09

Leonardo Machado