-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
256 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Notifications } from "../../pages/Notifications"; | ||
|
||
export default function Page() { | ||
return <Notifications />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import Constants from "expo-constants"; | ||
import React, { useEffect, useRef, useState } from "react"; | ||
import { View, Platform } from "react-native"; | ||
import { Button } from "~/components/ui/button"; | ||
import { Text } from "~/components/ui/text"; | ||
import Screen from "~/components/Screen"; | ||
|
||
import * as Device from "expo-device"; | ||
import * as ExpoNotifications from "expo-notifications"; | ||
|
||
ExpoNotifications.setNotificationHandler({ | ||
handleNotification: async () => ({ | ||
shouldShowAlert: true, | ||
shouldPlaySound: false, | ||
shouldSetBadge: false, | ||
}), | ||
}); | ||
|
||
async function sendPushNotification(expoPushToken: string) { | ||
const message = { | ||
to: expoPushToken, | ||
sound: 'default', | ||
title: 'Original Title', | ||
body: 'And here is the body!', | ||
data: { someData: 'goes here' }, | ||
}; | ||
|
||
await fetch('https://exp.host/--/api/v2/push/send', { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Accept-encoding': 'gzip, deflate', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(message), | ||
}); | ||
} | ||
|
||
function handleRegistrationError(errorMessage: string) { | ||
alert(errorMessage); | ||
throw new Error(errorMessage); | ||
} | ||
|
||
async function registerForPushNotificationsAsync() { | ||
if (Platform.OS === 'android') { | ||
ExpoNotifications.setNotificationChannelAsync('default', { | ||
name: 'default', | ||
importance: ExpoNotifications.AndroidImportance.MAX, | ||
vibrationPattern: [0, 250, 250, 250], | ||
lightColor: '#FF231F7C', | ||
}); | ||
} | ||
|
||
if (Device.isDevice) { | ||
const { status: existingStatus } = await ExpoNotifications.getPermissionsAsync(); | ||
let finalStatus = existingStatus; | ||
if (existingStatus !== 'granted') { | ||
const { status } = await ExpoNotifications.requestPermissionsAsync(); | ||
finalStatus = status; | ||
} | ||
if (finalStatus !== 'granted') { | ||
handleRegistrationError('Permission not granted to get push token for push notification!'); | ||
return; | ||
} | ||
const projectId = | ||
Constants?.expoConfig?.extra?.eas?.projectId ?? Constants?.easConfig?.projectId; | ||
if (!projectId) { | ||
handleRegistrationError('Project ID not found'); | ||
} | ||
try { | ||
const pushTokenString = ( | ||
await ExpoNotifications.getExpoPushTokenAsync({ | ||
projectId, | ||
}) | ||
).data; | ||
console.log(pushTokenString); | ||
return pushTokenString; | ||
} catch (e: unknown) { | ||
handleRegistrationError(`${e}`); | ||
} | ||
} else { | ||
handleRegistrationError('Must use physical device for push notifications'); | ||
} | ||
} | ||
|
||
export function Notifications() { | ||
const [expoPushToken, setExpoPushToken] = useState(''); | ||
const [notification, setNotification] = useState<ExpoNotifications.Notification | undefined>( | ||
undefined | ||
); | ||
const notificationListener = useRef<ExpoNotifications.Subscription>(); | ||
const responseListener = useRef<ExpoNotifications.Subscription>(); | ||
|
||
useEffect(() => { | ||
registerForPushNotificationsAsync() | ||
.then(token => setExpoPushToken(token ?? '')) | ||
.catch((error: any) => setExpoPushToken(`${error}`)); | ||
|
||
notificationListener.current = ExpoNotifications.addNotificationReceivedListener(notification => { | ||
setNotification(notification); | ||
}); | ||
|
||
responseListener.current = ExpoNotifications.addNotificationResponseReceivedListener(response => { | ||
console.log(response); | ||
}); | ||
|
||
return () => { | ||
notificationListener.current && | ||
ExpoNotifications.removeNotificationSubscription(notificationListener.current); | ||
responseListener.current && | ||
ExpoNotifications.removeNotificationSubscription(responseListener.current); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'space-around' }}> | ||
<Screen | ||
title="Notifications" | ||
/> | ||
<Text>Your Expo push token: {expoPushToken}</Text> | ||
<View style={{ alignItems: 'center', justifyContent: 'center' }}> | ||
<Text>Title: {notification && notification.request.content.title} </Text> | ||
<Text>Body: {notification && notification.request.content.body}</Text> | ||
<Text>Data: {notification && JSON.stringify(notification.request.content.data)}</Text> | ||
</View> | ||
<Button | ||
onPress={async () => { | ||
await sendPushNotification(expoPushToken); | ||
}} | ||
> | ||
<Text>Send push</Text> | ||
</Button> | ||
</View> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.