Skip to content

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

add support for pinning notifications

ammarahm-ed5e6317e9cead
diff --git a/apps/mobile/android/app/src/main/AndroidManifest.xml b/apps/mobile/android/app/src/main/AndroidManifest.xml
index 9712389af..39169f9ea 100644
--- a/apps/mobile/android/app/src/main/AndroidManifest.xml
+++ b/apps/mobile/android/app/src/main/AndroidManifest.xml
@@ -11,6 +11,8 @@
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
+ <uses-permission android:name="android.permission.VIBRATE" />
+ <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:name=".MainApplication"
@@ -88,6 +90,32 @@
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_viewer_provider_paths" />
</provider>
+
+ <!-- Change the value to true to enable pop-up for in foreground on receiving remote notifications (for prevent duplicating while showing local notifications set this to false) -->
+ <meta-data android:name="com.dieam.reactnativepushnotification.notification_foreground"
+ android:value="false"/>
+ <!-- Change the resource name to your App's accent color - or any other color you want -->
+ <meta-data android:name="com.dieam.reactnativepushnotification.notification_color"
+ android:resource="@android:color/white"/> <!-- or @android:color/{name} to use a standard color -->
+
+ <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
+ <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
+ <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
+ <intent-filter>
+ <action android:name="android.intent.action.BOOT_COMPLETED" />
+ <action android:name="android.intent.action.QUICKBOOT_POWERON" />
+ <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
+ </intent-filter>
+ </receiver>
+
+ <service
+ android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
+ android:exported="false" >
+ <intent-filter>
+ <action android:name="com.google.firebase.MESSAGING_EVENT" />
+ </intent-filter>
+ </service>
+
</application>
</manifest>
\ No newline at end of file
diff --git a/apps/mobile/index.js b/apps/mobile/index.js
index 728c7fd8c..130c2eb5d 100644
--- a/apps/mobile/index.js
+++ b/apps/mobile/index.js
@@ -3,12 +3,14 @@ import React from 'react';
import {AppRegistry} from 'react-native';
import {name as appName} from './app.json';
import {enableScreens} from 'react-native-screens';
+import Notifications from './src/services/Notifications';
global.Buffer = require('buffer').Buffer;
enableScreens(true);
+
let Provider;
let App;
let NotesnookShare;
-
+Notifications.init()
const AppProvider = () => {
Provider = require('./src/provider').Provider;
App = require('./App').default;
diff --git a/apps/mobile/src/components/ActionSheetComponent/index.js b/apps/mobile/src/components/ActionSheetComponent/index.js
index 1b01a896d..67efff4ab 100644
--- a/apps/mobile/src/components/ActionSheetComponent/index.js
+++ b/apps/mobile/src/components/ActionSheetComponent/index.js
@@ -20,8 +20,15 @@ import {
useUserStore,
} from '../../provider/stores';
import {DDS} from '../../services/DeviceDetection';
-import {eSendEvent, openVault, ToastEvent} from '../../services/EventManager';
+import {
+ eSendEvent,
+ eSubscribeEvent,
+ eUnSubscribeEvent,
+ openVault,
+ ToastEvent,
+} from '../../services/EventManager';
import Navigation from '../../services/Navigation';
+import Notifications from '../../services/Notifications';
import Sync from '../../services/Sync';
import {editing, toTXT} from '../../utils';
import {
@@ -69,6 +76,7 @@ export const ActionSheetComponent = ({
const [note, setNote] = useState(item);
const user = useUserStore(state => state.user);
const lastSynced = useUserStore(state => state.lastSynced);
+ const [notifPinned, setNotifPinned] = useState(null);
const refreshing = false;
const isPublished = db.monographs.isPublished(note.id);
@@ -81,15 +89,43 @@ export const ActionSheetComponent = ({
useEffect(() => {
if (item.id === null) return;
-
- sleep(1000).then(() => {
- setNote({...item});
- if (item.type !== note) {
- setIsPinnedToMenu(db.settings.isPinned(note.id));
- }
- });
+ checkNotifPinned();
+ setNote({...item});
+ if (item.type !== 'note') {
+ setIsPinnedToMenu(db.settings.isPinned(note.id));
+ }
}, [item]);
+ function checkNotifPinned() {
+ let pinned = Notifications.getPinnedNotes();
+ console.log(pinned);
+ if (!pinned) {
+ setNotifPinned(null);
+ return;
+ }
+ let index = pinned.findIndex(notif => notif.tag === item.id);
+ if (index !== -1) {
+ setNotifPinned(pinned[index]);
+ } else {
+ setNotifPinned(null);
+ }
+ }
+
+ const onUpdate = type => {
+ console.log('update', type);
+ if (type === 'unpin') {
+ checkNotifPinned();
+ }
+ };
+
+ useEffect(() => {
+ eSubscribeEvent('onUpdate', onUpdate);
+
+ return () => {
+ eUnSubscribeEvent('onUpdate', onUpdate);
+ };
+ }, []);
+
function changeColorScheme(colors = COLOR_SCHEME, accent = ACCENT) {
let newColors = setColorScheme(colors, accent);
dispatch({type: Actions.THEME, colors: newColors});
@@ -225,6 +261,37 @@ export const ActionSheetComponent = ({
id: notesnook.ids.dialogs.actionsheet.favorite,
color: 'orange',
},
+ {
+ name: 'PinToNotif',
+ title:
+ notifPinned !== null
+ ? 'Unpin from Notifications'
+ : 'Pin to Notifications',
+ icon: 'bell',
+ on: notifPinned !== null,
+ func: async () => {
+ if (notifPinned !== null) {
+ Notifications.remove(note.id, notifPinned.identifier);
+ await Notifications.get();
+ checkNotifPinned();
+ return;
+ }
+ if (note.locked) return;
+ let text = await db.notes.note(note.id).content();
+ text = toTXT(text);
+ Notifications.present({
+ title: note.title,
+ message: note.headline,
+ subtitle: note.headline,
+ bigText: text,
+ ongoing: true,
+ actions: ['UNPIN'],
+ tag: note.id,
+ });
+ await Notifications.get();
+ checkNotifPinned();
+ },
+ },
{
name: 'Edit Notebook',
@@ -271,7 +338,7 @@ export const ActionSheetComponent = ({
},
{
name: 'Restore',
- title:"Restore " + note.itemType,
+ title: 'Restore ' + note.itemType,
icon: 'delete-restore',
func: async () => {
close();
@@ -416,11 +483,11 @@ export const ActionSheetComponent = ({
title: 'Edit tag',
paragraph: 'Change the title of the tag',
positivePress: async value => {
- await db.tags.rename(note.id,value);
+ await db.tags.rename(note.id, value);
Navigation.setRoutesToUpdate([
Navigation.routeNames.Notes,
Navigation.routeNames.NotesPage,
- Navigation.routeNames.Tags
+ Navigation.routeNames.Tags,
]);
},
input: true,
@@ -504,7 +571,7 @@ export const ActionSheetComponent = ({
Navigation.setRoutesToUpdate([
Navigation.routeNames.Notes,
Navigation.routeNames.NotesPage,
- Navigation.routeNames.Tags
+ Navigation.routeNames.Tags,
]);
},
positiveText: 'Delete',
@@ -532,7 +599,7 @@ export const ActionSheetComponent = ({
},
{
name: 'PermDelete',
- title:"Delete " + note.itemType,
+ title: 'Delete ' + note.itemType,
icon: 'delete',
func: async () => {
close();
@@ -595,7 +662,9 @@ export const ActionSheetComponent = ({
/>
</PressableButton>
- <Paragraph style={{textAlign: 'center'}}>{rowItem.title}</Paragraph>
+ <Paragraph size={SIZE.sm - 1} style={{textAlign: 'center'}}>
+ {rowItem.title}
+ </Paragraph>
</View>
);
diff --git a/apps/mobile/src/components/NoteItem/index.js b/apps/mobile/src/components/NoteItem/index.js
index e8c2a269e..6c076c51b 100644
--- a/apps/mobile/src/components/NoteItem/index.js
+++ b/apps/mobile/src/components/NoteItem/index.js
@@ -65,11 +65,11 @@ const showActionSheet = (item, isTrash) => {
'Publish',
'Pin',
'Favorite',
+ 'PinToNotif',
'Vault',
'Delete',
'RemoveTopic',
],
- isTrash ? [] : ['Pin', 'Favorite', 'Vault', 'Delete', 'RemoveTopic'],
);
};
@@ -202,7 +202,7 @@ const NoteItem = ({item, isTrash, tags}) => {
) : null}
{item.pinned ? (
- <Icon1
+ <Icon
style={{marginRight: 10}}
name="pin"
size={SIZE.sm}
diff --git a/apps/mobile/src/services/Notifications.ts b/apps/mobile/src/services/Notifications.ts
new file mode 100644
index 000000000..586906721
--- /dev/null
+++ b/apps/mobile/src/services/Notifications.ts
@@ -0,0 +1,132 @@
+import { useNoteStore } from './../provider/stores';
+import PushNotificationIOS from '@react-native-community/push-notification-ios';
+import { Platform } from 'react-native';
+import PushNotification, { Importance, PushNotificationDeliveredObject } from 'react-native-push-notification';
+import { useSettingStore } from '../provider/stores';
+import { eSendEvent, eSubscribeEvent, eUnSubscribeEvent } from './EventManager';
+import { db } from '../utils/DB';
+import { DDS } from './DeviceDetection';
+import { tabBarRef } from '../utils/Refs';
+import { eOnLoadNote } from '../utils/Events';
+import { editing } from '../utils';
+import { MMKV } from '../utils/mmkv';
+
+
+const NOTIFICATION_TAG = 'notesnook';
+const CHANNEL_ID = 'com.streetwriters.notesnook';
+
+function loadNote(id: string, jump: boolean) {
+ let 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 init() {
+ PushNotification.configure({
+ onNotification: function (notification) {
+ editing.movedAway = false;
+ MMKV.removeItem('appState');
+ if (useNoteStore?.getState()?.loading === false) {
+ //@ts-ignore
+ loadNote(notification.tag,false)
+ return;
+ }
+
+ let unsub = useNoteStore.subscribe((loading) => {
+ if (loading === false) {
+ //@ts-ignore
+ loadNote(notification.tag,true);
+ //@ts-ignore
+ }
+ unsub();
+ }, state => state.loading);
+
+ },
+ onAction: function (notification) {
+ console.log("ACTION: ", notification.id);
+ if (notification.action === 'UNPIN') {
+ //@ts-ignore
+ remove(notification.tag, notification.id);
+ }
+ },
+ popInitialNotification: true,
+ requestPermissions: Platform.OS === 'ios',
+ });
+
+ PushNotification.createChannel(
+ {
+ channelId: CHANNEL_ID, // (required)
+ channelName: 'Notesnook', // (required)
+ playSound: false, // (optional) default: true
+ soundName: 'default', // (optional) See `soundName` parameter of `localNotification` function
+ importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
+ vibrate: true, // (optional) default: true. Creates the default vibration pattern if true.
+ },
+ created => console.log(`createChannel returned '${created}'`), // (optional) callback returns whether the channel was created, false means it already existed.
+ );
+}
+
+function remove(tag: string, id: string) {
+ console.log(tag, id);
+ PushNotification.clearLocalNotification(tag || NOTIFICATION_TAG, parseInt(id));
+ get().then(() => {
+ eSendEvent("onUpdate", "unpin");
+ });
+}
+
+function present({ title, message, subtitle, bigText, actions = [], ongoing, tag }: { title: string, message: string, subtitle: string, bigText: string, actions: Array<string>, ongoing: boolean, tag: string }) {
+
+ PushNotification.localNotification({
+ 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"
+ });
+}
+
+function clearAll() {
+ PushNotification.cancelAllLocalNotifications();
+ PushNotification.clearAllNotifications();
+}
+
+let pinned = [];
+
+
+function getPinnedNotes(): PushNotificationDeliveredObject[] {
+ return pinned;
+}
+
+function get(): Promise<PushNotificationDeliveredObject[]> {
+ return new Promise(resolve => {
+ PushNotification.getDeliveredNotifications(n => {
+ pinned = n;
+ resolve(n);
+ });
+ });
+}
+
+export default {
+ init,
+ present,
+ clearAll,
+ remove,
+ get,
+ getPinnedNotes
+};