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

[Feedback]: canRequestPermission should probably return false on Android versions < 13 #1716

Open
1 task done
mikemonaco opened this issue Jun 17, 2024 · 1 comment
Open
1 task done

Comments

@mikemonaco
Copy link

What's on your mind?

When calling OneSignal.Notifications.canRequestPermission() on Android version < 13, the return value always seems to be true. This may be misleading because you cannot request permission on older Android versions, they are granted by default. Notification permissions dialogs were added in Android version 13+.

For example, the following pseduo-code would produce an unfavorable outcome:

const canRequestPermission = await OneSignal.Notifications.canRequestPermission();
if (canRequestPermission) {
  showUserPrettyNotificationsUI();
  // on Android versions < 13, you would see this UI even though you cannot prompt for permissions
}

Since you cannot request permissions on Android version < 13, it would probably make sense to return false when calling canRequestPermission()

My current workaround is to check both getPermissionAsync() and canRequestPermission()

const hasPermission = await OneSignal.Notifications.getPermissionAsync();
const canRequestPermission = await OneSignal.Notifications.canRequestPermission();
// Only show UI if we DON'T have permission AND we CAN request permissions
// This fixes Android < 13 because hasPermission will be true
const shouldShowPrettyNotificationsUI = !hasPermission && canRequestPermission;

I'm not sure of the ideal solution for the SDK internally, since it may have other affects on iOS, but wanted to present this inconsistency to the developers of the OneSignal react native sdk.

Code of Conduct

  • I agree to follow this project's Code of Conduct
@fahimjubayer-dsi
Copy link

fahimjubayer-dsi commented Jul 6, 2024

If i am not wrong, the purpose of canRequestPermission is to track the situation where user DENIES the notification permission since the permission alert dialog wont show up if its denied a couple of times. So i suggest you change your logic like this

const hasPermission = await OneSignal.Notifications.getPermissionAsync();

if (!hasPermission) {

showUIExplainingWhyYouNeedNotificationPermission()

const canRequestPermission = await OneSignal.Notifications.canRequestPermission();

if (canRequestPermission) {
await OneSignal.Notifications.requestPermission(false)
} else {
await OneSignal.Notifications.requestPermission(true)
}
}

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