|
| 1 | +/* eslint-disable react-native/split-platform-components */ |
1 | 2 | import { createNativeStackNavigator } from '@react-navigation/native-stack'; |
| 3 | +import { useEffect } from 'react'; |
| 4 | +import { PermissionsAndroid, Platform } from 'react-native'; |
2 | 5 |
|
3 | 6 | import { Route } from '../../constants/routes'; |
4 | 7 | import { useIterableApp } from '../../hooks/useIterableApp'; |
| 8 | +import type { RootStackParamList } from '../../types'; |
5 | 9 | import { Login } from '../Login'; |
6 | 10 | import { Main } from './Main'; |
7 | | -import type { RootStackParamList } from '../../types'; |
8 | 11 |
|
9 | 12 | const Stack = createNativeStackNavigator<RootStackParamList>(); |
10 | 13 |
|
| 14 | +const requestNotificationPermission = async () => { |
| 15 | + if (Platform.OS === 'android') { |
| 16 | + const apiLevel = Platform.Version; // Get the Android API level |
| 17 | + |
| 18 | + if (apiLevel >= 33) { |
| 19 | + // Check if Android 13 or higher |
| 20 | + try { |
| 21 | + const granted = await PermissionsAndroid.request( |
| 22 | + PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS, |
| 23 | + { |
| 24 | + title: 'Notification Permission', |
| 25 | + message: |
| 26 | + 'This app needs access to your notifications for push, in-app messages, embedded messages and more.', |
| 27 | + buttonNeutral: 'Ask Me Later', |
| 28 | + buttonNegative: 'Cancel', |
| 29 | + buttonPositive: 'OK', |
| 30 | + } |
| 31 | + ); |
| 32 | + if (granted === PermissionsAndroid.RESULTS.GRANTED) { |
| 33 | + console.log('Notification permission granted'); |
| 34 | + } else { |
| 35 | + console.log('Notification permission denied'); |
| 36 | + } |
| 37 | + } catch (err) { |
| 38 | + console.warn(err); |
| 39 | + } |
| 40 | + } else { |
| 41 | + // For Android versions below 13, notification permission is generally not required |
| 42 | + // or is automatically granted upon app installation. |
| 43 | + console.log( |
| 44 | + 'Notification permission not required for this Android version.' |
| 45 | + ); |
| 46 | + } |
| 47 | + } |
| 48 | +}; |
| 49 | + |
11 | 50 | export const App = () => { |
12 | 51 | const { isLoggedIn } = useIterableApp(); |
13 | 52 |
|
| 53 | + useEffect(() => { |
| 54 | + requestNotificationPermission(); |
| 55 | + }, []); |
| 56 | + |
14 | 57 | return ( |
15 | 58 | <Stack.Navigator> |
16 | 59 | {isLoggedIn ? ( |
|
0 commit comments