【问题标题】:web push notification - getting same notifications multiple times on chrome网络推送通知 - 在 chrome 上多次获取相同的通知
【发布时间】:2019-07-03 13:55:32
【问题描述】:

我正在处理推送通知。即使我编写了关闭通知的代码,我也面临多次收到相同通知的问题。

self.addEventListener('push', async (event) => {
  if (event.data) {
    const [data] = event.data.json()
    const body = `Notification: ${data.timestampV} - ${data.eventType}`
    const title = 'Web Notification'
    const options = {
      body,
      icon: 'images/icon.png',
      badge: 'images/badge.png',
    }

    event.waitUntil(self.registration.showNotification(title, options))
  } else {
    console.log('This push event has no data.')
  }
})

self.addEventListener('notificationclick', function (event) {
  event.notification.close()

  // close all notifications
  self.registration.getNotifications().then(function (notifications) {
    notifications.forEach(function (notification) {
      notification.close();
    });
  });
  // eslint-disable-next-line no-undef
  clients.openWindow('/feeds')
})

提前致谢。

【问题讨论】:

    标签: push-notification notifications web-push


    【解决方案1】:

    此代码对我有用。 添加异步功能

      var newEvent = new Event('push');
      newEvent.waitUntil = e.waitUntil.bind(e);
      newEvent.data = {
        json: async function () {
          var newData = e.data.json();
          newData.notification = newData.data;
          self.registration.showNotification(newData.data.title, {
            ...newData.data,
            data: newData.data
          });
          return newData;
        },
      };
    

    请查看 https://github.com/firebase/quickstart-js/issues/71#issuecomment-396958853

    【讨论】:

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