- google-services.json - utils/notifications.ts Fix: - app.config.js : tambah access googleServicesFile - app/_layout.tsx : tambah untuk push notifikasi - package.json: install install expo-notifications expo-device expo-constants ### No Issue
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
// utils/notifications.ts
|
|
import * as Device from 'expo-device';
|
|
import * as Notifications from 'expo-notifications';
|
|
import Constants from 'expo-constants';
|
|
|
|
Notifications.setNotificationHandler({
|
|
handleNotification: async () => ({
|
|
shouldShowAlert: true,
|
|
shouldPlaySound: true,
|
|
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;
|
|
} |