Skip to content

Instantly share code, notes, and snippets.

@senthilp
Last active August 2, 2018 17:07
Show Gist options
  • Save senthilp/2ab003af5bab7d24996720517344709c to your computer and use it in GitHub Desktop.
Save senthilp/2ab003af5bab7d24996720517344709c to your computer and use it in GitHub Desktop.
Subscription steps
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