Skip to content

Commit

Permalink
[MOO-1519]: fix request notification permission return value (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad-Elsayed authored Aug 29, 2024
2 parents 22911d3 + 3225e80 commit 5095f0f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/Build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ jobs:
- name: "Defining environment variables"
if: startsWith(matrix.os, 'ubuntu')
id: variablesLinux
run: echo "arg=$(if [ "${{ steps.files.outputs.global_files }}" = "" ] && ${{ github.event_name == 'pull_request' }}; then echo '--since'; else echo ''; fi)" >> ${{ runner.os == 'Windows' && '$env:GITHUB_OUTPUT' || '$GITHUB_OUTPUT' }}
run: echo "arg=$(if [ "${{ steps.files.outputs.global_files }}" = "" ] && ${{ github.event_name == 'pull_request' }}; then echo '--since'; else echo '--all'; fi)" >> ${{ runner.os == 'Windows' && '$env:GITHUB_OUTPUT' || '$GITHUB_OUTPUT' }}
- name: "Defining environment variables"
if: startsWith(matrix.os, 'windows')
id: variablesWindows
run: echo "arg=$(If ('${{ steps.files.outputs.global_files }}' -eq '' -AND '${{ github.event_name == 'pull_request' }}' -eq 'true'){ echo '--since' } Else { echo '' })" >> ${{ runner.os == 'Windows' && '$env:GITHUB_OUTPUT' || '$GITHUB_OUTPUT' }}
run: echo "arg=$(If ('${{ steps.files.outputs.global_files }}' -eq '' -AND '${{ github.event_name == 'pull_request' }}' -eq 'true'){ echo '--since' } Else { echo '--all' })" >> ${{ runner.os == 'Windows' && '$env:GITHUB_OUTPUT' || '$GITHUB_OUTPUT' }}
- name: "Defining node version"
uses: actions/setup-node@d86ebcd40b3cb50b156bfa44dd277faf38282d12 # v4
with:
Expand All @@ -51,6 +51,6 @@ jobs:
- name: "Installing dependencies"
run: yarn install
- name: "Running build for development"
run: yarn workspaces foreach ${{ steps.variablesLinux.outputs.arg }} ${{ steps.variablesWindows.outputs.arg }} --all --parallel run build
run: yarn workspaces foreach ${{ steps.variablesLinux.outputs.arg }} ${{ steps.variablesWindows.outputs.arg }} --parallel run build
env:
NODE_OPTIONS: --max_old_space_size=8192
6 changes: 3 additions & 3 deletions .github/workflows/Release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ jobs:
- name: "Defining environment variables"
if: startsWith(matrix.os, 'ubuntu')
id: variablesLinux
run: echo "arg=$(if [ "${{ steps.files.outputs.global_files }}" = "" ] && ${{ github.event_name == 'pull_request' }}; then echo '--since'; else echo ''; fi)" >> ${{ runner.os == 'Windows' && '$env:GITHUB_OUTPUT' || '$GITHUB_OUTPUT' }}
run: echo "arg=$(if [ "${{ steps.files.outputs.global_files }}" = "" ] && ${{ github.event_name == 'pull_request' }}; then echo '--since'; else echo '--all'; fi)" >> ${{ runner.os == 'Windows' && '$env:GITHUB_OUTPUT' || '$GITHUB_OUTPUT' }}
- name: "Defining environment variables"
if: startsWith(matrix.os, 'windows')
id: variablesWindows
run: echo "arg=$(If ('${{ steps.files.outputs.global_files }}' -eq '' -AND '${{ github.event_name == 'pull_request' }}' -eq 'true'){ echo '--since' } Else { echo '' })" >> ${{ runner.os == 'Windows' && '$env:GITHUB_OUTPUT' || '$GITHUB_OUTPUT' }}
run: echo "arg=$(If ('${{ steps.files.outputs.global_files }}' -eq '' -AND '${{ github.event_name == 'pull_request' }}' -eq 'true'){ echo '--since' } Else { echo '--all' })" >> ${{ runner.os == 'Windows' && '$env:GITHUB_OUTPUT' || '$GITHUB_OUTPUT' }}
- name: "Defining node version"
uses: actions/setup-node@d86ebcd40b3cb50b156bfa44dd277faf38282d12 # v4
with:
Expand All @@ -48,6 +48,6 @@ jobs:
- name: "Installing dependencies"
run: yarn install
- name: "Running release for production"
run: yarn workspaces foreach ${{ steps.variablesLinux.outputs.arg }} ${{ steps.variablesWindows.outputs.arg }} --all --parallel run release
run: yarn workspaces foreach ${{ steps.variablesLinux.outputs.arg }} ${{ steps.variablesWindows.outputs.arg }} --parallel run release
env:
NODE_OPTIONS: --max_old_space_size=8192
6 changes: 3 additions & 3 deletions .github/workflows/UnitTests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
run: git fetch --no-tags --prune --depth=1 origin +refs/heads/${{ github.base_ref }}:refs/remotes/origin/${{ github.base_ref }}
- name: "Defining environment variables"
id: variables
run: echo "arg=$(if [ "${{ steps.files.outputs.global_files }}" = "" ] && ${{ github.event_name == 'pull_request' }}; then echo '--since'; else echo ''; fi)" >> ${{ runner.os == 'Windows' && '$env:GITHUB_OUTPUT' || '$GITHUB_OUTPUT' }}
run: echo "arg=$(if [ "${{ steps.files.outputs.global_files }}" = "" ] && ${{ github.event_name == 'pull_request' }}; then echo '--since'; else echo '--all'; fi)" >> ${{ runner.os == 'Windows' && '$env:GITHUB_OUTPUT' || '$GITHUB_OUTPUT' }}
- name: "Defining node version"
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3
with:
Expand All @@ -50,6 +50,6 @@ jobs:
- name: "Installing dependencies"
run: yarn install
- name: "Linting code"
run: yarn workspaces foreach ${{ steps.variables.outputs.arg }} --all --parallel run lint
run: yarn workspaces foreach ${{ steps.variables.outputs.arg }} --parallel run lint
- name: "Running unit tests"
run: yarn workspaces foreach ${{ steps.variables.outputs.arg }} --all --parallel run test
run: yarn workspaces foreach ${{ steps.variables.outputs.arg }} --parallel run test
4 changes: 4 additions & 0 deletions packages/jsActions/mobile-resources-native/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Fixed

- We fixed an issue where the `RequestNotificationPermission` action incorrectly returned 'true' after the user previously selected 'Don't Allow' on iOS and Android.

## [8.0.0] Native Mobile Resources - 2024-07-30

### BREAKING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,40 @@ import messaging from "@react-native-firebase/messaging";
*/
export async function RequestNotificationPermission(): Promise<boolean> {
// BEGIN USER CODE
// Documentation https://rnfirebase.io/docs/v5.x.x/notifications/receiving-notifications
// Documentation https://rnfirebase.io/messaging/usage

if (NativeModules && !NativeModules.RNFBMessagingModule) {
return Promise.reject(new Error("Firebase module is not available in your app"));
}

if (Platform.OS === "android") {
return PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS)
.then(() => {
return true;
})
.catch(() => {
return false;
});
try {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS);
return granted === PermissionsAndroid.RESULTS.GRANTED;
} catch (error) {
console.error("Failed to request permission on Android", error);
return false;
}
}

return messaging()
.requestPermission()
.then(() =>
Platform.OS === "ios" && !messaging().isDeviceRegisteredForRemoteMessages
? messaging()
.registerDeviceForRemoteMessages()
.then(() => true)
: true
)
.catch(() => false);
try {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (!enabled) {
return false;
}

if (!messaging().isDeviceRegisteredForRemoteMessages) {
await messaging().registerDeviceForRemoteMessages();
return true;
}
return true;
} catch (error) {
console.error("Failed to request permission on iOS", error);
return false;
}

// END USER CODE
}

0 comments on commit 5095f0f

Please sign in to comment.