navigator.serviceWorker.ready.then(reg => {
    reg.pushManager.getSubscription().then(function (sub) {
        // check if subscription is not exist and the process is for initial generation
        if (!sub && !onlyRefreshToken) {
            // ask for permission
            Notification.requestPermission()

            //generate subscription object
            reg.pushManager.subscribe({
                userVisibleOnly: true,
                applicationServerKey: applicationServerKey
            }).then(function (subscribe) {
                // send subscription to server to be saved
                // parse string version of the json to get the expected object structure
                sendSubscriptionFunction(JSON.parse(JSON.stringify(subscribe)))
            }).catch(e => {
                console.log(e)
            })
        }
        // check if subscription is not exist but the permission already granted
        else if (!sub && onlyRefreshToken && isPermissionGranted) {
            //re-generate subscription object
            reg.pushManager.subscribe({
                userVisibleOnly: true,
                applicationServerKey: applicationServerKey
            }).then(function (subscribe) {
                // send subscription to server to be saved
                // parse string version of the json to get the expected object structure
                sendSubscriptionFunction(JSON.parse(JSON.stringify(subscribe)))
            }).catch(e => {
                console.log(e)
            })
        }
        // check if subscription is already exist
        else if (!!sub) {
            // send subscription to server to be saved
            // parse string version of the json to get the expected object structure
            sendSubscriptionFunction(JSON.parse(JSON.stringify(sub)))
        }
    })
})