From 6b9ac448b50b9b9fa2cf943615db1ff215bf821b Mon Sep 17 00:00:00 2001 From: Nan Date: Mon, 23 Oct 2023 13:58:11 -0700 Subject: [PATCH 1/4] Resolve correct boolean when permission denied * result.isSuccess() always returns true because this is Whether the coroutine call was successful * result.getData() holds the value --- .../java/com/onesignal/rnonesignalandroid/RNOneSignal.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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()); + } })); } From c281499b68a986abb05f304f3c02a8e72f17100f Mon Sep 17 00:00:00 2001 From: Nan Date: Mon, 23 Oct 2023 15:09:40 -0700 Subject: [PATCH 2/4] example app - add permission native button * Also fix syntax for requestPermission --- examples/RNOneSignalTS/src/OSButtons.tsx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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, From 0421f6171bed9e28d9d5103f83c496adffd701fe Mon Sep 17 00:00:00 2001 From: Nan Date: Sun, 29 Oct 2023 20:28:58 -0700 Subject: [PATCH 3/4] docs: fix optIn and optOut description --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4ffda205..f8f5a265 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; From 9680d81e187841dcf452b229db13f8dbace0a01b Mon Sep 17 00:00:00 2001 From: Nan Date: Mon, 6 Nov 2023 12:19:11 -0800 Subject: [PATCH 4/4] Resolve request permission call when permission already exists Problem: If permission is already enabled, the native call to `OneSignal.getNotifications().requestPermission(fallbackToSettings, Continue.with(...)` never suspends and the Continuation code block never runs. As a result, we would not be able to resolve the promise over the bridge. Solution: Before calling that method, do a permission check and return true early. --- src/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/index.ts b/src/index.ts index f8f5a265..ad0223ab 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); }