diff --git a/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java b/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java index ff8885c3..11993212 100644 --- a/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java +++ b/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java @@ -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()); + } })); } diff --git a/examples/RNOneSignalTS/src/OSButtons.tsx b/examples/RNOneSignalTS/src/OSButtons.tsx index 96c32ed5..20e73a65 100644 --- a/examples/RNOneSignalTS/src/OSButtons.tsx +++ b/examples/RNOneSignalTS/src/OSButtons.tsx @@ -139,6 +139,14 @@ class OSButtons extends React.Component { }, ); + const permissionNativeButton = renderButtonView( + 'Permission Native', + async () => { + const granted = await OneSignal.Notifications.permissionNative(); + loggingFunction(`Permission Native: ${granted}`); + }, + ); + const canRequestPermissionButton = renderButtonView( 'Can Request Permission', async () => { @@ -151,9 +159,8 @@ class OSButtons extends React.Component { '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}`); }, ); @@ -167,6 +174,7 @@ class OSButtons extends React.Component { return [ hasPermissionButton, + permissionNativeButton, canRequestPermissionButton, requestPermissionButton, clearOneSignalNotificationsButton, diff --git a/src/index.ts b/src/index.ts index 4ffda205..ad0223ab 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; @@ -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); }