向特定用户发送推送通知-Ruby on Rails

时间:2018-07-09 16:49:22

标签: ruby-on-rails ruby push-notification web-push

我正在使用宝石webpush向用户发送推送通知。

我在这里遵循了有关Github的教程:https://github.com/zaru/webpush

所有工作都与本教程说明的一样,但是我不知道如何将发送推送通知发送给组或仅特定用户。该示例仅显示了如何在按钮单击时发送推送通知。这是我的代码:

Webpush.payload_send(
message: params[:message],
endpoint: params[:subscription][:endpoint],
p256dh: params[:subscription][:keys][:p256dh],
auth: params[:subscription][:keys][:auth],
vapid: {
  subject: "mailto:sender@example.com",
  public_key: "xxxx",
  private_key: "yyyy"
},
ssl_timeout: 5, # value for Net::HTTP#ssl_timeout=, optional
open_timeout: 5, # value for Net::HTTP#open_timeout=, optional
read_timeout: 5 # value for Net::HTTP#read_timeout=, optional
)

我的Javascript

<script>


   window.vapidPublicKey = new Uint8Array(<%= Base64.urlsafe_decode64("xxxx").bytes %>); 
if (navigator.serviceWorker) {
  navigator.serviceWorker.register('/serviceworker.js')
  .then(function(reg) {
     console.log('Service worker change, registered the service worker');
  });
}
// Otherwise, no push notifications :(
else {
  console.error('Service worker is not supported in this browser');
}

navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
  serviceWorkerRegistration.pushManager
  .subscribe({
    userVisibleOnly: true,
    applicationServerKey: window.vapidPublicKey
  });
});

我的服务人员:

function onPush(event) {
  var title = (event.data && event.data.text()) || "Yay a message";

  event.waitUntil(
    self.registration.showNotification(title, {
      body: "We have received a push message",
      icon: "/assets/path/to/icon.png",
      tag:  "push-simple-demo-notification-tag"
    })
  );
}

self.addEventListener("push", onPush);

0 个答案:

没有答案