Why Am I Receiving Error `Push notification prompting can only be done from a user gesture` In Safar

When this error is encountered it indicates that you must have your users interact with your site before you are allowed to ask for notification permissions - for example by clicking on a button. However, it is also possible to receive this error even in response to a button click. This appears to be related to the scope of the beamsClient instance, and only appears to impact Safari browsers. Developers will need to ensure that the beamsClient is instantiated outside of the onClick handler function.

For example, the following code throws the error:

function askForPermission() {
 btn.addEventListener('click', function() {
    const beamsClient = new PusherPushNotifications.Client({instanceId: instanceId});
    beamsClient.start()
});
}

However by moving the declaration and initialization of `beamsClient` outside the event listener then the error is not encountered:

const beamsClient = new PusherPushNotifications.Client({instanceId: instanceId});function askForPermission(){
    btn.addEventListener('click', function() {
        beamsClient.start()
    } );
}

Still have questions? Please reach out to our Support team by visiting this page.

Last updated