Add logic to handle notifications to open profile or posts
This commit is contained in:
21
App.js
21
App.js
@@ -31,6 +31,7 @@ import SongPlayer from './Views/SongPlayer.js';
|
||||
import { Platform } from 'react-native';
|
||||
import { PostHogProvider } from 'posthog-react-native'
|
||||
import * as Updates from 'expo-updates';
|
||||
import { useNavigation } from '@react-navigation/native';
|
||||
|
||||
|
||||
const Tab = createBottomTabNavigator();
|
||||
@@ -95,6 +96,7 @@ const MainNavigation = ({ route }) => {
|
||||
const [notification, setNotification] = useState(false);
|
||||
const notificationListener = useRef();
|
||||
const responseListener = useRef();
|
||||
const mainNavigation = useNavigation();
|
||||
useEffect(() => {
|
||||
registerForPushNotificationsAsync().then(async (token) => {
|
||||
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
|
||||
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
|
||||
//console.log("got notif", notification)
|
||||
//console.log("got notif", 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)
|
||||
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
|
||||
//console.log("got notif click", notification)
|
||||
console.log(response);
|
||||
const data = response.notification.request.content.data;
|
||||
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 () => {
|
||||
|
||||
Reference in New Issue
Block a user