Files
hipmi-mobile/utils/notifications.ts
bagasbanuna 868e96a54a Try to notification
### Issue: package import * as Notifications from expo-notifications;
2025-11-18 17:46:33 +08:00

53 lines
1.3 KiB
TypeScript

// utils/notifications.ts
import * as Notifications from 'expo-notifications';
import Constants from 'expo-constants';
import * as Device from 'expo-device';
// Notifications.setNotificationHandler({
// handleNotification: async () => ({
// shouldShowAlert: true,
// shouldPlaySound: true,
// shouldSetBadge: false,
// shouldShowBanner: true,
// shouldShowList: true,
// }),
// });
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldPlaySound: false,
shouldSetBadge: false,
shouldShowBanner: true,
shouldShowList: true,
}),
});
export async function registerForPushNotificationsAsync() {
if (!Device.isDevice) {
console.warn("Push notifications don't work on simulator");
return null;
}
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== 'granted') {
console.warn('Permission not granted for push notifications');
return null;
}
const projectId = Constants?.expoConfig?.extra?.eas?.projectId;
const token = (await Notifications.getExpoPushTokenAsync({
projectId,
})).data;
console.log("Expo Push Token:", token);
return token;
}