feat: Lazy-load Capacitor local notifications to optimize runtime loading
This commit is contained in:
@@ -119,6 +119,15 @@ let notificationTapCallback: ((path: string) => void) | null = null;
|
|||||||
const ANDROID_NOTIFICATION_SMALL_ICON = 'ic_stat_paarrot';
|
const ANDROID_NOTIFICATION_SMALL_ICON = 'ic_stat_paarrot';
|
||||||
const ANDROID_NOTIFICATION_ICON_COLOR = '#FF8A00';
|
const ANDROID_NOTIFICATION_ICON_COLOR = '#FF8A00';
|
||||||
|
|
||||||
|
type CapacitorLocalNotificationsModule = typeof import('@capacitor/local-notifications');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lazy-load Capacitor local notifications at runtime only.
|
||||||
|
* Vite must not pre-resolve this module because some desktop builds do not include it.
|
||||||
|
*/
|
||||||
|
const importCapacitorLocalNotifications = async (): Promise<CapacitorLocalNotificationsModule> =>
|
||||||
|
import(/* @vite-ignore */ '@capacitor/local-notifications');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bring the Tauri window to the front and focus it
|
* Bring the Tauri window to the front and focus it
|
||||||
*/
|
*/
|
||||||
@@ -196,7 +205,7 @@ export const setupNotificationTapListener = async (onTap: (path: string) => void
|
|||||||
|
|
||||||
if (isCapacitorNative()) {
|
if (isCapacitorNative()) {
|
||||||
try {
|
try {
|
||||||
const { LocalNotifications } = await import('@capacitor/local-notifications');
|
const { LocalNotifications } = await importCapacitorLocalNotifications();
|
||||||
await LocalNotifications.addListener('localNotificationActionPerformed', async (event: any) => {
|
await LocalNotifications.addListener('localNotificationActionPerformed', async (event: any) => {
|
||||||
await focusWindow();
|
await focusWindow();
|
||||||
const path = event?.notification?.extra?.path;
|
const path = event?.notification?.extra?.path;
|
||||||
@@ -336,7 +345,7 @@ const ensureCapacitorNotificationChannel = async (): Promise<void> => {
|
|||||||
if (notificationChannelCreated || !isAndroid()) return;
|
if (notificationChannelCreated || !isAndroid()) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { LocalNotifications } = await import('@capacitor/local-notifications');
|
const { LocalNotifications } = await importCapacitorLocalNotifications();
|
||||||
await LocalNotifications.createChannel({
|
await LocalNotifications.createChannel({
|
||||||
id: 'messages',
|
id: 'messages',
|
||||||
name: 'Messages',
|
name: 'Messages',
|
||||||
@@ -365,7 +374,7 @@ export const requestSystemNotificationPermission = async (): Promise<boolean> =>
|
|||||||
|
|
||||||
if (isCapacitorNative()) {
|
if (isCapacitorNative()) {
|
||||||
try {
|
try {
|
||||||
const { LocalNotifications } = await import('@capacitor/local-notifications');
|
const { LocalNotifications } = await importCapacitorLocalNotifications();
|
||||||
let perm = await LocalNotifications.checkPermissions();
|
let perm = await LocalNotifications.checkPermissions();
|
||||||
if (perm.display !== 'granted') {
|
if (perm.display !== 'granted') {
|
||||||
perm = await LocalNotifications.requestPermissions();
|
perm = await LocalNotifications.requestPermissions();
|
||||||
@@ -385,7 +394,7 @@ export const requestSystemNotificationPermission = async (): Promise<boolean> =>
|
|||||||
export const getSystemNotificationPermissionState = async (): Promise<PermissionState> => {
|
export const getSystemNotificationPermissionState = async (): Promise<PermissionState> => {
|
||||||
if (isCapacitorNative()) {
|
if (isCapacitorNative()) {
|
||||||
try {
|
try {
|
||||||
const { LocalNotifications } = await import('@capacitor/local-notifications');
|
const { LocalNotifications } = await importCapacitorLocalNotifications();
|
||||||
const perm = await LocalNotifications.checkPermissions();
|
const perm = await LocalNotifications.checkPermissions();
|
||||||
return perm.display === 'granted' ? 'granted' : 'prompt';
|
return perm.display === 'granted' ? 'granted' : 'prompt';
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -471,7 +480,7 @@ export const sendNotification = async (options: {
|
|||||||
|
|
||||||
if (isCapacitorNative()) {
|
if (isCapacitorNative()) {
|
||||||
try {
|
try {
|
||||||
const { LocalNotifications } = await import('@capacitor/local-notifications');
|
const { LocalNotifications } = await importCapacitorLocalNotifications();
|
||||||
let perm = await LocalNotifications.checkPermissions();
|
let perm = await LocalNotifications.checkPermissions();
|
||||||
if (perm.display !== 'granted') {
|
if (perm.display !== 'granted') {
|
||||||
perm = await LocalNotifications.requestPermissions();
|
perm = await LocalNotifications.requestPermissions();
|
||||||
|
|||||||
Reference in New Issue
Block a user