The PushManager.getSubscription()
method of the PushManager
interface retrieves an existing push subscription.
It returns a Promise
that resolves to a PushSubscription
object containing details of an existing subscription. If no existing subscription exists, this resolves to a null
value.
PushManager.getSubscription().then(function(pushSubscription) { } );
A Promise
that resolves to a PushSubscription
object or null
.
This code snippet is taken from a push messaging and notification sample. (No live demo is available.)
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.getSubscription()
.then(function(subscription) {
var pushButton = document.querySelector('.js-push-button');
pushButton.disabled = false;
if (!subscription) {
return;
}
sendSubscriptionToServer(subscription);
showCurlCommand(subscription);
pushButton.textContent = 'Disable Push Messages';
isPushEnabled = true;
})
.catch(function(err) {
window.Demo.debug.log('Error during getSubscription()', err);
});
});