A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
mobile: remove react-native-push-notifications dep
ammarahm-ede7d0c956bcc8
diff --git a/apps/mobile/app/components/sheets/reminder/index.tsx b/apps/mobile/app/components/sheets/reminder/index.tsxindex 39c55a240..aa0b3ed65 100644--- a/apps/mobile/app/components/sheets/reminder/index.tsx+++ b/apps/mobile/app/components/sheets/reminder/index.tsx@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public Licensealong with this program. If not, see <http://www.gnu.org/licenses/>.*/import React, { RefObject, useRef, useState } from "react";-import { ScrollView, View } from "react-native";+import { Platform, ScrollView, View } from "react-native";import ActionSheet from "react-native-actions-sheet";import DateTimePickerModal from "react-native-modal-datetime-picker";import {@@ -26,20 +26,11 @@ import {} from "../../../services/event-manager";import { useThemeStore } from "../../../stores/use-theme-store";import { SIZE } from "../../../utils/size";-import DialogHeader from "../../dialog/dialog-header";import { Button } from "../../ui/button";import Input from "../../ui/input";import Notifications, { Reminder } from "../../../services/notifications";-import { Notice } from "../../ui/notice";-import Paragraph from "../../ui/typography/paragraph";-import notifee, {- AndroidStyle,- RepeatFrequency,- Trigger,- TriggerType-} from "@notifee/react-native";type ReminderSheetProps = {actionSheetRef: RefObject<ActionSheet>;close?: () => void;@@ -47,11 +38,17 @@ type ReminderSheetProps = {reminder?: Reminder;};-const ReminderModes = {- Once: "once",- Recurring: "recurring",- Permanent: "permanent"-};+const ReminderModes =+ Platform.OS === "ios"+ ? {+ Once: "once",+ Recurring: "recurring"+ }+ : {+ Once: "once",+ Recurring: "recurring",+ Permanent: "permanent"+ };const RecurringModes = {Weekly: "weekly",@@ -83,7 +80,9 @@ export default function ReminderSheet({reminder}: ReminderSheetProps) {const colors = useThemeStore((state) => state.colors);- const [reminderMode, setReminderMode] = useState(ReminderModes.Once);+ const [reminderMode, setReminderMode] = useState<string | undefined>(+ ReminderModes.Once+ );const [recurringMode, setRecurringMode] = useState(RecurringModes.Weekly);const [selectedDays, setSelectedDays] = useState<number[]>([]);const [date, setDate] = useState<Date>();@@ -270,7 +269,7 @@ export default function ReminderSheet({><DateTimePickerModalisVisible={isDatePickerVisible}- mode="datetime"+ mode="date"onConfirm={handleConfirm}onCancel={hideDatePicker}date={date || new Date(Date.now())}@@ -282,24 +281,25 @@ export default function ReminderSheet({onConfirm={handleConfirm}onCancel={hideDatePicker}date={date || new Date(Date.now())}- timePickerModeAndroid="spinner"/>- <Button- style={{- width: "48.5%"- }}- title={date ? date.toLocaleDateString() : "Select date"}- type={date ? "grayAccent" : "grayBg"}- icon="calendar"- onPress={() => {- showDatePicker();- }}- />+ {reminderMode === ReminderModes.Recurring ? null : (+ <Button+ style={{+ width: "48.5%"+ }}+ title={date ? date.toLocaleDateString() : "Select date"}+ type={date ? "grayAccent" : "grayBg"}+ icon="calendar"+ onPress={() => {+ showDatePicker();+ }}+ />+ )}<Buttonstyle={{- width: "48.5%"+ width: reminderMode === ReminderModes.Recurring ? "100%" : "48.5%"}}title={time || "Select time"}type={time ? "grayAccent" : "grayBg"}@@ -354,17 +354,6 @@ export default function ReminderSheet({</View>)}- <Paragraph- style={{- color: colors.icon,- fontSize: SIZE.xs,- paddingVertical: 12- }}- >- Permanent reminders are pinned at top of your notifications permanentaly- until they are removed from the app.- </Paragraph>-<Buttonstyle={{width: "100%"@@ -373,10 +362,15 @@ export default function ReminderSheet({type="accent"onPress={async () => {if (!(await Notifications.checkAndRequestPermissions())) return;- if (!date || !title.current) return;+ if (+ (!date && reminderMode !== ReminderModes.Permanent) ||+ !title.current+ )+ return;Notifications.scheduleNotification({id: "test_1",- date: date,+ type: "reminder",+ date: date?.getTime(),dateCreated: Date.now(),dateModified: Date.now(),priority: reminderNotificationMode as any,@@ -384,7 +378,7 @@ export default function ReminderSheet({details: details.current,recurringMode: recurringMode as any,selectedDays: selectedDays,- type: reminderMode as any+ mode: reminderMode as any});}}/>diff --git a/apps/mobile/app/hooks/use-actions.js b/apps/mobile/app/hooks/use-actions.jsindex 6ff84e53b..045333a35 100644--- a/apps/mobile/app/hooks/use-actions.js+++ b/apps/mobile/app/hooks/use-actions.js@@ -86,7 +86,7 @@ export const useActions = ({ close = () => null, item }) => {return;}- let index = pinned.findIndex((notif) => notif.tag === item.id);+ let index = pinned.findIndex((notif) => notif.id === item.id);if (index !== -1) {setNotifPinned(pinned[index]);} else {@@ -176,14 +176,14 @@ export const useActions = ({ close = () => null, item }) => {if (item.locked) return;let html = await db.notes.note(item.id).content();let text = await toTXT(item);- Notifications.present({+ Notifications.displayNotification({title: item.title,message: item.headline || text,subtitle: item.headline || text,bigText: html,ongoing: true,actions: ["UNPIN"],- tag: item.id+ id: item.id});await sleep(1000);await Notifications.get();diff --git a/apps/mobile/app/services/notifications.ts b/apps/mobile/app/services/notifications.tsindex 40f57fda3..334f59997 100644--- a/apps/mobile/app/services/notifications.ts+++ b/apps/mobile/app/services/notifications.ts@@ -19,19 +19,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.import notifee, {AndroidStyle,+ AuthorizationStatus,+ DisplayedNotification,+ EventType,RepeatFrequency,Trigger,TriggerType,- AuthorizationStatus+ Event} from "@notifee/react-native";import dayjs from "dayjs";import { Platform } from "react-native";-import DeviceInfo from "react-native-device-info";-import PushNotification, {- Importance,- PushNotificationDeliveredObject,- PushNotificationObject-} from "react-native-push-notification";import { db } from "../common/database";import { MMKV } from "../common/database/mmkv";import { editorState } from "../screens/editor/tiptap/utils";@@ -42,10 +39,152 @@ import { DDS } from "./device-detection";import { eSendEvent } from "./event-manager";import SettingsService from "./settings";-const NOTIFICATION_TAG = "notesnook";-const CHANNEL_ID = "com.streetwriters.notesnook";-let pinned: PushNotificationDeliveredObject[] = [];+type ReferenceNode = {+ id: string;+ parentId?: string;+ type: string;+};++export type Reminder = {+ id: string;+ type: string;+ title: string;+ details?: string;+ priority: "silent" | "vibrate" | "urgent";+ date?: number;+ mode: "recurring" | "once" | "permanent";+ recurringMode: "weekly" | "monthly" | "daily";+ selectedDays: number[];+ dateCreated: number;+ dateModified: number;+ references?: ReferenceNode[];+};++let pinned: DisplayedNotification[] = [];+const onEvent = async ({ type, detail }: Event) => {+ const { notification, pressAction, input } = detail;+ if (type === EventType.PRESS) {+ if (notification?.data?.type !== "pinnedNote") return;+ editorState().movedAway = false;+ MMKV.removeItem("appState");+ if (useNoteStore?.getState()?.loading === false) {+ await db.init();+ await db.notes?.init();+ loadNote(notification?.id as string, false);+ return;+ }+ const unsub = useNoteStore.subscribe(+ (loading) => {+ if (loading === false) {+ loadNote(notification?.id as string, true);+ }+ unsub();+ },+ (state) => state.loading+ );+ }++ if (type === EventType.ACTION_PRESS) {+ switch (pressAction?.id) {+ case "UNPIN":+ remove(notification?.id as string);+ break;+ case "Hide":+ unpinQuickNote();+ break;+ case "ReplyInput":+ displayNotification({+ title: "Quick note",+ message: 'Tap on "Take note" to add a note.',+ ongoing: true,+ actions: ["ReplyInput", "Hide"],+ id: "notesnook_note_input",+ reply_button_text: "Take note",+ reply_placeholder_text: "Write something..."+ });+ await db.init();+ await db.notes?.add({+ content: {+ type: "tiptap",+ data: `<p>${input} </p>`+ }+ });+ await db.notes?.init();+ useNoteStore.getState().setNotes();+ break;+ }+ }+};++async function scheduleNotification(reminder: Reminder, payload?: string) {+ try {+ const { title, details, priority } = reminder;+ const triggers = getTriggers(reminder);+ if (!triggers && reminder.mode === "permanent") {+ displayNotification({+ id: reminder.id,+ title: title,+ message: details || "",+ ongoing: true,+ subtitle: details || ""+ });+ return;+ }+ await clearAllPendingTriggersForId(reminder.id);+ if (!triggers) return;+ for (const trigger of triggers) {+ await notifee.createTriggerNotification(+ {+ id: trigger.id,+ title: title,+ body: details,+ data: {+ type: "reminder",+ payload: payload || ""+ },+ subtitle: details,+ android: {+ channelId: await getChannelId(priority),+ smallIcon: "ic_stat_name",+ pressAction: {+ id: "default",+ mainComponent: "notesnook"+ },+ style: !details+ ? undefined+ : {+ type: AndroidStyle.BIGTEXT,+ text: details+ }+ },+ ios: {+ sound: priority === "urgent" ? "default" : undefined,+ interruptionLevel: priority === "silent" ? "passive" : "active"+ }+ },+ trigger+ );+ }+ } catch (e) {+ console.log(e);+ }+}++function loadNote(id: string, jump: boolean) {+ if (!id || id === "notesnook_note_input") return;+ const note = db.notes?.note(id).data;+ if (!DDS.isTab && jump) {+ tabBarRef.current?.goToPage(1);+ }+ eSendEvent("loadingNote", note);+ setTimeout(() => {+ eSendEvent(eOnLoadNote, note);+ if (!jump && !DDS.isTab) {+ tabBarRef.current?.goToPage(1);+ }+ }, 2000);+}async function getChannelId(id: "silent" | "vibrate" | "urgent" | "default") {switch (id) {case "default":@@ -75,18 +214,68 @@ async function getChannelId(id: "silent" | "vibrate" | "urgent" | "default") {}}-export type Reminder = {- id: string;- title: string;- details?: string;- priority: "silent" | "vibrate" | "urgent";- date: Date;- type: "recurring" | "once" | "permanent";- recurringMode: "weekly" | "monthly" | "daily";- selectedDays: number[];- dateCreated: number;- dateModified: number;-};+async function displayNotification({+ title,+ message,+ subtitle,+ bigText,+ actions = [],+ ongoing,+ reply_placeholder_text,+ reply_button_text,+ id+}: {+ title?: string;+ message: string;+ subtitle?: string;+ bigText?: string;+ actions?: Array<string>;+ ongoing?: boolean;+ reply_placeholder_text?: string;+ reply_button_text?: string;+ id?: string;+}) {+ if (!(await checkAndRequestPermissions())) return;+ try {+ await notifee.displayNotification({+ id: id,+ title: title,+ body: message,+ subtitle: subtitle,+ data: {+ type: reply_placeholder_text ? "quickNote" : "pinnedNote"+ },+ android: {+ ongoing: ongoing,+ localOnly: true,+ channelId: await getChannelId("default"),+ autoCancel: false,+ actions: actions?.map((action) => ({+ pressAction: {+ id: action+ },+ title:+ action === "ReplyInput" ? (reply_button_text as string) : action,+ input:+ action !== "ReplyInput"+ ? undefined+ : {+ placeholder: reply_placeholder_text,+ allowFreeFormInput: true+ }+ })),+ style: !bigText+ ? undefined+ : {+ type: AndroidStyle.BIGTEXT,+ text: bigText+ }+ }+ });+ } catch (e) {+ console.log(e);+ }+}async function checkAndRequestPermissions() {let permissionStatus = await notifee.getNotificationSettings();@@ -109,6 +298,11 @@ async function checkAndRequestPermissions() {)return true;return false;+ } else {+ permissionStatus = await notifee.requestPermission();+ if (permissionStatus.authorizationStatus === AuthorizationStatus.AUTHORIZED)+ return true;+ return false;}}@@ -120,7 +314,7 @@ function getTriggers(case "once":return [{- timestamp: date?.getTime() as number,+ timestamp: date as number,type: TriggerType.TIMESTAMP,id: reminder.id,alarmManager: {@@ -135,7 +329,7 @@ function getTriggers(case "daily":return [{- timestamp: date?.getTime() as number,+ timestamp: date as number,type: TriggerType.TIMESTAMP,repeatFrequency: RepeatFrequency.DAILY,id: reminder.id,@@ -148,7 +342,7 @@ function getTriggers(return selectedDays.length === 7? [{- timestamp: date.getTime() as number,+ timestamp: date as number,type: TriggerType.TIMESTAMP,repeatFrequency: RepeatFrequency.DAILY,id: reminder.id,@@ -170,7 +364,7 @@ function getTriggers(return selectedDays.length === 31? [{- timestamp: date?.getTime() as number,+ timestamp: date as number,type: TriggerType.TIMESTAMP,repeatFrequency: RepeatFrequency.DAILY,id: reminder.id,@@ -189,12 +383,15 @@ function getTriggers(}}));}-- break;}}}+async function unpinQuickNote() {+ remove("notesnook_note_input");+ SettingsService.set({ notifNotes: false });+}+async function removeScheduledNotification(reminder: Reminder, day: number) {return notifee.cancelTriggerNotification(day ? `${reminder.id}_${day}` : reminder.id@@ -214,269 +411,59 @@ async function clearAllPendingTriggersForId(_id: string) {}}-async function scheduleNotification(reminder: Reminder) {- try {- const { title, details, priority } = reminder;- const triggers = getTriggers(reminder);- if (!triggers && reminder.type === "permanent") {- present({- tag: reminder.id,- title: title,- message: details || "",- ongoing: true,- subtitle: details || ""- });- return;- }- await clearAllPendingTriggersForId(reminder.id);- if (!triggers) return;- for (const trigger of triggers) {- console.log(trigger);- await notifee.createTriggerNotification(- {- id: trigger.id,- title: title,- body: details,- android: {- channelId: await getChannelId(priority),- smallIcon: "ic_stat_name",- pressAction: {- id: "default",- mainComponent: "callofwriting"- },- style: !details- ? undefined- : {- type: AndroidStyle.BIGTEXT,- text: details- }- }- },- trigger- );- }- } catch (e) {- console.log(e);- }+function clearAll() {+ notifee.cancelDisplayedNotifications();}-function loadNote(id: string, jump: boolean) {- if (!id || id === "notesnook_note_input") return;- const note = db.notes?.note(id).data;- if (!DDS.isTab && jump) {- tabBarRef.current?.goToPage(1);- }- eSendEvent("loadingNote", note);- setTimeout(() => {- eSendEvent(eOnLoadNote, note);- if (!jump && !DDS.isTab) {- tabBarRef.current?.goToPage(1);- }- }, 2000);+function getPinnedNotes(): DisplayedNotification[] {+ return pinned;}-function init() {- if (Platform.OS === "ios") return;- PushNotification.configure({- onNotification: async function (notification) {- editorState().movedAway = false;- MMKV.removeItem("appState");- if (useNoteStore?.getState()?.loading === false) {- await db.init();- await db.notes?.init();- loadNote(- (notification as unknown as PushNotificationDeliveredObject).tag,- false- );- return;- }-- const unsub = useNoteStore.subscribe(- (loading) => {- if (loading === false) {- loadNote(- (notification as unknown as PushNotificationDeliveredObject).tag,- true- );- }- unsub();- },- (state) => state.loading- );- },- onAction: async function (notification) {- switch (notification.action) {- case "UNPIN":- remove(- (notification as unknown as PushNotificationDeliveredObject).tag,- notification.id- );- break;- case "Hide":- unpinQuickNote();- break;- case "ReplyInput":- present({- title: "Quick note",- message: 'Tap on "Take note" to add a note.',- ongoing: true,- actions: ["ReplyInput", "Hide"],- tag: "notesnook_note_input",- reply_button_text: "Take note",- reply_placeholder_text: "Write something...",- id: 256266- });- await db.init();- await db.notes?.add({- content: {- type: "tiptap",- data: `<p>${- (- notification as unknown as PushNotificationDeliveredObject & {- reply_text: string;- }- ).reply_text- } </p>`- }- });- await db.notes?.init();- useNoteStore.getState().setNotes();-- break;- }- },- popInitialNotification: true,- requestPermissions: false+function get(): Promise<DisplayedNotification[]> {+ return new Promise((resolve) => {+ if (Platform.OS === "ios") resolve([]);+ notifee.getDisplayedNotifications().then((notifications) => {+ pinned = notifications;+ resolve(notifications);+ });});+}- PushNotification.createChannel(- {- channelId: CHANNEL_ID,- channelName: "Notesnook",- playSound: false,- soundName: "default",- importance: Importance.HIGH,- vibrate: true- },- (created) => console.log(`Android Notification Channel Status: ${created}`)- );+function init() {+ if (Platform.OS === "ios") return;+ notifee.onBackgroundEvent(onEvent);+ notifee.onForegroundEvent(onEvent);}-function remove(tag: string, id: string) {- PushNotification.clearLocalNotification(- tag || NOTIFICATION_TAG,- parseInt(id)- );+async function remove(id: string) {+ await notifee.cancelNotification(id);get().then(() => {eSendEvent("onUpdate", "unpin");});}-async function hasPermissions() {- if (DeviceInfo.getApiLevelSync() < 33) return true;- //@ts-ignore- if (!(await PushNotification.areNotificationsEnabled())) {- //@ts-ignore- const result = await PushNotification.askForPermission();- if (result) return true;- return true;- }- return true;-}-async function pinQuickNote(launch: boolean) {- if (!(await hasPermissions())) return;+ if (!(await checkAndRequestPermissions())) return;get().then((items) => {- const notif = items.filter((i) => i.tag === "notesnook_note_input");- if (notif && launch) {+ const notification = items.filter((n) => n.id === "notesnook_note_input");+ if (notification && launch) {return;}- present({+ displayNotification({title: "Quick note",message: 'Tap on "Take note" to add a note.',ongoing: true,actions: ["ReplyInput", "Hide"],- tag: "notesnook_note_input",reply_button_text: "Take note",reply_placeholder_text: "Write something...",- id: 256266- });- });-}--async function unpinQuickNote() {- remove("notesnook_note_input", 256266 + "");- SettingsService.set({ notifNotes: false });-}--async function present({- title,- message,- subtitle,- bigText,- actions = [],- ongoing,- tag,- reply_placeholder_text,- reply_button_text,- id-}: {- title?: string;- message: string;- subtitle?: string;- bigText?: string;- actions?: Array<string>;- ongoing?: boolean;- tag?: string;- reply_placeholder_text?: string;- reply_button_text?: string;- id?: number;-}) {- if (!(await hasPermissions())) return;- PushNotification.localNotification({- id: id,- channelId: CHANNEL_ID,- tag: tag || NOTIFICATION_TAG,- ongoing: ongoing,- visibility: "private",- ignoreInForeground: false,- actions: actions,- title: title,- subText: subtitle,- bigText: bigText,- message: message,- invokeApp: false,- autoCancel: false,- smallIcon: "ic_stat_name",- reply_placeholder_text,- reply_button_text- } as PushNotificationObject & {- reply_placeholder_text: string;- });-}--function clearAll() {- PushNotification.cancelAllLocalNotifications();- PushNotification.clearAllNotifications();-}--function getPinnedNotes(): PushNotificationDeliveredObject[] {- return pinned;-}--function get(): Promise<PushNotificationDeliveredObject[]> {- return new Promise((resolve) => {- if (Platform.OS === "ios") resolve([]);- PushNotification.getDeliveredNotifications((n) => {- pinned = n;- resolve(n);+ id: "notesnook_note_input"});});}const Notifications = {init,- present,+ displayNotification,clearAll,remove,get,diff --git a/apps/mobile/native/package.json b/apps/mobile/native/package.jsonindex 5f30a900e..6f10c44bf 100644--- a/apps/mobile/native/package.json+++ b/apps/mobile/native/package.json@@ -38,7 +38,6 @@"react-native-mmkv-storage": "^0.8.0","react-native-orientation": "https://github.com/yamill/react-native-orientation.git","react-native-privacy-snapshot": "https://github.com/standardnotes/react-native-privacy-snapshot.git",- "react-native-push-notification": "github:ammarahm-ed/react-native-push-notification","react-native-reanimated": "2.13.0","react-native-safe-area-context": "^4.3.1","react-native-scoped-storage": "^1.9.3",diff --git a/apps/mobile/package-lock.json b/apps/mobile/package-lock.jsonindex 9ac663c69..65439d3e9 100644--- a/apps/mobile/package-lock.json+++ b/apps/mobile/package-lock.json@@ -97,7 +97,6 @@"react-native-modal-datetime-picker": "14.0.0","react-native-orientation": "https://github.com/yamill/react-native-orientation.git","react-native-privacy-snapshot": "https://github.com/standardnotes/react-native-privacy-snapshot.git",- "react-native-push-notification": "github:ammarahm-ed/react-native-push-notification","react-native-reanimated": "2.13.0","react-native-safe-area-context": "^4.3.1","react-native-scoped-storage": "^1.9.3",@@ -18384,15 +18383,6 @@"react-native-svg": "*"}},- "node_modules/react-native-push-notification": {- "version": "8.1.1",- "resolved": "git+ssh://[email protected]/ammarahm-ed/react-native-push-notification.git#2e5face06c33ef2faf5917d3a641cd65bbdaa837",- "license": "MIT",- "peerDependencies": {- "@react-native-community/push-notification-ios": "^1.10.1",- "react-native": ">=0.33"- }- },"node_modules/react-native-qrcode-svg": {"version": "6.1.2","resolved": "https://registry.npmjs.org/react-native-qrcode-svg/-/react-native-qrcode-svg-6.1.2.tgz",@@ -24314,7 +24304,6 @@"react-native-modal-datetime-picker": "14.0.0","react-native-orientation": "https://github.com/yamill/react-native-orientation.git","react-native-privacy-snapshot": "https://github.com/standardnotes/react-native-privacy-snapshot.git",- "react-native-push-notification": "github:ammarahm-ed/react-native-push-notification","react-native-reanimated": "2.13.0","react-native-safe-area-context": "^4.3.1","react-native-scoped-storage": "^1.9.3",@@ -35052,10 +35041,6 @@"prop-types": "^15.7.2"}},- "react-native-push-notification": {- "version": "git+ssh://[email protected]/ammarahm-ed/react-native-push-notification.git#2e5face06c33ef2faf5917d3a641cd65bbdaa837",- "from": "react-native-push-notification@github:ammarahm-ed/react-native-push-notification"- },"react-native-qrcode-svg": {"version": "6.1.2","resolved": "https://registry.npmjs.org/react-native-qrcode-svg/-/react-native-qrcode-svg-6.1.2.tgz",