Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
Check that serviceWorker exists before unregistering
Browse files Browse the repository at this point in the history
  • Loading branch information
surajpindoria committed Jul 25, 2016
1 parent 63a29cd commit ef00400
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions www/browser/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ var PushNotification = function(options) {
throw new Error('Error subscribing for Push notifications.');
});
}).catch(function(error) {
console.log(error);
throw new Error('Error registering Service Worker');
});
} else {
Expand Down Expand Up @@ -109,20 +110,22 @@ PushNotification.prototype.unregister = function(successCallback, errorCallback,
};
}

serviceWorker.unregister().then(function(isSuccess) {
if (isSuccess) {
var deviceID = subscription.endpoint.substring(subscription.endpoint.lastIndexOf('/') + 1);
var xmlHttp = new XMLHttpRequest();
var xmlURL = (that.options.browser.pushServiceURL || 'http://push.api.phonegap.com/v1/push')
+ '/keys/' + deviceID;
xmlHttp.open('DELETE', xmlURL, true);
xmlHttp.send();

successCallback();
} else {
errorCallback();
}
});
if (serviceWorker) {
serviceWorker.unregister().then(function(isSuccess) {
if (isSuccess) {
var deviceID = subscription.endpoint.substring(subscription.endpoint.lastIndexOf('/') + 1);
var xmlHttp = new XMLHttpRequest();
var xmlURL = (that.options.browser.pushServiceURL || 'http://push.api.phonegap.com/v1/push')
+ '/keys/' + deviceID;
xmlHttp.open('DELETE', xmlURL, true);
xmlHttp.send();

successCallback();
} else {
errorCallback();
}
});
}
};

/**
Expand Down

0 comments on commit ef00400

Please sign in to comment.