From 2b3621af99bdeff7e8dcc14788fc6504182eb4d1 Mon Sep 17 00:00:00 2001 From: Uraz Akgultan Date: Fri, 6 Oct 2023 01:41:15 +0200 Subject: [PATCH] fix(mobile-resources-native): fix request notification permission --- .../mobile-resources-native/CHANGELOG.md | 4 +++ .../RequestNotificationPermission.ts | 35 +++++++++++++------ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/packages/jsActions/mobile-resources-native/CHANGELOG.md b/packages/jsActions/mobile-resources-native/CHANGELOG.md index 74712f27b..a43eaaa0b 100644 --- a/packages/jsActions/mobile-resources-native/CHANGELOG.md +++ b/packages/jsActions/mobile-resources-native/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +### Fixed + +- We fixed and issue when request notification permission for Android SDK 33. + ### Changed - Minimum mx version set to 9.24.1 diff --git a/packages/jsActions/mobile-resources-native/src/notifications/RequestNotificationPermission.ts b/packages/jsActions/mobile-resources-native/src/notifications/RequestNotificationPermission.ts index c86d946f8..ea75ffb07 100644 --- a/packages/jsActions/mobile-resources-native/src/notifications/RequestNotificationPermission.ts +++ b/packages/jsActions/mobile-resources-native/src/notifications/RequestNotificationPermission.ts @@ -5,7 +5,7 @@ // - the code between BEGIN USER CODE and END USER CODE // - the code between BEGIN EXTRA CODE and END EXTRA CODE // Other code you write will be lost the next time you deploy the project. -import { NativeModules, Platform } from "react-native"; +import { NativeModules, Platform, PermissionsAndroid } from "react-native"; import messaging from "@react-native-firebase/messaging"; // BEGIN EXTRA CODE @@ -24,16 +24,29 @@ export async function RequestNotificationPermission(): Promise { return Promise.reject(new Error("Firebase module is not available in your app")); } - return messaging() - .requestPermission() - .then(() => - Platform.OS === "ios" && !messaging().isDeviceRegisteredForRemoteMessages - ? messaging() - .registerDeviceForRemoteMessages() - .then(() => true) - : true - ) - .catch(() => false); + try { + if (Platform.OS === "android") { + const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS); + + if (granted !== PermissionsAndroid.RESULTS.GRANTED) { + return false; + } + } else if (Platform.OS === "ios") { + const authStatus = await messaging().requestPermission(); + const enabled = + authStatus === messaging.AuthorizationStatus.AUTHORIZED || + authStatus === messaging.AuthorizationStatus.PROVISIONAL; + + if (!enabled) { + if (!messaging().isDeviceRegisteredForRemoteMessages) { + await messaging().registerDeviceForRemoteMessages(); + } + } + } + } catch (error) { + return false; + } + return true; // END USER CODE }