推送通知未发送到Chrome(桌面级) [英] Push notification is not being sent to chrome(desktop level)

查看:100
本文介绍了推送通知未发送到Chrome(桌面级)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的server.js代码,它使用网络推送模块发送通知.

This is My server.js code which uses web-push module to send the notification.

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

这是我的浏览器客户端代码,用于捕获端点,身份验证和密钥:

This my browser client side code to capture the endpoint, auth and key:

 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))) : '';

这是侦听通知的服务人员代码:

This is the service-worker code that listens for the notification:

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正常工作;当我允许通知时,将发送api请求,并且网络推送会将推送通知发送到端点,并且iam能够获得欢迎推送通知.但是chrome中的相同代码无法正常工作. IE;它没有给出任何错误,同时也没有给出欢迎推送通知.

This code is working fine for firefox.i.e; when i allow a notification, api request is sent and the web push is sending the push notification to the endpoint and iam able to get the welcome push notification. But the same code in chrome is not working. i.e; it is not giving any error and at the same time it is not giving the welcome push notification.

有人可以帮我吗?任何帮助都将受到高度赞赏. 谢谢.

Can someone help me with this? Any kind of help is highly appreciated. Thanks.

推荐答案

如果您使用的是网络推送,则可能与加密数据有关. 尝试使用下面的server.js代码:

If you are using web-push it might have something todo with your encrypted data. Try the below code for server.js:

var webPush = require('web-push');

const VALID_SUBSCRIPTION = {
  endpoint: 'your-endpoint-id',
  keys: {
    p256dh: 'userPublicKey',
    auth: 'your-auth-secret'
 }
};

var payload = {
   title: "This is a title",
   body: "this is the body",
   icon: "images/someImageInPath.png"
}

webPush.setGCMAPIKey("your-gcmapikey");

webPush.sendNotification(VALID_SUBSCRIPTION.endpoint, {
    TTL: 10,
    payload: JSON.stringify(payload),
    userPublicKey: VALID_SUBSCRIPTION.keys.p256dh,
    userAuth: VALID_SUBSCRIPTION.keys.auth,
  })
  .then(function() {
    console.log(JSON.stringify(payload));
  });

这篇关于推送通知未发送到Chrome(桌面级)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆