Kantongku: amankan notifikasi untuk Expo Go (aktif penuh di APK)

This commit is contained in:
Bro
2026-08-16 16:35:27 +07:00
parent 0b269621b2
commit b5cd330696
2 changed files with 50 additions and 19 deletions
+19 -4
View File
@@ -122,12 +122,19 @@ export function SettingsScreen() {
const ok = await ensureNotificationPermission();
if (!ok) {
Alert.alert(
'Izin notifikasi ditolak',
'Aktifkan izin notifikasi di pengaturan HP untuk memakai pengingat.'
'Notifikasi belum tersedia di Expo Go',
'Fitur pengingat akan aktif saat app di-build sebagai APK. (Expo Go tidak mendukung notifikasi sejak SDK 53)'
);
return;
}
const scheduled = await scheduleDailyReminder(reminderHour, 0);
if (!scheduled) {
Alert.alert(
'Gagal menjadwalkan',
'Notifikasi belum tersedia di Expo Go — akan aktif di versi APK.'
);
return;
}
await scheduleDailyReminder(reminderHour, 0);
await setSetting('reminder_enabled', '1');
await setSetting('reminder_hour', String(reminderHour));
setReminderEnabled(true);
@@ -142,7 +149,15 @@ export function SettingsScreen() {
setReminderHour(hour);
await setSetting('reminder_hour', String(hour));
if (reminderEnabled) {
await scheduleDailyReminder(hour, 0);
const scheduled = await scheduleDailyReminder(hour, 0);
if (!scheduled) {
setReminderEnabled(false);
await setSetting('reminder_enabled', '0');
Alert.alert(
'Gagal menjadwalkan',
'Notifikasi belum tersedia di Expo Go — akan aktif di versi APK.'
);
}
}
};
+31 -15
View File
@@ -1,5 +1,11 @@
import * as Notifications from 'expo-notifications';
/**
* Catatan: expo-notifications TIDAK tersedia di Expo Go (sejak SDK 53).
* Semua fungsi di sini aman (return false) supaya tidak crash —
* fitur jalan penuh saat app di-build sebagai APK (development build).
*/
export async function ensureNotificationPermission(): Promise<boolean> {
try {
const { status } = await Notifications.getPermissionsAsync();
@@ -14,21 +20,31 @@ export async function ensureNotificationPermission(): Promise<boolean> {
export async function scheduleDailyReminder(
hour: number,
minute: number
): Promise<void> {
await Notifications.cancelAllScheduledNotificationsAsync();
await Notifications.scheduleNotificationAsync({
content: {
title: 'Kantongku 👛',
body: 'Belum catat hari ini? 5 detik aja, kak — biar saldo & tabunganmu tetap kejaga!',
},
trigger: {
type: Notifications.SchedulableTriggerInputTypes.DAILY,
hour,
minute,
},
});
): Promise<boolean> {
try {
await Notifications.cancelAllScheduledNotificationsAsync();
await Notifications.scheduleNotificationAsync({
content: {
title: 'Kantongku 👛',
body: 'Belum catat hari ini? 5 detik aja, kak — biar saldo & tabunganmu tetap kejaga!',
},
trigger: {
type: Notifications.SchedulableTriggerInputTypes.DAILY,
hour,
minute,
},
});
return true;
} catch {
return false;
}
}
export async function cancelReminder(): Promise<void> {
await Notifications.cancelAllScheduledNotificationsAsync();
export async function cancelReminder(): Promise<boolean> {
try {
await Notifications.cancelAllScheduledNotificationsAsync();
return true;
} catch {
return false;
}
}