Skip to content

Instantly share code, notes, and snippets.

@goofmint
Created July 2, 2018 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goofmint/ab5e16e64bb8d82385d4f39738b08267 to your computer and use it in GitHub Desktop.
Save goofmint/ab5e16e64bb8d82385d4f39738b08267 to your computer and use it in GitHub Desktop.
function onLoad() {
if (!('serviceWorker' in navigator)) {
// Service Worker非対応
}
navigator.serviceWorker.register('./serviceworker.js')
.then(() => {
if (!('showNotification' in ServiceWorkerRegistration.prototype)) {
// プッシュ通知がサポートされていない場合
return;
}
if (Notification.permission === 'denied') {
// プッシュ通知を拒否された場合
return;
}
if (!('PushManager' in window)) {
// PushManagerが存在しない場合
return;
}
return navigator.serviceWorker.ready;
})
.then(serviceWorkerRegistration => {
return serviceWorkerRegistration.pushManager.getSubscription();
})
.then(subscription => {
if (!subscription) {
// 未購読
} else {
// すでに購読中
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment