44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import Constants from 'expo-constants';
|
|
import * as Device from 'expo-device';
|
|
import * as Notifications from 'expo-notifications';
|
|
import { Platform } from "react-native";
|
|
|
|
export async function registerForPushNotificationsAsync() {
|
|
if (Platform.OS === 'android') {
|
|
await Notifications.setNotificationChannelAsync('default', {
|
|
name: 'default',
|
|
importance: Notifications.AndroidImportance.MAX,
|
|
vibrationPattern: [0, 250, 250, 250, 250, 250, 250, 250, 500],
|
|
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') {
|
|
throw new Error('Permission not granted')
|
|
}
|
|
|
|
const projectId = Constants?.expoConfig?.extra?.eas?.projectId ?? Constants?.expoConfig?.extra?.projectId
|
|
if (!projectId) {
|
|
throw new Error('Project ID not found')
|
|
}
|
|
|
|
try {
|
|
const pushTokenString = (await Notifications.getExpoPushTokenAsync({ projectId })).data
|
|
console.log(pushTokenString)
|
|
return pushTokenString
|
|
} catch (error) {
|
|
throw new Error(`Error getting push token: ${error}`)
|
|
}
|
|
}else{
|
|
throw new Error('Must use physical device for Push Notifications')
|
|
}
|
|
}
|