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_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
|
||||
*/
|
||||
@@ -196,7 +205,7 @@ export const setupNotificationTapListener = async (onTap: (path: string) => void
|
||||
|
||||
if (isCapacitorNative()) {
|
||||
try {
|
||||
const { LocalNotifications } = await import('@capacitor/local-notifications');
|
||||
const { LocalNotifications } = await importCapacitorLocalNotifications();
|
||||
await LocalNotifications.addListener('localNotificationActionPerformed', async (event: any) => {
|
||||
await focusWindow();
|
||||
const path = event?.notification?.extra?.path;
|
||||
@@ -336,7 +345,7 @@ const ensureCapacitorNotificationChannel = async (): Promise<void> => {
|
||||
if (notificationChannelCreated || !isAndroid()) return;
|
||||
|
||||
try {
|
||||
const { LocalNotifications } = await import('@capacitor/local-notifications');
|
||||
const { LocalNotifications } = await importCapacitorLocalNotifications();
|
||||
await LocalNotifications.createChannel({
|
||||
id: 'messages',
|
||||
name: 'Messages',
|
||||
@@ -365,7 +374,7 @@ export const requestSystemNotificationPermission = async (): Promise<boolean> =>
|
||||
|
||||
if (isCapacitorNative()) {
|
||||
try {
|
||||
const { LocalNotifications } = await import('@capacitor/local-notifications');
|
||||
const { LocalNotifications } = await importCapacitorLocalNotifications();
|
||||
let perm = await LocalNotifications.checkPermissions();
|
||||
if (perm.display !== 'granted') {
|
||||
perm = await LocalNotifications.requestPermissions();
|
||||
@@ -385,7 +394,7 @@ export const requestSystemNotificationPermission = async (): Promise<boolean> =>
|
||||
export const getSystemNotificationPermissionState = async (): Promise<PermissionState> => {
|
||||
if (isCapacitorNative()) {
|
||||
try {
|
||||
const { LocalNotifications } = await import('@capacitor/local-notifications');
|
||||
const { LocalNotifications } = await importCapacitorLocalNotifications();
|
||||
const perm = await LocalNotifications.checkPermissions();
|
||||
return perm.display === 'granted' ? 'granted' : 'prompt';
|
||||
} catch (err) {
|
||||
@@ -471,7 +480,7 @@ export const sendNotification = async (options: {
|
||||
|
||||
if (isCapacitorNative()) {
|
||||
try {
|
||||
const { LocalNotifications } = await import('@capacitor/local-notifications');
|
||||
const { LocalNotifications } = await importCapacitorLocalNotifications();
|
||||
let perm = await LocalNotifications.checkPermissions();
|
||||
if (perm.display !== 'granted') {
|
||||
perm = await LocalNotifications.requestPermissions();
|
||||
|
||||
Reference in New Issue
Block a user