node.js - 网络推送未知问题,WebPushError "Received unexpected response code"400

标签 node.js google-chrome google-cloud-messaging service-worker web-push

开发过程中web-push 使用 GCM 和 chrome。我最近遇到了这个问题(几个小时前一切仍然正常)

我不确定这是否是由于 GCM 的某些更改或云的停机而发生的...任何信息将不胜感激。

我的代码

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);
});

跟踪:

{
  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' 
}

最佳答案

@Lihn,将您的有效负载粘贴到此处。从错误消息来看,您似乎正在发送格式错误的 json 消息。

您可以使用 this service检查 json 中的语法问题。

GCM docs声明状态代码 400 表示无法解析 json 消息。

关于node.js - 网络推送未知问题,WebPushError "Received unexpected response code"400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37026991/

相关文章:

node.js - 无法执行 Node 命令的二进制文件

javascript - 高效安全的Javascript按位/移位操作

javascript - 在 Node.js 中,为什么第二次调用函数所需的时间比第一次调用要少得多?

javascript - 在多个用户之间共享上传的图像到 Canvas

javascript - 在 ES6 中,如何让 js 文件中的类看起来像 html 文件中定义的那样?

eclipse - 修改后的 CSS3 代码适用于 IE,但不适用于 Chrome

android - GCM 是否需要 google play services 应用程序才能工作?

android - 从 Android Play Services 6.x 中的 GCM 升级到 7.5.x 的后果是什么

android - 在 iOS 和 Android 上处理推送通知场景

node.js - Nodejs上传文件HTTP POST