javascript - 两名服务人员排成一列

标签 javascript firebase firebase-cloud-messaging service-worker

我不知道我这样做是否正确,或者还有其他方法可以做到这一点。我有一个用于缓存的 Service Worker,我也需要实现 Firebase Cloud 消息传递,这需要一个 Service Worker。我的第一个 Service Worker 位于我的项目的根目录中,其中有这个。

importScripts('https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js');
if (workbox) {
  workbox.precaching.precacheAndRoute([
  {
    "url": "icon-128x128.png",
    "revision": "af75a93ca2c343f1129dd7ee02fd4da7"
  },
  {
...
 }
]);



workbox.routing.registerRoute(
 /^\/$/,
  new workbox.strategies.NetworkFirst({
    cacheName: 'static-resources',
  })
);

workbox.routing.registerRoute(
 /^\/([^/]+\.htm)$/,
  new workbox.strategies.NetworkFirst({
    cacheName: 'static-resources',
  })
);


/**

  workbox.routing.registerRoute(
  /\.(?:js|css)$/,
  new workbox.strategies.StaleWhileRevalidate({
    cacheName: 'static-resources',
  })
);
**/

  workbox.routing.registerRoute(
  /\.(?:js|css)$/,
  new workbox.strategies.StaleWhileRevalidate({
    cacheName: 'static-resources',
  })
);




workbox.routing.registerRoute(
  // Cache image files.
  /\.(?:png|jpg|jpeg|svg|gif)$/,
  // Use the cache if it's available.
  new workbox.strategies.CacheFirst({
    // Use a custom cache name.
    cacheName: 'image-cache',
    plugins: [
      new workbox.expiration.Plugin({
        // Cache only 50 images.
        maxEntries: 50,
        // Cache for a maximum of a week.
        maxAgeSeconds: 7 * 24 * 60 * 60,
      })
    ],
  })
);
 console.log(`Yay! Workbox is loaded 🎉`);

} else {
  console.log(`Boo! Workbox didn't load 😬`);
}

现在我还需要 FCM 的服务人员,其中应该有这个

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

if (firebase) {

// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
   'messagingSenderId': '14******4'
});

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();

messaging.setBackgroundMessageHandler(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  // Customize notification here
  const notificationTitle = 'Background Message Title';
  const notificationOptions = {
    body: 'Background Message body.',
    icon: '/icon-128x128.png'
  };

  return self.registration.showNotification(notificationTitle,
      notificationOptions);
});
}

我将它们组合成一个服务工作人员,但似乎存在问题,因为它注册了 sw.js 这是我拥有的服务工作人员的名称,并且还尝试注册 firebase-messaging-sw.js 失败,因为我什至没有该文件。

从文档中可以看出

One subtlety with the register() method is the location of the service worker file. You'll notice in this case that the service worker file is at the root of the domain. This means that the service worker's scope will be the entire origin. In other words, this service worker will receive fetch events for everything on this domain. If we register the service worker file at /example/sw.js, then the service worker would only see fetch events for pages whose URL starts with /example/ (i.e. /example/page1/, /example/page2/).

现在看来我需要在根目录中拥有用于缓存和 fcm 的服务工作人员。我该怎么做?

我用它来注册服务人员

<script>
  if ('serviceWorker' in navigator) {
     console.log('Service Worker is supported');
    window.addEventListener('load', () => {
      navigator.serviceWorker.register("/sw.js")
        .then(registration => {

          console.log(`Service Worker registered! Scope: ${registration.scope}`);
        })
        .catch(err => {
          console.log(`Service Worker registration failed: ${err}`);
        });
    });
  }
</script>

最佳答案

嗯,我刚刚将两个工作人员的服务工作人员文件重命名为 firebase-messaging-sw.js 并且它注册了这两个工作人员。它对我来说就是这样。

关于javascript - 两名服务人员排成一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57929739/

相关文章:

android - FCM - 发送到主题和 "Not Registered"错误

android - 传递消息 : ServiceIntent not found 时出错

iOS Firebase 推送通知未使用 obj c 在设备中接收

javascript - 在网站的新页面上保持 jquery 下拉菜单打开

ios - 与 firebase 规则混淆(读写)

android - 实现 Firebase 实时数据库时,我应该创建一个契约(Contract)类吗?

java - 如何限制用户在数据库中多次写入

javascript - JW Player 准备就绪

javascript - 使用javascript在浏览器中修改url?

javascript - 1/x 的逆关系?