Geolocation asking for multiple permission requests inside a TWA

1.3k views Asked by At

I am fairly new to coding so this might be noob question..

I have created a PWA and am launching it as a TWA from within android studio then publishing the app to the play store.

There is a Geolocation feature in the PWA that determine the users location when they press a specific button.

var result;

function showPosition() {
    
    result = document.getElementById("result");
    
    
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
        result.innerHTML = "Getting the position information...";
    } else {
        alert("Sorry, your browser does not support HTML5 geolocation.");
    }
};


function successCallback(position) {
    result.innerHTML = "Your current position is (" + "Latitude: " + position.coords.latitude + ", " + "Longitude: " + position.coords.longitude + ")";
}

I have set up the ACCESS_FINE_LOCATION permissions in android studio and this will sucessully prompt the user for permissions when the app first launches.

This is in my android manifest

  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

and this is in the main activity:

 private static final String[] GPS_PERMISSIONS = {
        Manifest.permission.ACCESS_FINE_LOCATION};


private void verifyPermissions() {

    int permissionGps = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION);
    int permissionInternet = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INTERNET);

    if (permissionGps != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(
                MainActivity.this,
                GPS_PERMISSIONS,
                1

        );

This issue I face is that is is asking the user twice for location permissions.

Permissions are asked the first time when the app launches (the native prompt)

Native prompt screenshot

then again when they press the button in the TWA (the HTML5 prompt)

PWA prompt screenshot

Is it possible to share/pass the permissions from the native app to the TWA so all permissions are handled within the native app and user does not need to approve again once inside a TWA?

1

There are 1 answers

0
andreban On

Location Delegation is now stable and this shouldn't be an issue anymore. Check out the demo app on how to use it. If this behaviour persists, please file a bug at https://crbug.com