native-update
Version:
Foundation package for building a comprehensive update system for Capacitor apps. Provides architecture and interfaces but requires backend implementation.
89 lines • 3.6 kB
JavaScript
import { NotificationPriority } from '../definitions';
export class NotificationManager {
constructor() {
this.preferences = {
title: 'App Update Available',
description: 'A new version of the app is available',
soundEnabled: true,
vibrationEnabled: true,
showActions: true,
actionLabels: {
updateNow: 'Update Now',
updateLater: 'Later',
dismiss: 'Dismiss',
},
priority: NotificationPriority.DEFAULT,
};
}
setPreferences(preferences) {
this.preferences = Object.assign(Object.assign({}, this.preferences), preferences);
}
async sendUpdateNotification(appUpdate, liveUpdate) {
const permissions = await this.getPermissionStatus();
if (!permissions.granted) {
return false;
}
const notificationData = this.createNotificationData(appUpdate, liveUpdate);
return this.showNotification(notificationData);
}
createNotificationData(appUpdate, liveUpdate) {
var _a, _b, _c;
let title = this.preferences.title || 'App Update Available';
let body = this.preferences.description || 'A new version of the app is available';
if ((appUpdate === null || appUpdate === void 0 ? void 0 : appUpdate.updateAvailable) && (liveUpdate === null || liveUpdate === void 0 ? void 0 : liveUpdate.available)) {
title = 'App Updates Available';
body = `App version ${appUpdate.availableVersion} and content updates are available`;
}
else if (appUpdate === null || appUpdate === void 0 ? void 0 : appUpdate.updateAvailable) {
title = 'App Update Available';
body = `Version ${appUpdate.availableVersion} is available`;
}
else if (liveUpdate === null || liveUpdate === void 0 ? void 0 : liveUpdate.available) {
title = 'Content Update Available';
body = `New content version ${liveUpdate.version} is available`;
}
const actions = [];
if (this.preferences.showActions) {
actions.push({
id: 'update_now',
title: ((_a = this.preferences.actionLabels) === null || _a === void 0 ? void 0 : _a.updateNow) || 'Update Now',
});
actions.push({
id: 'update_later',
title: ((_b = this.preferences.actionLabels) === null || _b === void 0 ? void 0 : _b.updateLater) || 'Later',
});
actions.push({
id: 'dismiss',
title: ((_c = this.preferences.actionLabels) === null || _c === void 0 ? void 0 : _c.dismiss) || 'Dismiss',
});
}
return {
title,
body,
data: {
type: 'update_available',
appUpdate,
liveUpdate,
},
actions,
};
}
getChannelId() {
return this.preferences.channelId || 'capacitor_native_update';
}
getChannelName() {
return this.preferences.channelName || 'App Updates';
}
getPriority() {
return this.preferences.priority || NotificationPriority.DEFAULT;
}
shouldPlaySound() {
var _a;
return (_a = this.preferences.soundEnabled) !== null && _a !== void 0 ? _a : true;
}
shouldVibrate() {
var _a;
return (_a = this.preferences.vibrationEnabled) !== null && _a !== void 0 ? _a : true;
}
}
//# sourceMappingURL=notification-manager.js.map