如何使用nodejs向android chrome浏览器发送通知?

Posted

技术标签:

【中文标题】如何使用nodejs向android chrome浏览器发送通知?【英文标题】:how to send notification to android chrome browser using nodejs? 【发布时间】:2020-01-22 04:06:33 【问题描述】:

我想从节点 js 向我的 android 设备的 chrome 浏览器发送推送通知。这可能吗?如果是这样,如何实现?提前致谢。

【问题讨论】:

您使用SNS服务发送通知 【参考方案1】:

看看漂亮的模块web-push 你需要有GCM API key和VAPID key,第一次生成哪个模块帮你搞定。

const webpush = require('web-push');

// VAPID keys should only be generated only once.
const vapidKeys = webpush.generateVAPIDKeys();

webpush.setGCMAPIKey('<Your GCM API Key Here>');
webpush.setVapidDetails(
  'mailto:example@yourdomain.org',
  vapidKeys.publicKey,
  vapidKeys.privateKey
);

// This is the same output of calling JSON.stringify on a PushSubscription
const pushSubscription = 
  endpoint: '.....',
  keys: 
    auth: '.....',
    p256dh: '.....'
  
;

webpush.sendNotification(pushSubscription, 'Your Push Payload Text');

祝你好运!

【讨论】:

谢谢迪玛!我尝试了您的解决方案,它适用于我的桌面,但不适用于我的手机的 chrome 浏览器。有什么解决办法吗? this 也是?

以上是关于如何使用nodejs向android chrome浏览器发送通知?的主要内容,如果未能解决你的问题,请参考以下文章