Kantongku: lazy-load expo-notifications agar tidak crash di Expo Go
This commit is contained in:
@@ -1,13 +1,23 @@
|
||||
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).
|
||||
* Modul di-require secara LAZY + dibungkus try/catch supaya:
|
||||
* - tidak crash di Expo Go (runtime error),
|
||||
* - fitur jalan penuh saat app di-build sebagai APK.
|
||||
*/
|
||||
|
||||
async function getNotifications(): Promise<any | null> {
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
return require('expo-notifications');
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function ensureNotificationPermission(): Promise<boolean> {
|
||||
try {
|
||||
const Notifications = await getNotifications();
|
||||
if (!Notifications) return false;
|
||||
const { status } = await Notifications.getPermissionsAsync();
|
||||
if (status === 'granted') return true;
|
||||
const req = await Notifications.requestPermissionsAsync();
|
||||
@@ -22,6 +32,8 @@ export async function scheduleDailyReminder(
|
||||
minute: number
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const Notifications = await getNotifications();
|
||||
if (!Notifications) return false;
|
||||
await Notifications.cancelAllScheduledNotificationsAsync();
|
||||
await Notifications.scheduleNotificationAsync({
|
||||
content: {
|
||||
@@ -42,6 +54,8 @@ export async function scheduleDailyReminder(
|
||||
|
||||
export async function cancelReminder(): Promise<boolean> {
|
||||
try {
|
||||
const Notifications = await getNotifications();
|
||||
if (!Notifications) return false;
|
||||
await Notifications.cancelAllScheduledNotificationsAsync();
|
||||
return true;
|
||||
} catch {
|
||||
|
||||
Reference in New Issue
Block a user