From ff1ff4c3119bbd581604154ea454dd38ca4a6a7f Mon Sep 17 00:00:00 2001 From: AlixH Date: Fri, 5 May 2023 15:01:49 +0200 Subject: [PATCH 1/2] Use react-native-permissions to request notifications permission --- ios/PodFile | 1 + ios/Podfile.lock | 8 +++++++- src/notification/Notifications.tsx | 27 +++++++-------------------- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/ios/PodFile b/ios/PodFile index 7a156f744..85c5b7ef0 100644 --- a/ios/PodFile +++ b/ios/PodFile @@ -47,6 +47,7 @@ target 'eMobility' do # Camera Permission permissions_path = '../node_modules/react-native-permissions/ios' pod 'Permission-Camera', :path => "#{permissions_path}/Camera" + pod 'Permission-Notifications', :path => "#{permissions_path}/Notifications" pod 'RNSecureStorage', :path => '../node_modules/rn-secure-storage/ios/RNSecureStorage.podspec' diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 3d06f65af..5844f8a73 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -70,6 +70,8 @@ PODS: - nanopb/encode (2.30909.0) - Permission-Camera (3.6.1): - RNPermissions + - Permission-Notifications (3.6.1): + - RNPermissions - PromisesObjC (2.1.1) - RCT-Folly (2021.07.22.00): - boost @@ -479,6 +481,7 @@ DEPENDENCIES: - FBReactNativeSpec (from `../node_modules/react-native/React/FBReactNativeSpec`) - glog (from `../node_modules/react-native/third-party-podspecs/glog.podspec`) - Permission-Camera (from `../node_modules/react-native-permissions/ios/Camera`) + - Permission-Notifications (from `../node_modules/react-native-permissions/ios/Notifications`) - RCT-Folly (from `../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`) - RCTRequired (from `../node_modules/react-native/Libraries/RCTRequired`) - RCTTypeSafety (from `../node_modules/react-native/Libraries/TypeSafety`) @@ -569,6 +572,8 @@ EXTERNAL SOURCES: :podspec: "../node_modules/react-native/third-party-podspecs/glog.podspec" Permission-Camera: :path: "../node_modules/react-native-permissions/ios/Camera" + Permission-Notifications: + :path: "../node_modules/react-native-permissions/ios/Notifications" RCT-Folly: :podspec: "../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec" RCTRequired: @@ -688,6 +693,7 @@ SPEC CHECKSUMS: GoogleUtilities: bad72cb363809015b1f7f19beb1f1cd23c589f95 nanopb: b552cce312b6c8484180ef47159bc0f65a1f0431 Permission-Camera: bf6791b17c7f614b6826019fcfdcc286d3a107f6 + Permission-Notifications: 150484ae586eb9be4e32217582a78350a9bb31c3 PromisesObjC: ab77feca74fa2823e7af4249b8326368e61014cb RCT-Folly: 0080d0a6ebf2577475bda044aa59e2ca1f909cda RCTRequired: e1866f61af7049eb3d8e08e8b133abd38bc1ca7a @@ -750,6 +756,6 @@ SPEC CHECKSUMS: SwiftyJSON: 36413e04c44ee145039d332b4f4e2d3e8d6c4db7 Yoga: 99caf8d5ab45e9d637ee6e0174ec16fbbb01bcfc -PODFILE CHECKSUM: ed930c9d00383a5a51627bbdba13753c584ed53a +PODFILE CHECKSUM: c42bb45a37c759d178470b56c0a1bb7df1c5eae1 COCOAPODS: 1.11.3 diff --git a/src/notification/Notifications.tsx b/src/notification/Notifications.tsx index cb26c82e9..cd70fe23a 100644 --- a/src/notification/Notifications.tsx +++ b/src/notification/Notifications.tsx @@ -4,11 +4,11 @@ import messaging from '@react-native-firebase/messaging'; import CentralServerProvider from '../provider/CentralServerProvider'; import {Notification} from '../types/UserNotifications'; import {getApplicationName, getBundleId, getVersion} from 'react-native-device-info'; -import {PLATFORM} from '../theme/variables/commonColor'; import Message from '../utils/Message'; import I18n from 'i18n-js'; import SecuredStorage from '../utils/SecuredStorage'; import ProviderFactory from '../provider/ProviderFactory'; +import {requestNotifications} from 'react-native-permissions'; export default class Notifications { private static centralServerProvider: CentralServerProvider; @@ -17,29 +17,16 @@ export default class Notifications { public static async initialize(): Promise { // Setup central provider this.centralServerProvider = await ProviderFactory.getProvider(); - // Check if app has permission - let authorizationStatus = await messaging().hasPermission(); - if (Platform.OS === PLATFORM.IOS && - authorizationStatus !== messaging.AuthorizationStatus.PROVISIONAL && - authorizationStatus !== messaging.AuthorizationStatus.AUTHORIZED - ) { - try { - // requesting permission from user is required for iOS - authorizationStatus = await messaging().requestPermission(); - } catch ( error ) { - console.error(error); - } - } - if (authorizationStatus === messaging.AuthorizationStatus.AUTHORIZED || messaging.AuthorizationStatus.PROVISIONAL) { - try { - // Retrieve mobile token + try { + const response = await requestNotifications(['alert', 'sound']); + if (response?.status === 'granted' ) { const fcmToken = await messaging().getToken(); - if ( fcmToken ) { + if (fcmToken) { this.token = fcmToken; } - } catch ( error ) { - console.error(error); } + } catch ( error ) { + console.error(error); } } From dfedd604ccd2f8f3fa5eccf9b9236fec064acbb6 Mon Sep 17 00:00:00 2001 From: AlixH Date: Fri, 5 May 2023 15:40:34 +0200 Subject: [PATCH 2/2] Fixed tests --- __mocks__/react-native-permissions.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 __mocks__/react-native-permissions.js diff --git a/__mocks__/react-native-permissions.js b/__mocks__/react-native-permissions.js new file mode 100644 index 000000000..e69de29bb