Skip to content

Commit

Permalink
Merge pull request #1591 from OneSignal/fix/request_permission_deny
Browse files Browse the repository at this point in the history
Fix `requestPermission` to return `false` when permission is denied
  • Loading branch information
nan-li committed Nov 9, 2023
2 parents ac2181a + 9680d81 commit f410b06
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,11 @@ public void onNotificationPermissionChange(boolean permission) {
@ReactMethod
public void requestNotificationPermission(final boolean fallbackToSettings, Promise promise) {
OneSignal.getNotifications().requestPermission(fallbackToSettings, Continue.with(result -> {
promise.resolve(result.isSuccess());
if (result.isSuccess()) {
promise.resolve(result.getData());
} else {
promise.reject(result.getThrowable().getMessage());
}
}));
}

Expand Down
14 changes: 11 additions & 3 deletions examples/RNOneSignalTS/src/OSButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ class OSButtons extends React.Component<Props> {
},
);

const permissionNativeButton = renderButtonView(
'Permission Native',
async () => {
const granted = await OneSignal.Notifications.permissionNative();
loggingFunction(`Permission Native: ${granted}`);
},
);

const canRequestPermissionButton = renderButtonView(
'Can Request Permission',
async () => {
Expand All @@ -151,9 +159,8 @@ class OSButtons extends React.Component<Props> {
'Request Permission',
async () => {
loggingFunction('Requesting notification permission');
OneSignal.Notifications.requestPermission(false, (granted) => {
loggingFunction(`Notification permission granted ${granted}`);
});
const granted = await OneSignal.Notifications.requestPermission(false);
loggingFunction(`Notification permission granted ${granted}`);
},
);

Expand All @@ -167,6 +174,7 @@ class OSButtons extends React.Component<Props> {

return [
hasPermissionButton,
permissionNativeButton,
canRequestPermissionButton,
requestPermissionButton,
clearOneSignalNotificationsButton,
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ export namespace OneSignal {
return pushSub.optedIn;
}

/** Enable the push notification subscription to OneSignal. */
/** Disable the push notification subscription to OneSignal. */
export function optOut() {
if (!isNativeModuleLoaded(RNOneSignal)) return;

RNOneSignal.optOut();
}

/** Disable the push notification subscription to OneSignal. */
/** Enable the push notification subscription to OneSignal. */
export function optIn() {
if (!isNativeModuleLoaded(RNOneSignal)) return;

Expand Down Expand Up @@ -413,6 +413,10 @@ export namespace OneSignal {
if (!isNativeModuleLoaded(RNOneSignal)) {
return Promise.reject(new Error('OneSignal native module not loaded'));
}
// if permission already exists, return early as the native call will not resolve
if (hasPermission()) {
return Promise.resolve(true);
}

return RNOneSignal.requestNotificationPermission(fallbackToSettings);
}
Expand Down

0 comments on commit f410b06

Please sign in to comment.