Add logic to handle notifications to open profile or posts

This commit is contained in:
Adolfo Reyna
2025-02-27 23:04:56 -05:00
parent 733c1fd793
commit f5c7ff38dd

21
App.js
View File

@@ -31,6 +31,7 @@ import SongPlayer from './Views/SongPlayer.js';
import { Platform } from 'react-native'; import { Platform } from 'react-native';
import { PostHogProvider } from 'posthog-react-native' import { PostHogProvider } from 'posthog-react-native'
import * as Updates from 'expo-updates'; import * as Updates from 'expo-updates';
import { useNavigation } from '@react-navigation/native';
const Tab = createBottomTabNavigator(); const Tab = createBottomTabNavigator();
@@ -95,6 +96,7 @@ const MainNavigation = ({ route }) => {
const [notification, setNotification] = useState(false); const [notification, setNotification] = useState(false);
const notificationListener = useRef(); const notificationListener = useRef();
const responseListener = useRef(); const responseListener = useRef();
const mainNavigation = useNavigation();
useEffect(() => { useEffect(() => {
registerForPushNotificationsAsync().then(async (token) => { registerForPushNotificationsAsync().then(async (token) => {
let isLoggedIn = await API.isLoggedIn(); let isLoggedIn = await API.isLoggedIn();
@@ -105,14 +107,27 @@ const MainNavigation = ({ route }) => {
// This listener is fired whenever a notification is received while the app is foregrounded // This listener is fired whenever a notification is received while the app is foregrounded
notificationListener.current = Notifications.addNotificationReceivedListener(notification => { notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
//console.log("got notif", notification) //console.log("got notif", notification);
setNotification(notification); setNotification(notification);
}); });
// This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed) // This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => { responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
//console.log("got notif click", notification) const data = response.notification.request.content.data;
console.log(response); if (data && Object.keys(data).length > 0) {
try {
if (data.profile_id) {
mainNavigation.navigate("Profile", { profileid: data.profile_id });
}
if (data.post_id) {
mainNavigation.navigate("SinglePost", { postid: data.post_id });
}
} catch (error) {
alert("Error: " + error);
}
} else {
//alert("Notification clicked but no data found.");
}
}); });
const interval = setInterval(async () => { const interval = setInterval(async () => {