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(); const ok = await ensureNotificationPermission();
if (!ok) { if (!ok) {
Alert.alert( Alert.alert(
'Izin notifikasi ditolak', 'Notifikasi belum tersedia di Expo Go',
'Aktifkan izin notifikasi di pengaturan HP untuk memakai pengingat.' '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; return;
} }
await scheduleDailyReminder(reminderHour, 0);
await setSetting('reminder_enabled', '1'); await setSetting('reminder_enabled', '1');
await setSetting('reminder_hour', String(reminderHour)); await setSetting('reminder_hour', String(reminderHour));
setReminderEnabled(true); setReminderEnabled(true);
@@ -142,7 +149,15 @@ export function SettingsScreen() {
setReminderHour(hour); setReminderHour(hour);
await setSetting('reminder_hour', String(hour)); await setSetting('reminder_hour', String(hour));
if (reminderEnabled) { 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'; 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> { export async function ensureNotificationPermission(): Promise<boolean> {
try { try {
const { status } = await Notifications.getPermissionsAsync(); const { status } = await Notifications.getPermissionsAsync();
@@ -14,21 +20,31 @@ export async function ensureNotificationPermission(): Promise<boolean> {
export async function scheduleDailyReminder( export async function scheduleDailyReminder(
hour: number, hour: number,
minute: number minute: number
): Promise<void> { ): Promise<boolean> {
await Notifications.cancelAllScheduledNotificationsAsync(); try {
await Notifications.scheduleNotificationAsync({ await Notifications.cancelAllScheduledNotificationsAsync();
content: { await Notifications.scheduleNotificationAsync({
title: 'Kantongku 👛', content: {
body: 'Belum catat hari ini? 5 detik aja, kak — biar saldo & tabunganmu tetap kejaga!', title: 'Kantongku 👛',
}, body: 'Belum catat hari ini? 5 detik aja, kak — biar saldo & tabunganmu tetap kejaga!',
trigger: { },
type: Notifications.SchedulableTriggerInputTypes.DAILY, trigger: {
hour, type: Notifications.SchedulableTriggerInputTypes.DAILY,
minute, hour,
}, minute,
}); },
});
return true;
} catch {
return false;
}
} }
export async function cancelReminder(): Promise<void> { export async function cancelReminder(): Promise<boolean> {
await Notifications.cancelAllScheduledNotificationsAsync(); try {
await Notifications.cancelAllScheduledNotificationsAsync();
return true;
} catch {
return false;
}
} }