
Firebase Messaging Js Sdk Insights : getToken() : Part 1
getToken() first check if the site has permission for showing Notification to the user. For this, it calls getNotificationPermission_() which uses Notification API to get the current permission. The code for getNotificationPermission_() is below.
If the site has been “granted” with permissions to show Notifications, getSWRegistration_() is called. This will register the default service worker and return the registration.
In the above function, the this.registrationToUse value is being set when we call the useServiceWorker() function on the firebase instance. The above function returns default activated service worker or the one that we passed into useServiceWorker(). The waitForRegistrationToActivate_() makes sure for the same. It waits for the service worker it relates to become active.
Coming back to the getToken() method, after getting the Service worker Registration object, it will call getPublicVapidKey_() which returns the default VAPID key or the uint8array version of the public VAPID key provided by the developer.