react-onesignal icon indicating copy to clipboard operation
react-onesignal copied to clipboard

[Question]: How should requestPermission method be used

Open Kismar opened this issue 7 months ago • 3 comments

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.

Kismar avatar May 06 '25 14:05 Kismar

I'm running into the same issue, its stuck in the await call. Did you figure it out @Kismar

harishraman94 avatar May 14 '25 08:05 harishraman94

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

Kismar avatar May 20 '25 12:05 Kismar

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 OneSignal is completely initialized before trying to call requestPermission(). Your first example implies that it is, but I don’t have the larger context of how init() and login() are called to be sure.
  • If OneSignal is 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.

Let me know if that helps or if you have any further questions.

sherwinski avatar May 28 '25 00:05 sherwinski