BackgroundFetchUpdateUIEvent

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

注意:此功能僅在 Service Workers 中可用。

BackgroundFetchUpdateUIEvent 介面是 Background Fetch API 的一個事件型別,用於 backgroundfetchsuccessbackgroundfetchfail 事件,並提供一個方法來更新應用的標題和圖示,以便通知使用者後臺獲取的成功或失敗。

Event ExtendableEvent BackgroundFetchEvent BackgroundFetchUpdateUIEvent

建構函式

BackgroundFetchUpdateUIEvent() 實驗性

建立一個新的 BackgroundFetchUIEvent 物件。這個建構函式通常不會被直接使用,因為瀏覽器會為 backgroundfetchsuccessbackgroundfetchfail 事件自行建立這些物件。

例項屬性

還繼承了其父介面 BackgroundFetchEvent 的屬性。

例項方法

還繼承了其父介面 BackgroundFetchEvent 的方法。

BackgroundFetchUpdateUIEvent.updateUI() 實驗性

更新使用者介面中的標題和圖示,以顯示後臺獲取的狀態。返回一個 Promise

示例

在此示例中,會監聽 backgroundfetchsuccess 事件,表明一個獲取已成功完成。然後呼叫 updateUI() 方法,並附帶一條訊息,告知使用者他們下載的劇集已準備就緒。

js
addEventListener("backgroundfetchsuccess", (event) => {
  const bgFetch = event.registration;

  event.waitUntil(
    (async () => {
      // Create/open a cache.
      const cache = await caches.open("downloads");
      // Get all the records.
      const records = await bgFetch.matchAll();
      // Copy each request/response across.
      const promises = records.map(async (record) => {
        const response = await record.responseReady;
        await cache.put(record.request, response);
      });

      // Wait for the copying to complete.
      await Promise.all(promises);

      // Update the progress notification.
      event.updateUI({ title: "Episode 5 ready to listen!" });
    })(),
  );
});

規範

規範
Background Fetch
# background-fetch-update-ui-event

瀏覽器相容性