Web Crypto : Decryption (AES-GCM) doesn't work when the application is in background?


I’m currently facing a challenge with the Web Crypto API. While attempting to perform decryption, I’ve noticed that it functions as expected when the application is in the foreground. However, when the application is moved to the background, the decryption process seems to halt, giving the impression that the application has frozen.

Interestingly, when I return the application to the foreground, I receive all the pending notifications at once. This behavior is puzzling and I’m seeking assistance to understand and resolve this issue.

Any help would be greatly appreciated. Thank you.

            .subscribe(async data => {
                this.ngxLogger.info('PUSH: onMessageRecieved:'+JSON.stringify(data));

                let decryptedData = await Promise.resolve(this.decryptionService.decryptCipherData(data.data, data.ets));
                this.decryptedData = decryptedData;

                this.ngxLogger.info('Decrypted Alert ' + this.decryptedData);
            }, errRes => {
                console.log(' Error in receiveing message from Firebasex ' + JSON.stringify(errRes));

   async decryptCipherData(data, ets) {

            var secretKey  = localStorage.getItem('secretKey1');
            var iv = localStorage.getItem('ivParameterSpec1');
            return this.decrypt(data, secretKey, iv);


    async decrypt(data, secretKey, iv) {
        try {
            // Convert iv and ciphertext to Uint8Array
            var ivUint8Array = this.b64ToUint8Array(iv);
            var ciphertextUint8Array = this.b64ToUint8Array(data);

            // Import the CryptoKey
            this.ngxLogger.info('Importing the key!');
            console.log('Importing the key!')
            var key = await Promise.resolve(this.importKey(secretKey)).catch(e);

            // Decrypt using crypto.subtle.decrypt
            var decrypted = await crypto.subtle.decrypt(
                { name: 'AES-GCM', iv: ivUint8Array },

            // Decode the decrypted data
            this.ngxLogger.info('Decode the decrypted data!');
            console.log('Decode the decrypted data!')
            var dec = new TextDecoder();
            var decryptedText = dec.decode(new Uint8Array(decrypted));

            console.log('Decrypted Text:', decryptedText);
            console.log('Decrypted Text:', JSON.stringify(decryptedText));
            return decryptedText;
        } catch (error) {
            console.error('Decryption failed:', error);
            return null;

    async importKey(secretKey) {
        try {
            this.ngxLogger.info('Parse the Base64-encoded key');
            console.log('Parse the Base64-encoded key')
            var keyBuffer = this.b64ToUint8Array(secretKey);

            // Import the key using crypto.subtle.importKey
            this.ngxLogger.info(' Import the key using crypto.subtle.importKey');
            console.log(' Import the key using crypto.subtle.importKey')
            var key = await crypto.subtle.importKey(
                { name: "AES-GCM" },
                ["decrypt", "encrypt"]

            this.ngxLogger.info('Returning the key');
            console.log('Returning the key')
            return key;
        } catch (ex) {
            this.ngxLogger.info('ImportKey error:', ex.name, ', Message:', ex.message);
            console.error('ImportKey error:', ex.name, ', Message:', ex.message);
            return null;

    // Helper function
    b64ToUint8Array(base64string) {
        return Uint8Array.from(atob(base64string), c => c.charCodeAt(0));