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

Notifications #37

Merged
merged 1 commit into from
Sep 13, 2024
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
132 changes: 0 additions & 132 deletions App.js

This file was deleted.

109 changes: 109 additions & 0 deletions Notification.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { useState, useEffect, useRef } from 'react';
import { Text, View, Button, Platform } from 'react-native';
import * as Device from 'expo-device';
import * as Notifications from 'expo-notifications';
import Constants from 'expo-constants';

Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});

export default function Notification() {
const [expoPushToken, setExpoPushToken] = useState('');
const [channels, setChannels] = useState<Notifications.NotificationChannel[]>([]);
const [notification, setNotification] = useState<Notifications.Notification | undefined>(
undefined
);
const notificationListener = useRef<Notifications.Subscription>();
const responseListener = useRef<Notifications.Subscription>();

useEffect(() => {
registerForPushNotificationsAsync().then(token => token && setExpoPushToken(token));

if (Platform.OS === 'android') {
Notifications.getNotificationChannelsAsync().then(value => setChannels(value ?? []));
}
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
setNotification(notification);
});

responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
console.log(response);
});

return () => {
notificationListener.current &&
Notifications.removeNotificationSubscription(notificationListener.current);
responseListener.current &&
Notifications.removeNotificationSubscription(responseListener.current);
};
}, []);

return (
<View
style={{
flex: 1,
alignItems: 'center',
justifyContent: 'space-around',
}}>
<Text>Your expo push token: {expoPushToken}</Text>
<Text>{`Channels: ${JSON.stringify(
channels.map(c => c.id),
null,
2
)}`}</Text>
</View>
);
}

async function registerForPushNotificationsAsync() {
let token;

if (Platform.OS === 'android') {
await Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}

if (Device.isDevice) {
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== 'granted') {
alert('Failed to get push token for push notification!');
return;
}
// Learn more about projectId:
// https://docs.expo.dev/push-notifications/push-notifications-setup/#configure-projectid
// EAS projectId is used here.
try {
const projectId =
Constants?.expoConfig?.extra?.eas?.projectId ?? Constants?.easConfig?.projectId;
if (!projectId) {
throw new Error('Project ID not found');
}
token = (
await Notifications.getExpoPushTokenAsync({
projectId,
})
).data;
console.log(token);
} catch (e) {
token = `${e}`;
}
} else {
alert('Must use physical device for Push Notifications');
}

return token;
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
"expo-auth-session": "^5.5.2",
"expo-camera": "^15.0.11",
"expo-dev-client": "~4.0.25",
"expo-device": "~6.0.2",
"expo-font": "~12.0.9",
"expo-linking": "^6.3.1",
"expo-notifications": "~0.28.16",
"expo-splash-screen": "~0.27.5",
"expo-status-bar": "~1.12.1",
"expo-updates": "~0.25.24",
Expand Down
2 changes: 2 additions & 0 deletions screens/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { RootState } from "../redux/store";
import CurrentEventCard from "../Components/CurrentEventCard";
import Colors from "../constants/Colors";
import { Text } from "@gluestack-ui/themed";
import Notification from "../Notification";

const { width, height } = Dimensions.get("window");

Expand Down Expand Up @@ -70,6 +71,7 @@ const Home: React.FC = () => {
)}

</View>
<Notification/>
</SafeAreaView>
);
};
Expand Down
Loading