Can a progressive web app (PWA) run a background service on a mobile device to get data from hardware (accelerometer, gps…)?

只谈情不闲聊 提交于 2019-12-03 16:56:11

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/44530885/can-a-progressive-web-app-pwa-run-a-background-service-on-a-mobile-device-to-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!