react-onesignal
react-onesignal copied to clipboard
[Question]: How should requestPermission method be used
How can we help?
Hi,
I was updating my project from v2 to v3 and I run into an issue with
OneSignal.Notifications.requestPermission()
When and how should be this method be used? When I ran this method the OneSignal looked stuck. Here is the code that I tried to run:
const init = () ->> {
OneSignal.init({ appId: AppId });
}
const login = (user: string) => {
console.log('Requesting permission');
await OneSignal.Notifications.requestPermission();
console.log('Permission successfully requested'); //this consol.log will will not be printed
await OneSignal.login(user);
}
but if I run the same function without await the function run and user is logged into OneSignal and notifications are received.
const login = (user: string) => {
console.log('Requesting permission');
OneSignal.Notifications.requestPermission();
console.log('Permission successfully requested'); // console.log will be printed
await OneSignal.login(user);
}
Is there an reason why it gets stuck on this awaited method? Thank you in advance for any response.
I'm running into the same issue, its stuck in the await call. Did you figure it out @Kismar
Hi @harishraman94 I run the code without "await" (second block of code), this is more of a question about correct usage, and if it's not a potential bug
Hi @Kismar and thanks for reaching out. requestPermission() should be awaited as you have it in your first example. A couple of different reasons come to mind that could cause it to hang:
- Ensure that
OneSignalis completely initialized before trying to callrequestPermission(). Your first example implies that it is, but I don’t have the larger context of howinit()andlogin()are called to be sure. - If
OneSignalis being properly initialized, then we might need to check if browser conditions are preventing the prompt from showing:- Was the permission prompt previously denied? This could cause it to not show up again for a certain amount of time. If so, I would suggest resetting the notification permissions for the site and trying again. (You can also test this in your browser console with
await OneSignal.Notifications.permission) - Some browsers (such as Arc) experience issues displaying permission prompts, which could cause. I would suggest testing your browser with a tool like https://tests.peter.sh/ to confirm this is not the case.
- Was the permission prompt previously denied? This could cause it to not show up again for a certain amount of time. If so, I would suggest resetting the notification permissions for the site and trying again. (You can also test this in your browser console with
Let me know if that helps or if you have any further questions.