40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import * as Notifications from 'expo-notifications';
|
|
import { Platform } from 'react-native';
|
|
|
|
Notifications.setNotificationHandler({
|
|
handleNotification: async () => ({
|
|
shouldPlaySound: true,
|
|
shouldSetBadge: true,
|
|
shouldShowBanner: true,
|
|
shouldShowList: true,
|
|
}),
|
|
});
|
|
|
|
function HandleRegistrationError(errorMessage: string) {
|
|
alert(errorMessage);
|
|
throw new Error(errorMessage);
|
|
}
|
|
|
|
export async function RegisterForLocalNotificationsAsync() {
|
|
if (Platform.OS === 'android') {
|
|
await Notifications.setNotificationChannelAsync('default', {
|
|
name: 'default',
|
|
importance: Notifications.AndroidImportance.MAX
|
|
});
|
|
}
|
|
|
|
const { status: existingStatus } = await Notifications.getPermissionsAsync();
|
|
|
|
let finalStatus = existingStatus;
|
|
|
|
if (existingStatus !== 'granted') {
|
|
const { status } = await Notifications.requestPermissionsAsync();
|
|
finalStatus = status;
|
|
}
|
|
|
|
if (finalStatus !== 'granted') {
|
|
HandleRegistrationError('Permission not granted for local notifications');
|
|
return;
|
|
}
|
|
}
|