I am looking for a solution to fetch geolocation periodically (every 1 minute) on mobile browser. I did some research and came to know that fetching geolocation in background (when phone is locked or webapp not active) is impossible. In foreground it works ok.
I am making a web app where I fetch user location and send it to my server. All works ok, until the point where user locks his/her phone. I tried many things for workarounds:
- setInterval to get geolocation, works fine in foreground but fails in background
- converted my web app into Android using cordova, but same problem existed
- used https://github.com/mauron85/cordova-plugin-background-geolocation this plugin for android
- works good in both foreground and background
- but the same plugin does not work in iPhone (I did not tested it, I searched in Google before moving ahead)
- Its not good to release my app with just one platform (android) support, thus this workaround also failed for me.
I also considered using serviceworkers for my web app but it seems issue persist for background https://github.com/w3c/ServiceWorker/issues/745
I have another solution in mind, but before investing time in it, I wish to know if someone has tried it already.
I learnt about Push notifications:
- Web app client registers to Push notification
- It sends subscriber object to my server
- My server using this object sends message to Push notification server
- Push notification server sends messages to my web app client
- My web app client wakes up my service worker for a brief period to show the notification message
- As per google documentation:
Note: In the current implementation of Chrome, whenever we receive a push message and we don't have our site visible in the browser we must display a notification. That is, we can't do it silently without the user knowing. If we don't display a notification the browser automatically creates one to let the user know that the app is doing work in the background.
I won't mind showing a permanent notification to users until my app is running. I am ready for this trade-off.
Now, my question is, at step 5, when I wake up the service worker **is there a chance I call my main.js (main web app) which might be running in background **, will fetch the geolocation and update it to my server?
Has someone tried this solution already with success or failure, please inform.