From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/pushmanager/getsubscription/index.html | 95 ++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 files/zh-cn/web/api/pushmanager/getsubscription/index.html (limited to 'files/zh-cn/web/api/pushmanager/getsubscription') diff --git a/files/zh-cn/web/api/pushmanager/getsubscription/index.html b/files/zh-cn/web/api/pushmanager/getsubscription/index.html new file mode 100644 index 0000000000..1a86444b2d --- /dev/null +++ b/files/zh-cn/web/api/pushmanager/getsubscription/index.html @@ -0,0 +1,95 @@ +--- +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")}}

+
+
+ +
+
+
-- cgit v1.2.3-54-g00ecf