From 3ffea83eaf399a6cab0db460f9777fea4e1631bd Mon Sep 17 00:00:00 2001 From: onurpolattimur Date: Tue, 26 May 2020 11:59:06 +0300 Subject: [PATCH] feat: add custom download completed notification (#4952) * feat: add custom download completed notification Added optional parameter for download notification in checkForUpdatesAndNotify - if the parameter not given, uses default notification message. - if {appName} occurs in notification body or title, formatNotification changes it to application name. - if {version} occurs in notification body or title, formatNotification changes it to application version. * fix typing in DownloadNotification interface. --- packages/electron-updater/src/AppUpdater.ts | 27 +++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/electron-updater/src/AppUpdater.ts b/packages/electron-updater/src/AppUpdater.ts index a4881622cc3..e682d9bdb6d 100644 --- a/packages/electron-updater/src/AppUpdater.ts +++ b/packages/electron-updater/src/AppUpdater.ts @@ -244,7 +244,7 @@ export abstract class AppUpdater extends EventEmitter { } // noinspection JSUnusedGlobalSymbols - checkForUpdatesAndNotify(): Promise { + checkForUpdatesAndNotify(downloadNotification?: DownloadNotification): Promise { if (!this.isUpdaterActive()) { return Promise.resolve(null) } @@ -262,16 +262,28 @@ export abstract class AppUpdater extends EventEmitter { downloadPromise .then(() => { - new Notification({ - title: "A new update is ready to install", - body: `${this.app.name} version ${it.updateInfo.version} has been downloaded and will be automatically installed on exit` - }).show() + const notificationContent = this.formatDownloadNotification(it.updateInfo.version, this.app.name, downloadNotification); + new Notification(notificationContent).show() }) return it }) } + private formatDownloadNotification(version: string, appName: string, downloadNotification?: DownloadNotification): DownloadNotification { + if (downloadNotification == null) { + downloadNotification = { + title: "A new update is ready to install", + body: `{appName} version {version} has been downloaded and will be automatically installed on exit` + } + } + downloadNotification = { + title: downloadNotification.title.replace("{appName}", appName).replace("{version}", version), + body: downloadNotification.body.replace("{appName}", appName).replace("{version}", version) + } + return downloadNotification; + } + private async isStagingMatch(updateInfo: UpdateInfo): Promise { const rawStagingPercentage = updateInfo.stagingPercentage let stagingPercentage = rawStagingPercentage @@ -665,6 +677,11 @@ export interface DownloadExecutorTask { readonly done?: (event: UpdateDownloadedEvent) => Promise } +export interface DownloadNotification { + body: string + title: string +} + /** @private */ export interface TestOnlyUpdaterOptions { platform: ProviderPlatform