From 499429858c6c4926dbeb3511bed45744ea98fd74 Mon Sep 17 00:00:00 2001 From: Chongyi Zheng Date: Thu, 21 Sep 2023 09:08:04 -0400 Subject: [PATCH] Use API v2 for Bark notification (#2759) * Use API v2 for Bark notification * API v2 endpoint should end with path `/push` * Support both v1 and v2 * Flip the bool * Allow selecting api version * Apply review suggestion Co-authored-by: Nelson Chan <3271800+chakflying@users.noreply.github.com> * Add translated string to `en.json` * Apply review suggestion Co-authored-by: Nelson Chan <3271800+chakflying@users.noreply.github.com> --------- Co-authored-by: Nelson Chan <3271800+chakflying@users.noreply.github.com> --- server/notification-providers/bark.js | 26 ++++++++++++++++++-------- src/components/notifications/Bark.vue | 7 +++++++ src/lang/en.json | 1 + 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/server/notification-providers/bark.js b/server/notification-providers/bark.js index 8746761e4..7fe9a7a9f 100644 --- a/server/notification-providers/bark.js +++ b/server/notification-providers/bark.js @@ -46,8 +46,7 @@ class Bark extends NotificationProvider { } /** - * Add additional parameter for better on device styles (iOS 15 - * optimized) + * Add additional parameter for Bark v1 endpoints * @param {BeanModel} notification Notification to send * @param {string} postUrl URL to append parameters to * @returns {string} Additional URL parameters @@ -96,12 +95,23 @@ class Bark extends NotificationProvider { * @returns {string} Success message */ async postNotification(notification, title, subtitle, endpoint) { - // url encode title and subtitle - title = encodeURIComponent(title); - subtitle = encodeURIComponent(subtitle); - let postUrl = endpoint + "/" + title + "/" + subtitle; - postUrl = this.appendAdditionalParameters(notification, postUrl); - let result = await axios.get(postUrl); + let result; + if (notification.apiVersion === "v1" || notification.apiVersion == null) { + // url encode title and subtitle + title = encodeURIComponent(title); + subtitle = encodeURIComponent(subtitle); + let postUrl = endpoint + "/" + title + "/" + subtitle; + postUrl = this.appendAdditionalParameters(notification, postUrl); + result = await axios.get(postUrl); + } else { + result = await axios.post(`${endpoint}/push`, { + title, + body: subtitle, + icon: barkNotificationAvatar, + sound: notification.barkSound || "telegraph", // default sound is telegraph + group: notification.barkGroup || "UptimeKuma", // default group is UptimeKuma + }); + } this.checkResult(result); if (result.statusText != null) { return "Bark notification succeed: " + result.statusText; diff --git a/src/components/notifications/Bark.vue b/src/components/notifications/Bark.vue index 6cac73d36..b38a7dc62 100644 --- a/src/components/notifications/Bark.vue +++ b/src/components/notifications/Bark.vue @@ -1,4 +1,11 @@