7

During the development of web-push with GCM and chrome. I've encountered this issue recently (couple hours ago everything still working fine)

I am not sure if this is happen due to some changes at GCM or a downtime of the cloud... Any information will be appreciated.

My code

var payload = JSON.stringify({
    title: 'notification title',
    body: 'message body'
});
webPush.sendNotification(params.endpoint, {
    userPublicKey: params.userPublicKey,
    userAuth: params.userAuth,
    payload: payload
}).then(function (res) { 
    // console.log of stuff
})
.catch(function(error){
    console.log(error);
    process.exit(1);
});

Trace:

{
  WebPushError: Received unexpected response code
  at IncomingMessage.<anonymous> (/var/www/***/pwa/node_modules/web-push /index.js:264:20)
  at emitNone (events.js:91:20)
  at IncomingMessage.emit (events.js:185:7)
  at endReadableNT (_stream_readable.js:926:12)
  at _combinedTickCallback (internal/process/next_tick.js:74:11)
  at process._tickCallback (internal/process/next_tick.js:98:9)
  name: 'WebPushError',
  message: 'Received unexpected response code',
  statusCode: 400,
  headers: {
    'content-type': 'text/plain; charset=UTF-8',
     date: 'Wed, 04 May 2016 11:35:13 GMT',
     expires: 'Wed, 04 May 2016 11:35:13 GMT',
    'cache-control': 'private, max-age=0',
    'x-content-type-options': 'nosniff',
    'x-frame-options': 'SAMEORIGIN',
    'x-xss-protection': '1; mode=block',
     server: 'GSE',
    'alternate-protocol': '443:quic',
    'alt-svc': 'quic=":443"; ma=2592000; v="33,32,31,30,29,28,27,26,25"',
    'accept-ranges': 'none',
     vary: 'Accept-Encoding',
     connection: 'close'
  },
  body: 'JSON_PARSING_ERROR: Unexpected token END OF FILE at position 467.\n' 
}
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
Linh Pham
  • 3,005
  • 23
  • 34
  • I suggest using services like [pushpad-node](https://github.com/pushpad/pushpad-node) if you don't want to mess with low level details – collimarco May 04 '16 at 16:34
  • Can you show us an example of your payload? – Marco Castelluccio May 04 '16 at 16:46
  • @collimarco Your suggest seem very interesting to me. But sadly the requirement is make a dedicated push module for the project... 3rd service will be denied right away :( – Linh Pham May 04 '16 at 16:53
  • @Marco, I've add the script for generating payload. It is a stringified JSON. – Linh Pham May 04 '16 at 16:56
  • Are you sure you're setting the GCM key correctly? Is there any chance there's a newline character in the GCM key you're setting? – Marco Castelluccio May 04 '16 at 18:15
  • Hey, I'm getting the same error, tried everything possible, is your issue resolved? – anshulix Jun 05 '16 at 08:42
  • @anshulix, Hi, it was busy days, and I did not have any chance to update this question,but for short there are many causes for this to happen.... In my case this, this was cause by the malformed data, when either the `endpoint`, `userPublicKey`, `userAuth` or `payload` got bad encrypted/decrypted. --- In other words, I will suggest you to first check the `payload` make sure it is a "String". Then debug `endpoint`, `userPublicKey` or `userAuth` before execute web-push make sure they are still exactly the same when you received it. – Linh Pham Jun 05 '16 at 15:02
  • @Linh, it shows weird behaviour, worked well for sometime and then suddenly started showing this error again. I think I'll have to roll back to my native GCM implementation. – anshulix Jun 18 '16 at 05:21
  • @Linh, what do you mean by "userPublicKey or userAuth ... make sure they are still exactly the same when you received it" ? ... I'm not receiving them from anywhere, I'm generating them from the subscriptionObject on the frontend. (http://stackoverflow.com/questions/41870838/webpusherror-and-unauthorizedregistration-when-try-to-send-push-notification-to) – pesho hristov Jan 26 '17 at 23:00

2 Answers2

2

@Lihn, paste here your payload. From error the message, it seems like you are sending a malformed json message.

You can use this service to check for syntax problems in your json.

The GCM docs states that a status code 400 means the json message could not be parsed.

joarleymoraes
  • 1,891
  • 14
  • 16
  • Hi! My payload is not a JSON. It is either a stringified JSON or simply a string. Something like this `"notification message"` --- As mentioned in the question on both FireFox and Chrome, all were working well until recently it is broken for GCM on Chrome. --- Still thank you for your time and the answer! :) – Linh Pham May 04 '16 at 13:18
  • Try to set the headers to 'Content-type': 'application/json' ? – joarleymoraes May 04 '16 at 13:25
  • Already did `'Content-type': 'application/json'` , also I've tried without `payload`, `userPublicKey` and `userAuth` none of them work. – Linh Pham May 04 '16 at 13:27
  • 1
    GCM clearly [documents][1] error status 400 as a json that could not be parsed, so my advice is to debug the json string. [1]: https://developers.google.com/cloud-messaging/http-server-ref#downstream-http-messages-plain-text – joarleymoraes May 04 '16 at 13:37
  • Thanks again for the url of documentation, sadly it mention the error only apply for JSON request. And for `web-push` I only use text as payload. --- But I guess I could check the other fields like `enpoint`... etc... --- Please edit your answer and provide the link to GCM documentation, I will upvote it (since it is useful for me). – Linh Pham May 04 '16 at 13:42
  • Is this occuring on a firefox endpoint or Chrome? If it's firefox a non 2XX http status code means there was either a problem contacting the push service or something wrong with the request. If it's Chrome, then I'm not sure, normally GCM returns an error code in the JSON response. – Matt Gaunt May 31 '16 at 09:17
1

If you are trying to send push notifications using web-push, try regenerating the public and private key using #npm run web-push generate-vapid-keys. Try again by replacing these keys. Good Luck.