Wake-lock works in browser, but not as PWA
Asked Answered
I

1

10

I have a web app that uses wake lock (see below) - I have registered the origin trial and added the code to my index.html. - see https://lucid-franklin-4016c2.netlify.com/

When I load the page in the latest version of Android Chrome the phone stays awake, and the text-to-speech messages I use for this running app work fine.

I've also set it up as a PWA (service worker, manifest,... - all good in lighthouse). However, after "add page to home screen" and loading the PWA from there the phone does go to sleep. It continues to count time and can play sound files in the background but text-to-speech is deactivated.

Is this expected behaviour?

My key need is text-to-speech rather than to keep the phone awake as such, as geolocation seems to work while the phone is asleep anyway. Is there something else I could be doing?

// The wake lock sentinel.
let wakeLock = null;

// Function that attempts to request a wake lock.
export const requestWakeLock = async cb => {
    try {
        wakeLock = await navigator.wakeLock.request("screen");
        wakeLock.addEventListener("release", () => {
            console.log("Wake Lock was released");
        });
        console.log("Wake Lock is active");
    } catch (err) {
        console.error(`${err.name}, ${err.message}`);
        cb({
            tag: "error",
            payload: "wakeLock not supported"
        });
    }
};
Iberian answered 26/1, 2020 at 10:59 Comment(0)
P
2

Chrome have recently promoted Wake Lock API out of origin trial in version 84 and seems to work well for Chrome on Android PWA.

Demo: https://reillyeon.github.io/scraps/wakelock.html

Pren answered 15/7, 2020 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.