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).
|
* Catatan: expo-notifications TIDAK tersedia di Expo Go (sejak SDK 53).
|
||||||
* Semua fungsi di sini aman (return false) supaya tidak crash —
|
* Modul di-require secara LAZY + dibungkus try/catch supaya:
|
||||||
* fitur jalan penuh saat app di-build sebagai APK (development build).
|
* - 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> {
|
export async function ensureNotificationPermission(): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
|
const Notifications = await getNotifications();
|
||||||
|
if (!Notifications) return false;
|
||||||
const { status } = await Notifications.getPermissionsAsync();
|
const { status } = await Notifications.getPermissionsAsync();
|
||||||
if (status === 'granted') return true;
|
if (status === 'granted') return true;
|
||||||
const req = await Notifications.requestPermissionsAsync();
|
const req = await Notifications.requestPermissionsAsync();
|
||||||
@@ -22,6 +32,8 @@ export async function scheduleDailyReminder(
|
|||||||
minute: number
|
minute: number
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
|
const Notifications = await getNotifications();
|
||||||
|
if (!Notifications) return false;
|
||||||
await Notifications.cancelAllScheduledNotificationsAsync();
|
await Notifications.cancelAllScheduledNotificationsAsync();
|
||||||
await Notifications.scheduleNotificationAsync({
|
await Notifications.scheduleNotificationAsync({
|
||||||
content: {
|
content: {
|
||||||
@@ -42,6 +54,8 @@ export async function scheduleDailyReminder(
|
|||||||
|
|
||||||
export async function cancelReminder(): Promise<boolean> {
|
export async function cancelReminder(): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
|
const Notifications = await getNotifications();
|
||||||
|
if (!Notifications) return false;
|
||||||
await Notifications.cancelAllScheduledNotificationsAsync();
|
await Notifications.cancelAllScheduledNotificationsAsync();
|
||||||
return true;
|
return true;
|
||||||
} catch {
|
} catch {
|
||||||
|
|||||||
Reference in New Issue
Block a user