Capacitor geolocation with iOS on PWA

I’m building an Ionic PWA that uses the Capacitor Geolocation plugin and I want to make sure the user has given proper permission so I’m using the Permissions plugin: https://capacitorjs.com/docs/apis/permissions

It seems to be working fine on Android, but I’m getting the following error on iOS/Safari when it tries to call the permissions query: “This browser does not support the Permissions API”

Is there something else I need to do so it works on iOS?

Thanks, Dan

1 Like

Safari doesn’t support the Permissions API at the moment as the API is still in flux.

Plugins how ever should be able to just call the APIs and the platforms will handle the permissions automatically.

1 Like

@mhartington your comment might answer my question (Capacitor Permissions Not Showing Native Dialog). I’ve been having issues puzzling out how to use the requestPermission method in case a user revokes permissions while using the application. I was expecting it would pop up a native dialog in Android asking for permission to use LocalNotifications, Geolocation, etc; but it always seems to return an empty object literal in all cases.

Assuming requestPermission was showing a dialog what is the proper use of permissions for the different plugins like LocalNotifications?

public async scheduleRenewalNotification(renewalDate: string) {
    // Check permissions are enabled
    const { value: notificationsEnabled } = await LocalNotifications.areEnabled();

    if (!notificationsEnabled) {
        // Prompt for permissions
        const { granted } = await LocalNotifications.requestPermission();
    
        if(!granted) {
            return;
        }
    }

    // continue with scheduling notifications
}

Seeing the response in the other post, permissions got a big overhaul in Cap v3. In the current release, they kind of are all over the place. Some require you request permissions before getting access, other require permissions right as you invoke a native function (like notifications).

Since notifications require permissions just in time, the requestPermission() method on iOS will just noop, while on android it will actually request permissions. Difference between platforms and permissions model.

Thanks @mhartington, it seems like I might need to use Cordova AndroidPermissions to pop the native dialog up. Currently Capacitor LocationNotifications.requestPermissions() doesn’t do anything on Android. Do you know what Permission is required for LocalNotifications? I haven’t been able to puzzle it out using https://developer.android.com/reference/android/Manifest.permission.html which only contains a single permission for notifications ( ACCESS_NOTIFICATION_POLICY), but doesn’t appear to be the right one. I don’t see a native dialog pop up like I do with Geolocation when I use it.

You shouldn’t need the Cordova plugin for this.

Looking into this more…

On iOS, it does make a call to the notification mgmt system, which should show the native UI if you haven’t already requested it.

On Android, I’m not sure if it will need anything. The default settings in the AndroidManifest.xml provide.

I just started a fresh project and it worked right out of the box.

Did you get a native prompt asking the user to permit LocalNotifications? I did something similar. I’ve been creating blank projects to find out how plugins work in Capacitor. In my case the notifications work out of the box when the permissions are turned on, but it doesn’t seem to prompt the user to allow notifications when they are turned off. When I’m using Geolocation I get a native dialog prompt asking me to approve or deny permissions for the plugin if permissions were previously denied.

No, I do not get a dialog to allow notifications, since permissions are handled differently for local notifications.

But I see what you mean about when users toggle the permissions at a later point. I reported this to the team and it’s being addressed in a future release!

1 Like