36 lines
964 B
TypeScript
36 lines
964 B
TypeScript
import * as Device from 'expo-device';
|
|
import * as Notifications from 'expo-notifications';
|
|
import { Platform } from 'react-native';
|
|
|
|
export async function registerForPushNotificationsAsync() {
|
|
if (!Device.isDevice) {
|
|
alert('Must use physical device for Push Notifications');
|
|
return null;
|
|
}
|
|
|
|
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 null;
|
|
}
|
|
|
|
const token = (await Notifications.getExpoPushTokenAsync()).data;
|
|
console.log('Expo Push Token:', token);
|
|
|
|
if (Platform.OS === 'android') {
|
|
await Notifications.setNotificationChannelAsync('default', {
|
|
name: 'default',
|
|
importance: Notifications.AndroidImportance.MAX,
|
|
});
|
|
}
|
|
|
|
return token;
|
|
}
|