这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
PushManager
接口的方法PushManager.getSubscription()
尝试获取已有的推送订阅。
它返回一个 Promise
用来resolve出一个包含现有订阅的详细信息的PushSubscription
对象。如果不存在已有的推送订阅,返回null。
语法
PushManager.getSubscription().then(function(pushSubscription) { ... } );
参数
无。
返回值
A Promise
that resolves to a 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);
});
});
说明
说明 | 状态 | 注释 |
---|---|---|
Push API getSubscription() |
Working Draft | 最初的定义 |
支持的浏览器
BCD tables only load in the browser
这个支持的浏览器列表是由结构化数据生成的。如果你想修改这个表格,请获取最新的代码并提交thttps://github.com/mdn/browser-compat-data 。