From 69136f7798d926a3b180585b66bd83c41802c478 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Sat, 25 Feb 2023 22:06:16 +0100 Subject: [PATCH] PhsNotifications: Add support for updating already delivered notifications --- .../integrationpluginpushnotifications.cpp | 32 ++++++++++++++----- .../integrationpluginpushnotifications.json | 21 ++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/pushnotifications/integrationpluginpushnotifications.cpp b/pushnotifications/integrationpluginpushnotifications.cpp index 1138e9e2..af9c8b96 100644 --- a/pushnotifications/integrationpluginpushnotifications.cpp +++ b/pushnotifications/integrationpluginpushnotifications.cpp @@ -122,11 +122,19 @@ void IntegrationPluginPushNotifications::executeAction(ThingActionInfo *info) QString title = action.param(pushNotificationsNotifyActionTitleParamTypeId).value().toString(); QString body = action.param(pushNotificationsNotifyActionBodyParamTypeId).value().toString(); QString data = action.paramValue(pushNotificationsNotifyActionDataParamTypeId).toString(); + QString notificationId = action.paramValue(pushNotificationsNotifyActionNotificationIdParamTypeId).toString(); + bool remove = action.paramValue(pushNotificationsNotifyActionRemoveParamTypeId).toBool(); + bool sound = action.paramValue(pushNotificationsNotifyActionSoundParamTypeId).toBool(); if (pushService != "None" && token.isEmpty()) { return info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Push notifications need to be reconfigured.")); } + if (notificationId.isEmpty()) { + notificationId = QUuid::createUuid().toString(); + } + + QVariantMap nymeaData; // FIXME: This is quite ugly but there isn't an API that allows to retrieve the server UUID yet NymeaSettings settings(NymeaSettings::SettingsRoleGlobal); @@ -157,31 +165,39 @@ void IntegrationPluginPushNotifications::executeAction(ThingActionInfo *info) notification.insert("title", title); notification.insert("body", body); notification.insert("nymeaData", nymeaData); + notification.insert("notificationId", notificationId); + notification.insert("remove", remove); if (pushService == "FB-GCM") { - QVariantMap soundMap; - soundMap.insert("sound", "default"); + notification.insert("sound", sound); QVariantMap android; android.insert("priority", "high"); - android.insert("notification", soundMap); payload.insert("android", android); payload.insert("data", notification); } else if (pushService == "FB-APNs") { - notification.insert("sound", "default"); + if (sound) { + notification.insert("sound", "default"); + } QVariantMap headers; - headers.insert("apns-priority", "10"); + headers.insert("apns-priority", sound ? "10" : "1"); + headers.insert("apns-collapse-id", notificationId); + QVariantMap apns; apns.insert("headers", headers); + notification.insert("tag", notificationId); + payload.insert("notification", notification); payload.insert("apns", apns); + + payload.insert("collapse_key", notificationId); } @@ -198,8 +214,8 @@ void IntegrationPluginPushNotifications::executeAction(ThingActionInfo *info) QVariantMap notification; notification.insert("card", card); - notification.insert("vibrate", true); - notification.insert("sound", true); + notification.insert("vibrate", sound); + notification.insert("sound", sound); notification.insert("nymeaData", nymeaData); QVariantMap data; @@ -237,7 +253,7 @@ void IntegrationPluginPushNotifications::executeAction(ThingActionInfo *info) } QVariantMap replyMap = jsonDoc.toVariant().toMap(); -// qDebug(dcPushNotifications) << qUtf8Printable(jsonDoc.toJson()); + // qDebug(dcPushNotifications) << qUtf8Printable(jsonDoc.toJson()); if (pushService == "FB-GCM" || pushService == "FB-APNs") { if (replyMap.value("success").toInt() != 1) { diff --git a/pushnotifications/integrationpluginpushnotifications.json b/pushnotifications/integrationpluginpushnotifications.json index f0c77770..c33750da 100644 --- a/pushnotifications/integrationpluginpushnotifications.json +++ b/pushnotifications/integrationpluginpushnotifications.json @@ -58,6 +58,27 @@ "inputType": "TextArea", "defaultValue": "" }, + { + "id": "a961c428-c824-410a-b4e6-527a13c4c4ed", + "name": "notificationId", + "displayName": "Notification ID", + "type": "QString", + "defaultValue": "" + }, + { + "id": "0f3c3661-e0c0-46df-a6da-aaf3a0e8157c", + "name": "sound", + "displayName": "Play sound", + "type": "bool", + "defaultValue": true + }, + { + "id": "92ffd4ff-4bb7-4261-a904-1ae3890704e0", + "name": "remove", + "displayName": "Remove", + "type": "bool", + "defaultValue": false + }, { "id": "ca36a33f-a7f2-4f59-8767-3522ecaaf8fb", "name": "data",