Web推送通知-在chrome上多次获得相同的通知

问题描述 投票:0回答:1

我正在处理推送通知。即使我已经编写了用于结束通知的代码,我也面临多次获得相同通知的问题。

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个回答
0
投票

此代码对我有用。添加async函数

  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;
    },
  };

please check it from
https://github.com/firebase/quickstart-js/issues/71#issuecomment-396958853
© www.soinside.com 2019 - 2024. All rights reserved.