Cách tạo lối tắt ứng dụng

François Beaufort
François Beaufort

Lối tắt ứng dụng giúp người dùng nhanh chóng bắt đầu các tác vụ phổ biến hoặc được đề xuất trong ứng dụng web của bạn. Việc dễ dàng truy cập vào các tác vụ đó từ bất kỳ vị trí nào mà biểu tượng ứng dụng hiển thị sẽ giúp nâng cao năng suất của người dùng cũng như tăng mức độ tương tác của họ với ứng dụng web.

Phong cách hiện đại

Xác định lối tắt ứng dụng trong tệp kê khai ứng dụng web

Trình đơn lối tắt ứng dụng được gọi bằng cách nhấp chuột phải vào biểu tượng ứng dụng trong thanh tác vụ (Windows) hoặc đế (macOS) trên màn hình của người dùng hoặc bằng cách chạm và giữ biểu tượng trình chạy của ứng dụng trên Android.

Trình đơn lối tắt ứng dụng đã mở trên Android.
Trình đơn lối tắt ứng dụng đã mở trên Android
Trình đơn lối tắt ứng dụng đang mở trên Windows.
Trình đơn lối tắt ứng dụng đã mở trên Windows

Trình đơn lối tắt ứng dụng chỉ hiển thị cho Ứng dụng web tiến bộ đã cài đặt. Hãy xem phần Cài đặt trong mô-đun Tìm hiểu về PWA để tìm hiểu các yêu cầu về khả năng cài đặt.

Mỗi lối tắt ứng dụng thể hiện một ý định của người dùng, mỗi ý định được liên kết với một URL trong phạm vi của ứng dụng web. URL này được mở ra khi người dùng kích hoạt lối tắt ứng dụng.

Bạn có thể tuỳ ý khai báo lối tắt ứng dụng trong thành phần mảng shortcuts của tệp kê khai ứng dụng web. Dưới đây là ví dụ về một tệp kê khai ứng dụng web tiềm năng.

{
  "name": "Player FM",
  "start_url": "https://player.fm?utm_source=homescreen",
  "shortcuts": [
    {
      "name": "Open Play Later",
      "short_name": "Play Later",
      "description": "View the list of podcasts you saved for later",
      "url": "/play-later?utm_source=homescreen",
      "icons": [{ "src": "/icons/play-later.png", "sizes": "192x192" }]
    },
    {
      "name": "View Subscriptions",
      "short_name": "Subscriptions",
      "description": "View the list of podcasts you listen to",
      "url": "/subscriptions?utm_source=homescreen",
      "icons": [{ "src": "/icons/subscriptions.png", "sizes": "192x192" }]
    }
  ]
}

Hỗ trợ trình duyệt

  • 96
  • 96
  • x
  • 17,4

Nguồn

Cách cổ điển

Nếu ứng dụng chưa được cài đặt, bạn có thể đề xuất người dùng kéo một số liên kết từ trang web của bạn và thả chúng vào thanh dấu trang của trình duyệt. Bằng cách đó, họ có thể nhanh chóng bắt đầu các tác vụ phổ biến hoặc được đề xuất trong ứng dụng web của bạn.

Tài liệu đọc thêm

Bản minh hoạ

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="color-scheme" content="dark light" />
    <link rel="manifest" href="manifest.json" />
    <title>How to create app shortcuts</title>
    <!-- TODO: Devsite - Removed inline handlers -->
    <!-- <script>
      if ('serviceWorker' in navigator) {
        window.addEventListener('load', () => {
          navigator.serviceWorker.register('sw.js');
        });
      }
    </script>
    <script type="module" src="script.js"></script> -->
  </head>
  <body>
    <h1>How to create app shortcuts</h1>
    <ol>
      <li>
        You can drag these <a href="blue.html">blue page</a> or
        <a href="red.html">red page</a> links to the bookmarks bar
        and access them later.
      </li>
      <li>
        Install the app by clicking the button below. After the installation,
        the button is disabled.
        <p>
          <button disabled type="button">Install</button>
        </p>
      </li>
    </ol>
  </body>
</html>

JS


        // The install button.
const installButton = document.querySelector('button');

// Only relevant for browsers that support installation.
if ('BeforeInstallPromptEvent' in window) {
  // Variable to stash the `BeforeInstallPromptEvent`.
  let installEvent = null;

  // Function that will be run when the app is installed.
  const onInstall = () => {
    // Disable the install button.
    installButton.disabled = true;
    // No longer needed.
    installEvent = null;
  };

  window.addEventListener('beforeinstallprompt', (event) => {
    // Do not show the install prompt quite yet.
    event.preventDefault();
    // Stash the `BeforeInstallPromptEvent` for later.
    installEvent = event;
    // Enable the install button.
    installButton.disabled = false;
  });

  installButton.addEventListener('click', async () => {
    // If there is no stashed `BeforeInstallPromptEvent`, return.
    if (!installEvent) {
      return;
    }
    // Use the stashed `BeforeInstallPromptEvent` to prompt the user.
    installEvent.prompt();
    const result = await installEvent.userChoice;
    // If the user installs the app, run `onInstall()`.
    if (result.outcome === 'accepted') {
      onInstall();
    }
  });

  // The user can decide to ignore the install button
  // and just use the browser prompt directly. In this case
  // likewise run `onInstall()`.
  window.addEventListener('appinstalled', () => {
    onInstall();
  });
}