【问题标题】:Open a custom url when clicking on a web push notification单击网络推送通知时打开自定义 URL
【发布时间】:2017-10-14 16:05:13
【问题描述】:

我正在实施Webpush ruby gem 向我网站的用户发送推送通知。

服务器代码:

  Webpush.payload_send({
      message: notification.message,
      url: notification.url,  # I can't figure out how to access this key
      id: notification.id,    # or this key from the service worker
      endpoint: endpoint,
      p256dh: p256dh_key,
      vapid: vapid_keys,
      ttl: 24 * 60 * 60,
      auth: auth_key,
    })

我在客户端设置了一个服务工作者来显示通知并使其可点击。

self.addEventListener("push", function (event) {
  var title = (event.data && event.data.text()) || "New Message";

  event.waitUntil(
    self.registration.showNotification(title, {
      body: "New push notification",
      icon: "/images/logo@2x.png",
      tag:  "push-notification-tag",
      data: {
        url: event.data.url, // This is returning null
        id: event.data.id // And this is returning null
      }
    })
  )
});

self.addEventListener('notificationclick', function(event) {
  event.notification.close();
  event.waitUntil(
    clients.openWindow(event.data.url + "?notification_id=" + event.data.id)
  );
})

一切正常,除了我通过的自定义键(urlid)无法从服务工作者中访问。

有谁知道如何通过 WebPush gem 传递自定义数据?

【问题讨论】:

  • 我不是这方面的专家,但我会这样做:消息:服务器上的 JSON.stringify(notification),客户端上的 JSON.parse...
  • @Jonasw - 我最终选择了你的解决方案。如果您将其写为答案,我会接受。感谢您的帮助。

标签: javascript notifications web-push


【解决方案1】:

Webpush (with a payload) documentation 看来,您应该使用JSON.stringify() 方法将所有数据放入消息中,并使用JSON.parse() 在Service Worker 中检索它。

服务器:

Webpush.payload_send({
  message:JSON.stringify({
    message: notification.message,
    url: notification.url,
    id: notification.id,
  }),
  endpoint: endpoint,
  p256dh: p256dh_key,
  vapid: vapid_keys,
  ttl: 24 * 60 * 60,
  auth: auth_key,
})

客户:

 event.waitUntil(
   self.registration.showNotification(title, {
   body: "New push notification",
   icon: "/images/logo@2x.png",
   tag:  "push-notification-tag",
   data: {
     url: JSON.parse(event.message).url
   }
})

【讨论】:

    【解决方案2】:

    自定义数据来自 event.notification 对象,而不是直接在 event 中(在 notificationclick 中)。所以如果你想在notificationclick函数中获取自定义数据变量,那么你应该这样做:)

    self.addEventListener('notificationclick', function(event) {
      event.notification.close();
      event.waitUntil(
        clients.openWindow(event.notification.data.url + "?notification_id=" + event.notification.data.id)
      );
    })
    

    【讨论】:

    • 问题是接收来自推送事件的数据,而不是来自点击事件的数据。但你是对的,这是我最终需要修复的另一个错误。
    • @BananaNeil 能否请您提及您是如何解决此问题的“问题是从推送事件接收数据,而不是从点击事件接收数据。”因为我也无法将数据从服务器端发送到客户端......
    • @usama 看看接受的答案,它有服务器/客户端代码
    【解决方案3】:

    在 node_modules/@angular/service-worker/ngsw-worker.js 中的 Angular 项目中添加它

    this.scope.addEventListener('notificationclick', (event) => {
                console.log('[Service Worker] Notification click Received. event:%s', event);
                event.notification.close();
                if (clients.openWindow && event.notification.data.url) {
                    event.waitUntil(clients.openWindow(event.notification.data.url));
                }
            });
    

    你可以输入上面的代码,你可以在文件里面找到这一行

    this.scope.addEventListener('notificationclick', (event) => ..
    

    您必须再次构建 dist 才能使其正常工作。在后端,您需要使用这种格式:

    {"notification":{"body":"This is a message.","title":"PUSH MESSAGE","vibrate":[300,100,400,100,400,100,400],"icon":"https://upload.wikimedia.org/wikipedia/en/thumb/3/34/AlthepalHappyface.svg/256px-AlthepalHappyface.svg.png","tag":"push demo","requireInteraction":true,"renotify":true,"data":{"url":"https://maps.google.com"}}}
    

    您可以在网址内输入您的网址,点击通知后,您的推送通知将打开给定的链接并将其聚焦在浏览器中

    【讨论】:

      【解决方案4】:

      对于Webpush Ruby Gem,经过一些修改,这对我有用:

      Webpush.payload_send(
        endpoint: user.push_subscription.endpoint,
        message: {
          message: "A new service request is created",
          id: service_request.request_number,
          url: "https://google.com/"
        }.to_json,
        p256dh: user.push_subscription.key,
        auth: user.push_subscription.auth_key,
        ttl: 24 * 60 * 60,
        vapid: {
          subject: 'A Push Notification',
          public_key: ENV['VAPID_PUBLIC_KEY'],
          private_key: ENV['VAPID_PRIVATE_KEY']
        }
      )
      

      地点:

      • user.push_subscription.endpointPushSubscription 模型中定义的返回端点的方法
      • user.push_subscription.key, user.push_subscription.auth_key 又是在同一模型中定义的方法

      内部/serviceworker.js.erb

      self.addEventListener("push", (event) => {
        let response = event.data && event.data.text();
        let title = JSON.parse(response).message;
        let body = JSON.parse(response).id;
        let icon = '/assets/logo-blue-120x120.jpg';
      
        event.waitUntil(
          self.registration.showNotification(title, { body, icon, data: { url: JSON.parse(response).url } })
        )
      });
      
      self.addEventListener('notificationclick', function(event) {
        event.notification.close();
        event.waitUntil(
          clients.openWindow(event.notification.data.url)
        );
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-11
        • 1970-01-01
        • 2019-06-11
        • 1970-01-01
        • 2019-05-13
        相关资源
        最近更新 更多