Kantongku: amankan notifikasi untuk Expo Go (aktif penuh di APK)
This commit is contained in:
@@ -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.'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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,7 +20,8 @@ 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> {
|
||||||
|
try {
|
||||||
await Notifications.cancelAllScheduledNotificationsAsync();
|
await Notifications.cancelAllScheduledNotificationsAsync();
|
||||||
await Notifications.scheduleNotificationAsync({
|
await Notifications.scheduleNotificationAsync({
|
||||||
content: {
|
content: {
|
||||||
@@ -27,8 +34,17 @@ export async function scheduleDailyReminder(
|
|||||||
minute,
|
minute,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function cancelReminder(): Promise<void> {
|
export async function cancelReminder(): Promise<boolean> {
|
||||||
|
try {
|
||||||
await Notifications.cancelAllScheduledNotificationsAsync();
|
await Notifications.cancelAllScheduledNotificationsAsync();
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user