OneSignal / react-native-onesignal

React Native Library for OneSignal Push Notifications Service
Other
1.56k stars 371 forks source link

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

Open mikemonaco opened 1 month ago

mikemonaco commented 1 month ago

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

fahimjubayer-dsi commented 3 weeks ago

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) } }