应用程序关闭时,ServiceWorker 以新旧样式推送通知

时间:2021-03-17 12:55:51

标签: javascript firebase mobile progressive-web-apps service-worker

我创建了一个带有 firebase 云消息传递的 PWA,并从我的服务器发送消息。在我的电脑上一切正常,在移动设备上,如果应用程序或网站打开,通知也很好,但是当我关闭应用程序时,它会推送两个通知。我的新设计中的一个,比如“你好用户”和旧版本的“你好!”。我不知道该网站从哪里获得旧的消息设计。 我什至创建了一个全新的 firebase 项目和一个新的子域来测试它。当我在移动设备上调试时,没有旧的 serviceworker。是否有可能将一个非常老的 serviceworker 存储在移动文件系统的某个地方,或者我使用的 firebase-cloud-messaging-sw.js 以某种方式将旧版本存储在 firebase 云中(我不使用云来存储某物)。我的软件中没有后台活动。

importScripts('https://www.gstatic.com/firebasejs/8.2.10/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.2.10/firebase-messaging.js');

const staticCacheName = 'site-static-v.1.000';
const dynamicCacheName = 'site-dynamic-v.1.000';
const assets = [
  '/',
  '/manifest.json',
  '/login.html',
  '/img/logo/icon-192x192.png',
  '/img/logo/icon-256x256.png',
  '/img/logo/icon-384x384.png',
  '/img/logo/icon-512x512.png'
];

// cache size limit function
const limitCacheSize = (name, size) => {
  caches.open(name).then(cache => {
    cache.keys().then(keys => {
      if (keys.length > size) {
        cache.delete(keys[0]).then(limitCacheSize(name, size));
      }
    });
  });
};

// install event
self.addEventListener('install', evt => {
  //console.log('service worker installed');
  evt.waitUntil(
    caches.open(staticCacheName).then((cache) => {
      console.log('caching shell assets');
      cache.addAll(assets);
    })
  );
});

// activate event
self.addEventListener('activate', evt => {
  evt.waitUntil(
    caches.keys().then(function (cacheNames) {
      return Promise.all(
        cacheNames.filter(function (cacheName) {
          // Return true if you want to remove this cache,
          // but remember that caches are shared across
          // the whole origin
          return true;
        }).map(function (cacheName) {
          return caches.delete(cacheName);
        })
      );
    })
  );
  //console.log('service worker activated');
  /*evt.waitUntil(
     caches.keys().then(keys => {
      //console.log(keys);
      return Promise.all(keys
        .filter(key => key !== staticCacheName && key !== dynamicCacheName)
        .map(key => caches.delete(key))
      );
    }) 
  );*/
});

// fetch events
self.addEventListener('fetch', evt => {
});

self.addEventListener('push', function (e) {
  console.log(self);
  const data = e.data.json();
  var body;
  //var js = JSON.parse(e.data);
  var n = "";

  var mstype = data.data.mstype;
  var un = data.data.un;
  var t = "Hallo " + un + "! " + data.notification.title;

  if (e.data) {
    //n = JSON.parse(e.data);
    body = data.notification.body + " Typ: " + mstype;

  } else {
    body = 'Push message no payload';
  }

  var options = {
    body: body,
    icon: '/img/logo/icon-192x192.png',
    vibrate: [100, 50, 100],
    data: {
      dateOfArrival: Date.now(),
      primaryKey: 2
    }
  };
  e.waitUntil(
    self.registration.showNotification(t, options)
  );
});



self.addEventListener('notificationclose', function (e) {
  var notification = e.notification;
  var primaryKey = notification.data.primaryKey;

  console.log('Closed notification: ' + primaryKey);
});


self.addEventListener('notificationclick', function (e) {
  console.log("notification click");
  var notification = e.notification;
  var action = e.action;

  var link = "";
  var base = "https://test.url.com/";
  if (action === 'close') {
    notification.close();
  } else {
    clients.openWindow(base + link);
    notification.close();
  }
});

0 个答案:

没有答案