Nodejs Web推送Chrome推送通知

时间:2017-01-17 06:55:18

标签: javascript node.js web-push

尝试在服务器上的Nodejs中使用webpush将消息从我的服务器推送到Web客户端。

webpush.sendNotification(body, 'Your Push Payload Text').then(console.log);

body是一个带有所需params的字符串,符合: https://github.com/web-push-libs/web-push

{"endpoint":"https://android.googleapis.com/gcm/send/eeXCuiNccV4:APA91bEC-ddg6Iy9OUjtDHzcjFd30cBM_6etyclYe63FrBP_UTCOA_oCwwDhrw-ILp1VqjdQD34X-G4WQ4xNUKnafTz4mGJZcppOsp-_HRNUHRgKXaAdppgClo2JsDAXZQjqxdGd0wq3","keys":{"p256dh":"BE3z-LbXeW65M6xdFhkhbFY9tj_re7RjEIUZDBPOslT1MqIG7-vaxHoUeZY3JOgJ_EaLa1eoifzNX3mno2PgCNc=","auth":"ZYov_VdiE2KZU65wpJWi8Q=="}}

收到此错误:

{ WebPushError: Received unexpected response code
12:42 AM
    at IncomingMessage.<anonymous> (/app/node_modules/web-push/src/web-push-lib.js:274:20)
12:42 AM
    at emitNone (events.js:91:20)
12:42 AM
    at IncomingMessage.emit (events.js:185:7)
12:42 AM
    at endReadableNT (_stream_readable.js:974:12)
12:42 AM
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
12:42 AM
    at process._tickCallback (internal/process/next_tick.js:98:9)
12:42 AM
  name: 'WebPushError',
12:42 AM
  message: 'Received unexpected response code',
12:42 AM
  statusCode: 400,
12:42 AM
  headers: 
12:42 AM
   { 'content-type': 'text/html; charset=UTF-8',
12:42 AM
     date: 'Tue, 17 Jan 2017 06:42:38 GMT',
12:42 AM
     expires: 'Tue, 17 Jan 2017 06:42:38 GMT',
12:42 AM
     'cache-control': 'private, max-age=0',
12:42 AM
     'x-content-type-options': 'nosniff',
12:42 AM
     'x-frame-options': 'SAMEORIGIN',
12:42 AM
     'x-xss-protection': '1; mode=block',
12:42 AM
     server: 'GSE',
12:42 AM
     'alt-svc': 'quic=":443"; ma=2592000; v="35,34"',
12:42 AM
     'accept-ranges': 'none',
12:42 AM
     vary: 'Accept-Encoding',
12:42 AM
     connection: 'close' },
12:42 AM
  body: '<HTML>\n<HEAD>\n<TITLE>UnauthorizedRegistration</TITLE>\n</HEAD>\n<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n<H1>UnauthorizedRegistration</H1>\n<H2>Error 400</H2>\n</BODY>\n</HTML>\n',
12:42 AM
  endpoint: 'https://android.googleapis.com/gcm/send/eeXCuiNccV4:APA91bEC-ddg6Iy9OUjtDHzcjFd30cBM_6etyclYe63FrBP_UTCOA_oCwwDhrw-ILp1VqjdQD34X-G4WQ4xNUKnafTz4mGJZcppOsp-_HRNUHRgKXaAdppgClo2JsDAXZQjqxdGd0wq3' }

1 个答案:

答案 0 :(得分:0)

您收到400错误请求,请确保您的VAPID密钥匹配。

服务器应该同时拥有公钥和私钥,应用程序应该只保存公钥。

确保您只生成一次密钥对。

您可以使用此页面生成密钥对: https://web-push-codelab.glitch.me/