web使用带有firebase功能的聚合物和firebase的推送通知

时间:2017-10-26 20:04:35

标签: javascript firebase polymer google-cloud-functions polymer-2.x

您好我想从我用聚合物创建的PWA发送网络推送通知,我想使用具有firebase功能的firebase作为“后端”,但我输了,我一直在谷歌搜索,但我找不到一个很好的例子那样做¿你知道使用这些技术的完整例子吗?

非常感谢。

1 个答案:

答案 0 :(得分:1)

您需要在firebase功能中安装 web-push :  npm install web-push --save

然后是如何初始化库并发送通知。

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

参考:https://github.com/web-push-libs/web-push

设置网络推送的详细教程here