Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use react-native-permissions to request notifications permission #1004

Merged
merged 3 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
1 change: 1 addition & 0 deletions ios/PodFile
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
8 changes: 7 additions & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -688,6 +693,7 @@ SPEC CHECKSUMS:
GoogleUtilities: bad72cb363809015b1f7f19beb1f1cd23c589f95
nanopb: b552cce312b6c8484180ef47159bc0f65a1f0431
Permission-Camera: bf6791b17c7f614b6826019fcfdcc286d3a107f6
Permission-Notifications: 150484ae586eb9be4e32217582a78350a9bb31c3
PromisesObjC: ab77feca74fa2823e7af4249b8326368e61014cb
RCT-Folly: 0080d0a6ebf2577475bda044aa59e2ca1f909cda
RCTRequired: e1866f61af7049eb3d8e08e8b133abd38bc1ca7a
Expand Down Expand Up @@ -750,6 +756,6 @@ SPEC CHECKSUMS:
SwiftyJSON: 36413e04c44ee145039d332b4f4e2d3e8d6c4db7
Yoga: 99caf8d5ab45e9d637ee6e0174ec16fbbb01bcfc

PODFILE CHECKSUM: ed930c9d00383a5a51627bbdba13753c584ed53a
PODFILE CHECKSUM: c42bb45a37c759d178470b56c0a1bb7df1c5eae1

COCOAPODS: 1.11.3
27 changes: 7 additions & 20 deletions src/notification/Notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,29 +17,16 @@ export default class Notifications {
public static async initialize(): Promise<void> {
// 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);
}
}

Expand Down