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

Fix requestPermission to return false when permission is denied #1591

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading