native-update
Version:
Foundation package for building a comprehensive update system for Capacitor apps. Provides architecture and interfaces but requires backend implementation.
100 lines • 3.86 kB
JavaScript
import { Logger } from '../core/logger';
export class AppUpdateNotifier {
constructor(config) {
this.config = config;
this.logger = new Logger('AppUpdateNotifier');
}
async notifyUpdateAvailable(updateInfo) {
this.logger.log('Notifying update available', updateInfo);
// Emit event for update availability
this.emitUpdateEvent('appUpdateAvailable', {
currentVersion: updateInfo.currentVersion,
availableVersion: updateInfo.availableVersion,
updatePriority: updateInfo.updatePriority,
updateSize: updateInfo.updateSize,
releaseNotes: updateInfo.releaseNotes,
storeUrl: updateInfo.updateURL,
});
// Show notification if configured
if (this.config.showUpdateNotification !== false) {
await this.showNotification(updateInfo);
}
}
async notifyUpdateDownloading(progress) {
this.logger.log('Update download progress', progress);
// Emit progress event
this.emitUpdateEvent('appUpdateProgress', {
percentComplete: progress,
});
}
async notifyUpdateReady() {
this.logger.log('Update ready for installation');
// Emit ready event
this.emitUpdateEvent('appUpdateReady', {
message: 'Update downloaded and ready to install',
});
// Show notification
if (this.config.showUpdateNotification !== false) {
await this.showReadyNotification();
}
}
async notifyUpdateFailed(error) {
this.logger.error('Update failed', error);
// Emit error event
this.emitUpdateEvent('appUpdateFailed', {
error: error.message,
code: error.code || 'UNKNOWN_ERROR',
});
}
async showNotification(updateInfo) {
if (!('Notification' in window)) {
return;
}
// Request permission if needed
if (Notification.permission === 'default') {
await Notification.requestPermission();
}
if (Notification.permission === 'granted') {
const notification = new Notification('App Update Available', {
body: `Version ${updateInfo.availableVersion} is available. ${updateInfo.releaseNotes || ''}`,
icon: '/icon.png',
tag: 'app-update',
requireInteraction: updateInfo.updatePriority === 5, // Priority 5 = IMMEDIATE
});
notification.onclick = () => {
// Trigger update action
this.emitUpdateEvent('appUpdateNotificationClicked', {});
notification.close();
};
}
}
async showReadyNotification() {
if (!('Notification' in window) || Notification.permission !== 'granted') {
return;
}
const notificationOptions = {
body: 'App update has been downloaded and is ready to install.',
icon: '/icon.png',
tag: 'app-update-ready',
};
// Actions are not supported in standard NotificationOptions
notificationOptions.actions = [
{ action: 'install', title: 'Install Now' },
{ action: 'later', title: 'Later' },
];
const notification = new Notification('Update Ready', notificationOptions);
notification.onclick = (event) => {
if (event.action === 'install') {
this.emitUpdateEvent('appUpdateInstallClicked', {});
}
notification.close();
};
}
emitUpdateEvent(eventName, data) {
// Emit custom event
if (typeof window !== 'undefined') {
window.dispatchEvent(new CustomEvent(eventName, { detail: data }));
}
}
}
//# sourceMappingURL=app-update-notifier.js.map