40

I see we can check the capabilities of a mobile browser using https://whatwebcando.today/, but can the hardware APIs be queried when not running on foreground?

I mean... With PWA am I able to build an app that gets hardware info while running in background, just like Octo U, for Android, and posts that info to a web server?

Rafael Oliveira
  • 2,823
  • 4
  • 33
  • 50
  • 1
    Hi Rafael. Just to clarify, are you aiming to develop a PWA where it's task is to retrieve data from the device sensors, and that is should still run even if the browser is in background? – AL. Jun 14 '17 at 06:28
  • @AL. that's right AL. – Rafael Oliveira Jun 14 '17 at 21:52
  • 1
    Since there has a lot been going on in the field of PWAs during the last half year, are there any news about that? I'd like to access the geolocation of a mobile device in standby mode with a PWA and send it to a web service. – seawave_23 Feb 08 '18 at 15:05
  • 1
    @Nadine I haven't been researching PWAs anymore so I don't know whats the current status of the desired feature. – Rafael Oliveira Feb 15 '18 at 13:12

2 Answers2

20

The modern method of running code "in the background" is by using a service worker, either via its push event handler (triggered via an incoming push message), or via its sync event handler (triggered by an automatic replay of a task that previously failed).

It's not currently possible to access the type of hardware sensors that you're asking about from inside a service worker.

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
16

service workers run on an event driven model. This means they only spin up when registered events (browser UI making a network request, push notification and background sync for now). What I think you are asking for is geo-fenching capabilities. AFAIK this is something being discussed to add to the SW model. If not it should be because it would be very valuable for marketing purposes. I know it is being used in native apps, so I think it would be on the radar. GPS is accessible from the front-end and has been for years in the browser. However the user would need to have your site/PWA loaded in the browser.

Chris Love
  • 3,740
  • 1
  • 19
  • 16
  • Is your response Chris limited to mobile devices or should a PWA (e.g., installed from desktop Chrome) receiving a push notification via a service worker start up on the desktop (i.e does the PWA service worker stay alive in the background if the user has closed it)? – SBG May 02 '19 at 10:41
  • they are managed the same, it is device agnostic. Each browser handles how the thread is spun up and spun down. But essentially they are all the same. The amount of time required to spin up a service worker is more dependent on the CPU (device hardware capacity) than anything. What takes 100ms on my i7 may take 500ms on an average phone. But everything is slower on slower processors. – Chris Love May 03 '19 at 00:05
  • @ChrisLove once the site is loaded in the browser on the phone, can the site send GEO location even the user locked the phone and put it back in their pocket? Similarly if the browser was minimised on the phone, can the website still access GPS? – Zaffer Feb 28 '22 at 11:10
  • 1
    Been a while since you posted this. But AFAIK, no. I really do wish this would get implemented. that way I could build an app to track my steps and workouts like Google Fit :) Seems like that would be as simple as getting user permission. there are lots of apps that do that sort of stuff and I have been asked at least a dozen times over the past 4 years to create a fitness tracker type experience in a PWA. – Chris Love Oct 19 '22 at 19:02