From 2c22cae07fce320d458eb092c27bd0dc0849c3cd Mon Sep 17 00:00:00 2001 From: adb Date: Fri, 9 Apr 2021 00:48:06 +0200 Subject: [PATCH] add showNativeNotification --- capacitor.config.json | 2 +- src/lib/NotificationHandler.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/capacitor.config.json b/capacitor.config.json index 8727388..3a4fc78 100644 --- a/capacitor.config.json +++ b/capacitor.config.json @@ -4,7 +4,7 @@ "bundledWebRuntime": false, "npmClient": "npm", "webDir": "./dist", - "linuxAndroidStudioPath": "~/.local/share/JetBrains/Toolbox/apps/AndroidStudio/ch-0/201.7199119/bin/studio.sh", + "linuxAndroidStudioPath": "/home/alban/.local/share/JetBrains/Toolbox/apps/AndroidStudio/ch-0/201.7199119/bin/studio.sh", "plugins": { "SplashScreen": { "launchShowDuration": 0 diff --git a/src/lib/NotificationHandler.js b/src/lib/NotificationHandler.js index 3d17dfe..474d168 100644 --- a/src/lib/NotificationHandler.js +++ b/src/lib/NotificationHandler.js @@ -2,6 +2,8 @@ import {getMxcFromUserId, getAvatarUrl} from '@/lib/getMxc'; import {calcUserName} from '@/lib/matrixUtils'; import {getRoom} from '@/lib/matrixUtils'; import {router} from '@/router'; +import {Capacitor, Plugins} from '@capacitor/core'; +const {LocalNotifications} = Plugins; export class NotificationHandler{ constructor() { @@ -17,6 +19,7 @@ export class NotificationHandler{ .then(permission => {return permission === 'granted'}); } showNotification(event){ + if (Capacitor.isNative) return this.showNativeNotification(event); if (Notification.permission !== 'granted') return false; console.log(event); let mxc = getMxcFromUserId(event.sender); @@ -25,4 +28,20 @@ export class NotificationHandler{ icon: mxc?getAvatarUrl(mxc):undefined }).onclick = ()=>router.push(`/rooms/${event.room_id}`); } + showNativeNotification(event){ + LocalNotifications.schedule({ + notifications: [ + { + title: `${calcUserName(event.sender)} in ${getRoom(event.room_id).name}`, + body: event.content.body, + id: 1, + schedule: { at: new Date(Date.now() + 1000 * 5) }, + sound: null, + attachments: null, + actionTypeId: '', + extra: null + } + ] + }); + } } \ No newline at end of file