Skip to content

Instantly share code, notes, and snippets.

@goofmint

goofmint/index.js

Created Jul 2, 2018
Embed
What would you like to do?
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