-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
76 lines (64 loc) · 2.24 KB
/
Copy pathApp.js
File metadata and controls
76 lines (64 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import 'react-native-get-random-values'
import React, { useEffect } from "react";
import "react-native-gesture-handler";
import { NavigationContainer } from "@react-navigation/native";
import { Provider } from "react-redux";
import store from "./source/redux/store/index"; // Ensure this path is correct
import { RootNavigator } from "./navigation";
import { GestureHandlerRootView } from 'react-native-gesture-handler';
// import { setupBackgroundTask } from "./src/services/backgroundTask";
import AppNavigator from "./source/navigation/AppNavigator";
import { LogBox } from "react-native";
import messaging from '@react-native-firebase/messaging';
// Firebase Push Notification Setup
const requestUserPermission = async () => {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
console.log("Notification permission granted.");
getFcmToken(); // Only fetch token if permission granted
} else {
console.log("Notification permission denied.");
}
};
const getFcmToken = async () => {
try {
const fcmToken = await messaging().getToken();
if (fcmToken) {
console.log("FCM Token:", fcmToken);
// You can send this token to your backend server
} else {
console.log("No token received");
}
} catch (error) {
console.error("Error fetching FCM token:", error);
}
};
const App = () => {
useEffect(() => {
LogBox.ignoreLogs([
"VirtualizedLists should never be nested",
]);
LogBox.ignoreAllLogs(false);
// Request permission & get FCM token
requestUserPermission();
// Foreground message handler
const unsubscribeOnMessage = messaging().onMessage(async remoteMessage => {
console.log('FCM Message (Foreground):', remoteMessage);
Alert.alert(remoteMessage.notification?.title || 'New Notification', remoteMessage.notification?.body || '');
});
return () => {
unsubscribeOnMessage(); // Clean up listener on unmount
};
}, []);
return (
<Provider store={store}>
<NavigationContainer>
<AppNavigator />
</NavigationContainer>
</Provider>
);
};
export default App;