Last active
August 2, 2018 17:07
-
-
Save senthilp/2ab003af5bab7d24996720517344709c to your computer and use it in GitHub Desktop.
Subscription steps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function subscribe(syncToken = false) { | |
try { | |
// STEP 1: Load and init firebase SDK | |
await loadAndInitFirebase('firebase'); | |
const messaging = firebase.messaging(); | |
// STEP 2: Use registered Service Worker | |
const swReg = await getServiceWorkerRegistration(); | |
messaging.useServiceWorker(swReg); | |
// STEP 3: Request user permission | |
// If called only for syncing token, the user has already granted permission | |
if (!syncToken) { | |
await requestPermission(messaging); | |
} | |
// STEP 4: Retrieve token and sync it with server | |
const token = await getToken(messaging); | |
await sendTokenToServer(token); | |
// STEP 5: Finally emit success event | |
emitEvent('subscribe-success'); | |
} catch (ex) { | |
// All errors have been handled by individual functions | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment