--- title: PushManager.getSubscription() slug: Web/API/PushManager/getSubscription tags: - API - PushManager - Service Worker - 实验中的 - 方法 translation_of: Web/API/PushManager/getSubscription ---

{{SeeCompatTable}}{{ApiRef("Push API")}}

{{domxref("PushManager")}} 接口的方法PushManager.getSubscription() 尝试获取已有的推送订阅。

它返回一个 {{jsxref("Promise")}} 用来resolve出一个包含现有订阅的详细信息的{{domxref("PushSubscription")}} 对象。如果不存在已有的推送订阅,返回null。

语法

​PushManager.getSubscription().then(function(pushSubscription) { ... } );

参数

无。

返回值

A {{jsxref("Promise")}} that resolves to a {{domxref("PushSubscription")}} object or null.

例子

这个代码片段来自 push messaging and notification sample. (没有能直接运行的例子.)

// We need the service worker registration to check for a subscription
  navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
    // Do we already have a push message subscription?
    serviceWorkerRegistration.pushManager.getSubscription()
      .then(function(subscription) {
        // Enable any UI which subscribes / unsubscribes from
        // push messages.
        var pushButton = document.querySelector('.js-push-button');
        pushButton.disabled = false;

        if (!subscription) {
          // We aren’t subscribed to push, so set UI
          // to allow the user to enable push
          return;
        }

        // Keep your server in sync with the latest subscriptionId
        sendSubscriptionToServer(subscription);

        showCurlCommand(subscription);

        // Set your UI to show they have subscribed for
        // push messages
        pushButton.textContent = 'Disable Push Messages';
        isPushEnabled = true;
      })
      .catch(function(err) {
        window.Demo.debug.log('Error during getSubscription()', err);
      });
  });

说明

说明 状态 注释
{{SpecName('Push API', '#widl-PushManager-getSubscription-Promise-PushSubscription', 'getSubscription()')}} {{Spec2('Push API')}} 最初的定义

支持的浏览器

{{Compat("api.PushManager.getSubscription")}}