Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: save reminder unconditionally and fix notification settings dialog

kashaf-ansari-devc4b969dad85a
diff --git a/apps/mobile/app/screens/add-reminder/index.tsx b/apps/mobile/app/screens/add-reminder/index.tsx
index 384e4e302..c73fd94c2 100644
--- a/apps/mobile/app/screens/add-reminder/index.tsx
+++ b/apps/mobile/app/screens/add-reminder/index.tsx
@@ -33,7 +33,6 @@ import DatePicker from "react-native-date-picker";
import DateTimePickerModal from "react-native-modal-datetime-picker";
import { SafeAreaView } from "react-native-safe-area-context";
import { db } from "../../common/database";
-import { Dialog } from "../../components/dialog";
import { Header } from "../../components/header";
import { Button } from "../../components/ui/button";
import { ReminderTime } from "../../components/ui/reminder-time";
@@ -257,9 +256,6 @@ export default function AddReminder(props: NavigationProps<"AddReminder">) {
if (!date && reminderMode !== ReminderModes.Permanent) return;
- if (!(await Notifications.checkAndRequestPermissions(true)))
- throw new Error(strings.noNotificationPermission());
-
date.setSeconds(0, 0);
const reminderId = await db.reminders?.add({
@@ -285,7 +281,12 @@ export default function AddReminder(props: NavigationProps<"AddReminder">) {
type: _reminder?.type
});
}
- Notifications.scheduleNotification(_reminder as Reminder);
+
+ const hasPermission = await Notifications.checkAndRequestPermissions(true);
+ if (hasPermission) {
+ Notifications.scheduleNotification(_reminder as Reminder);
+ }
+
Navigation.queueRoutesForUpdate();
useRelationStore.getState().update();
handleBackNavigation();
diff --git a/apps/mobile/app/services/notifications.ts b/apps/mobile/app/services/notifications.ts
index 7d67fd07f..649c386b2 100644
--- a/apps/mobile/app/services/notifications.ts
+++ b/apps/mobile/app/services/notifications.ts
@@ -600,7 +600,7 @@ async function displayNotification({
}
}
-function openSettingsDialog(context: string) {
+function openSettingsDialog(context = "global") {
return new Promise<boolean>((resolve) => {
presentDialog({
title: strings.notificationsDisabled(),
@@ -644,16 +644,23 @@ function waitForAppFocusAfterSettings(timeoutMs = 120000): Promise<void> {
});
}
+async function hasPermission(): Promise<boolean> {
+ const permissionStatus = await notifee.getNotificationSettings();
+ if (Platform.OS === "android") {
+ return (
+ permissionStatus.authorizationStatus === AuthorizationStatus.AUTHORIZED &&
+ permissionStatus.android.alarm === 1
+ );
+ }
+ return permissionStatus.authorizationStatus === AuthorizationStatus.AUTHORIZED;
+}
+
async function checkAndRequestPermissions(
promptUser?: boolean
): Promise<boolean> {
let permissionStatus = await notifee.getNotificationSettings();
if (Platform.OS === "android") {
- const hasNotificationPermission =
- permissionStatus.authorizationStatus === AuthorizationStatus.AUTHORIZED;
- const hasAlarmPermission = permissionStatus.android.alarm === 1;
-
- if (hasNotificationPermission && hasAlarmPermission) {
+ if (await hasPermission()) {
return true;
}
@@ -663,10 +670,13 @@ async function checkAndRequestPermissions(
// Wait for user to return from exact alarm settings
if (permissionStatus.android.alarm !== 1) {
- const waitForAppFocus = waitForAppFocusAfterSettings();
-
- await notifee.openAlarmPermissionSettings();
- await waitForAppFocus;
+ try {
+ const waitForAppFocus = waitForAppFocusAfterSettings();
+ await notifee.openAlarmPermissionSettings();
+ await waitForAppFocus;
+ } catch (err) {
+ /* empty */
+ }
}
permissionStatus = await notifee.getNotificationSettings();
@@ -679,11 +689,15 @@ async function checkAndRequestPermissions(
}
if (promptUser) {
- const openSettings = await openSettingsDialog("local");
+ const openSettings = await openSettingsDialog("global");
if (openSettings) {
- const waitForAppFocus = waitForAppFocusAfterSettings();
- await notifee.openNotificationSettings();
- await waitForAppFocus;
+ try {
+ const waitForAppFocus = waitForAppFocusAfterSettings();
+ await notifee.openNotificationSettings();
+ await waitForAppFocus;
+ } catch (err) {
+ /* empty */
+ }
permissionStatus = await notifee.getNotificationSettings();
const finalAuthorized =
permissionStatus.authorizationStatus ===
@@ -705,7 +719,7 @@ async function checkAndRequestPermissions(
return true;
}
if (promptUser) {
- await openSettingsDialog("local");
+ await openSettingsDialog("global");
}
return false;
}