Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Android: Notifications.requestPermission hangs (capacitor) and hasPermissions() always returns false #932

Open
2 of 3 tasks
kpturner opened this issue Oct 14, 2023 · 7 comments

Comments

@kpturner
Copy link

kpturner commented Oct 14, 2023

What happened?

Using version 5.0.2 - when an app is installed for the first time on Android and runs
Notifications.requestPermission(true)
The user is prompted to allow notifications.

In previous versions of the SDK, you could call Notifications.requestPermissions with impunity. If you do so when the user has already agreed, then the request is ignored. Now, if you do so, the request just hangs causing the app to become unresponsive. You cannot even condition the call to Notifications.requestPermissions because Notifications.hasPermission() always returns false.

Steps to reproduce?

1. Install 5.0.2
2. Call Notifications.requestPermission(true) while running on Android
3. Respond to the prompt
4. Restart the app 
5. Notifications.requestPermission(true) will hang

What did you expect to happen?

Notifications.requestPermission(true) should behave as it did before. Prompt the user only if necessary and do not hang.
hasPermissions() should return the correct value

OneSignal Cordova SDK version

5.0.2

Which platform(s) are affected?

  • iOS
  • Android

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@kpturner
Copy link
Author

kpturner commented Oct 14, 2023

I have worked around this issue by wrapping this requestPermission prompt in a call to Notifications.canRequestPermission() for the time being, and adding a timeout to the call to request permissions

    if (await OneSignal.Notifications.canRequestPermission()) {
      setTimeout(async () => {
        await OneSignal.Notifications.requestPermission(true)
      }, 0)
    } 

@kpturner
Copy link
Author

Another workaround is to not await but use a .then() chain

if (await OneSignal.Notifications.canRequestPermission()) {
      OneSignal.Notifications.requestPermission(true).then(
        (accepted) => {
            ....
        },
        (error) => {
           ....
        }
      )
 }

@nan-li
Copy link
Contributor

nan-li commented Nov 7, 2023

Hi @kpturner, we have implemented a fix for requestPermission that will be in the next release. However, I am concerned about this:

You cannot even condition the call to Notifications.requestPermissions because Notifications.hasPermission() always returns false.

Can you tell me more about this? Is it always false on all devices and use cases?

@kpturner
Copy link
Author

kpturner commented Nov 7, 2023

I do not think I am in a position to test "all devices and use cases". Perhaps you can tell me a device (simulator and API version) where it correctly returns true and I can verify that?

@nan-li
Copy link
Contributor

nan-li commented Nov 7, 2023

Hi @kpturner, just in your own testing if this was the case 100% of the time?

I most recently tried to reproduce in an Android emulator Nexus S API 33 using a minimal app:

  1. New app installation and call OneSignal.initialize
  2. Wait a few secons.
  3. Check hasPermission returns false
  4. Prompt for push with OneSignal.Notifications.requestPermission(true)
  5. Press accept
  6. Check hasPermission returns true now

@kpturner
Copy link
Author

kpturner commented Nov 8, 2023

Here are the dependencies in use:

    "@capacitor/android": "^5.0.5",
    "@capacitor/app": "^5.0.6",
    "@capacitor/cli": "^5.0.5",
    "@capacitor/core": "^5.0.5",
    "@capacitor/device": "^5.0.6",
    "@capacitor/ios": "^5.0.5",
    "@capacitor/preferences": "^5.0.6",
    "@capacitor/splash-screen": "^5.0.6",
    "capacitor-plugin-app-tracking-transparency": "^2.0.4",
    "capacitor-set-version": "^2.0.13",
    "googleapis": "^128.0.0",
    "onesignal-cordova-plugin": "^5.0.1"

On a Pixel 7 Pro API 33, OneSignal.Notifications.hasPermission() always returns false. That is after accepting permission and also after the app restarts. So the my question about your own test: Does it still return true after restarting the app?

@kpturner
Copy link
Author

I am wondering if this was purely down to the timing of the call to hasPermissions?
I have refactored my code so that this method is not called until the user has pressed a CTA button whereas before it was doing it on startup. In this scenario, it is working as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants