Search code examples
javascriptgoogle-chromepushsubscriptionweb-push

Web Push re-subscribe push manager chrome


Currently i have push subscription from many chrome browser with like this method,

swr.pushManager.subscribe({userVisibleOnly: true})
    .then(function (subscription) {
        if (subscription) {
            console.log("success::" + JSON.stringify(resp.subscription));
        }
        return resolve(resp);
    })
    .catch(function (err) {
        resp.status = 'subs_error';
        resp.error = '' + err;
        logger.error("Can not re-subscribe for pushes", err);
        return resolve(resp);
    });

But i want to change subscriptions with vapid key registrations like this,

swr.pushManager.subscribe({userVisibleOnly: true, applicationServerKey: base64UrlToUint8Array(<publicApplicationKey>)})
    .then(function (subscription) {
        if (subscription) {
            console.log("success::" + JSON.stringify(resp.subscription));
        }
        return resolve(resp);
    })
    .catch(function (err) {
        resp.status = 'subs_error';
        resp.error = '' + err;
        logger.error("Can not re-subscribe for pushes", err);
        return resolve(resp);
    });

Now our browsers already have subscription like below and i'm getting error "you are already subscribed" when i run bottom code block(with vapid keys).

Subscription.unsubscribe() method doesn't work on Chrome Browsers. How can i resubscribe to pushManager ?


Solution

  • I find answer after trying many different things. I was trying unsubscribe method on chrome console and i got "DOM error", not success. Then i added the below code to swr.pushManager.getSubscription() success callback. I can unsubscribe from push manager.

    subscription.unsubscribe().then(function(successful) {
    // You've successfully unsubscribed
       console.log("unsubscribe:" + successful);
    }).catch(function(e) {
    // Unsubscription failed
       console.logI("unsubscribe:err:" + e);
    })