14

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:

    1. Web app client registers to Push notification
    2. It sends subscriber object to my server
    3. My server using this object sends message to Push notification server
    4. Push notification server sends messages to my web app client
    5. My web app client wakes up my service worker for a brief period to show the notification message
    6. 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.

Savaratkar
  • 1,974
  • 1
  • 24
  • 44
  • 1
    I don't have a solution here ;( But the reason why the geolocation is turned off when running in the background or lock screen enabled is to avoid draining the battery. But at the same time how does the turn by turn directions work with the lock screen? Well, one of the benefits of a native (at this time). A similar problem exists with audio and video through the web. This is why the media playback API is important, because it can work behind the lock screen. There are all sorts of legal/trust issues the browsers have to sort through in bringing this to market for the web. – Chris Love Sep 24 '18 at 23:12
  • Did you find out a solution for this? I need a similar feature and I'm unable to find a workaround. – Zerok Jul 19 '19 at 00:42
  • No nothing yet.... @Zerok – Savaratkar Jul 19 '19 at 14:02
  • 2
    Hi @savaratkar. I have tried to make your suggestion, no success: https://codesandbox.io/s/webbrowser-geofence-w003z It seems that the service worker's `navigator` does not have the property `geolocation`: https://stackoverflow.com/a/22315979/858412 – Leonardo Sep 23 '19 at 22:31
  • oh ok. Thanks for the effort man! @Leonardo – Savaratkar Sep 25 '19 at 08:41

1 Answers1

-2

Can you try Page visibility Api, which listens for visibilitychange of a browser tab is hidden or switched to other tab (in case of browser). For mobiles, it's just a try.

src : https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API

or You can look into this answer too..

Detecting when screen is locked

vinay nvd
  • 27
  • 3