【发布时间】:2017-02-23 20:43:21
【问题描述】:
这是我的 server.js 代码,它使用 web-push 模块发送通知。
exports.sendWelcomePushNotification = function (req, res) {
var details = {
endpoint: req.body.endpoint,
key: req.body.key,
secret: req.body.authSecret
}
webPush.sendNotification(details.endpoint, {TTL: 0, payload: 'You have subscribed to Pushnotification.', userPublicKey: details.key, userAuth: details.secret}).then(function () {
res.sendStatus(200).json({message: 'User successfully subscribed'});
});
};
这是我的浏览器客户端代码,用于捕获端点、身份验证和密钥:
endpoint = subscription.endpoint;
var rawKey = subscription.getKey ? subscription.getKey('p256dh') : '';
key = rawKey ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : '';
var rawAuthSecret = subscription.getKey ? subscription.getKey('auth') : '';
authSecret = rawAuthSecret ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawAuthSecret))) : '';
这是监听通知的 service-worker 代码:
self.addEventListener('push', function (event) {
var payload = event.data ? event.data.text() : 'No Text Body';
console.log(payload,'payload');
event.waitUntil(
self.registration.showNotification('Notify Message', {
lang: 'la',
body: 'You have subscribed to Pushnotification.',
icon: 'https://pushover.net/images/icon-256.png',
requireInteraction: true
})
);
});
此代码适用于 firefox.i.e;当我允许通知时,将发送 api 请求并且网络推送正在将推送通知发送到端点并且我能够获得欢迎推送通知。但是chrome中的相同代码不起作用。 IE;它没有给出任何错误,同时也没有给出欢迎推送通知。
有人可以帮我解决这个问题吗?任何形式的帮助都将受到高度赞赏。 谢谢。
【问题讨论】:
-
这通常是由于您的 GCM api 密钥和您的发件人 ID(在清单中声明)不匹配造成的。检查它们,然后从浏览器取消订阅并重新订阅以推送通知
-
我已将项目编号作为发件人 ID,将服务器密钥作为 GCM API 密钥。
-
您查看过软件日志吗?你在 chrome 中得到了什么吗?
-
没有。我什么也没得到。
标签: node.js google-chrome notifications service-worker web-push