Skip to content

Commit d62b44b

Browse files
committed
Implement notification dismissal handling
1 parent 00be844 commit d62b44b

File tree

6 files changed

+70
-8
lines changed

6 files changed

+70
-8
lines changed

src/MainWindow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ void MainWindow::setupUI()
6262
// Connect signals
6363
connect(m_notificationManager, &NotificationManager::notificationReceived,
6464
m_notificationPanel, &NotificationPanel::addNotification);
65+
connect(m_notificationManager, &NotificationManager::notificationRemoved,
66+
m_notificationPanel, &NotificationPanel::removeNotification);
6567

6668
// Connect popup manager to show popups for new notifications
6769
connect(m_notificationManager, &NotificationManager::notificationReceived,

src/NotificationClient.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ void NotificationClient::sendConnectionRequest()
302302

303303
connMsg["payload"] = payload;
304304

305+
Logger::debug("Sending connection request to server: " + QJsonDocument(connMsg).toJson(QJsonDocument::Compact));
305306
sendMessage(connMsg);
306307
}
307308

@@ -358,22 +359,42 @@ void NotificationClient::handleMessage(const QJsonObject& message)
358359
}
359360
}
360361
}
362+
else if (msgType == "notification_action") {
363+
if (m_handshakeComplete) {
364+
handleNotificationAction(message);
365+
}
366+
}
361367
else if (msgType == "ping") {
362368
Logger::debug(QString("Received ping with ID: %1").arg(message.value("id").toString()));
363369
handlePing(message);
364370
}
365371
else {
366-
Logger::warning(QString("Unknown message type: %1").arg(msgType));
372+
Logger::warning(QString("Unknown message type: %1, message: %2")
373+
.arg(msgType, QJsonDocument(message).toJson(QJsonDocument::Compact)));
367374
}
368375
}
369376

370377
void NotificationClient::handlePing(const QJsonObject& message)
371378
{
372379
QString pingId = message.value("id").toString();
373-
Logger::debug(QString("Sending pong response for ping ID: %1").arg(pingId));
374380
sendPong(pingId);
375381
}
376382

383+
void NotificationClient::handleNotificationAction(const QJsonObject& message)
384+
{
385+
QJsonObject payload = message.value("payload").toObject();
386+
QString notificationId = payload.value("id").toString();
387+
QString actionType = payload.value("type").toString();
388+
389+
if (actionType == "notification_dismiss") {
390+
Logger::debug(QString("Received dismiss action for notification: %1").arg(notificationId));
391+
emit notificationDismissed(notificationId);
392+
} else {
393+
// Handle other action types if needed in the future
394+
Logger::debug(QString("Received notification action type '%1' for notification: %2").arg(actionType, notificationId));
395+
}
396+
}
397+
377398
void NotificationClient::sendPong(const QString& pingId)
378399
{
379400
QJsonObject pongMsg;
@@ -385,6 +406,7 @@ void NotificationClient::sendPong(const QString& pingId)
385406
payload["device"] = "Relay-PC";
386407
pongMsg["payload"] = payload;
387408

409+
Logger::debug("Sending pong to server: " + QJsonDocument(pongMsg).toJson(QJsonDocument::Compact));
388410
sendMessage(pongMsg);
389411
}
390412

@@ -404,6 +426,7 @@ void NotificationClient::sendNotificationReply(const QString& notificationId, co
404426
payload["body"] = replyText;
405427
actionMsg["payload"] = payload;
406428

429+
Logger::debug("Sending notification reply to server: " + QJsonDocument(actionMsg).toJson(QJsonDocument::Compact));
407430
sendMessage(actionMsg);
408431
}
409432

@@ -422,23 +445,26 @@ void NotificationClient::sendNotificationAction(const QString& notificationId, c
422445
payload["type"] = "action";
423446
actionMsg["payload"] = payload;
424447

448+
Logger::debug("Sending notification action to server: " + QJsonDocument(actionMsg).toJson(QJsonDocument::Compact));
425449
sendMessage(actionMsg);
426450
}
427451

428452
void NotificationClient::sendNotificationDismiss(const QString& notificationId)
429453
{
430454
if (!m_handshakeComplete) return;
431455

432-
QJsonObject dismissMsg;
433-
dismissMsg["type"] = "notification_dismiss";
434-
dismissMsg["id"] = QUuid::createUuid().toString(QUuid::WithoutBraces);
435-
dismissMsg["timestamp"] = QDateTime::currentSecsSinceEpoch();
456+
QJsonObject actionMsg;
457+
actionMsg["type"] = "notification_action";
458+
actionMsg["id"] = QUuid::createUuid().toString(QUuid::WithoutBraces);
459+
actionMsg["timestamp"] = QDateTime::currentSecsSinceEpoch();
436460

437461
QJsonObject payload;
438462
payload["id"] = notificationId;
439-
dismissMsg["payload"] = payload;
463+
payload["type"] = "notification_dismiss";
464+
actionMsg["payload"] = payload;
440465

441-
sendMessage(dismissMsg);
466+
Logger::debug("Sending notification dismiss to server: " + QJsonDocument(actionMsg).toJson(QJsonDocument::Compact));
467+
sendMessage(actionMsg);
442468
}
443469

444470
void NotificationClient::onReconnectTimer()

src/NotificationClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class NotificationClient : public QObject
3838
void connected();
3939
void disconnected();
4040
void notificationReceived(const NotificationData& notification);
41+
void notificationDismissed(const QString& notificationId); // New signal for incoming dismisses
4142
void errorOccurred(const QString& error);
4243
void serverDiscovered(const QHostAddress& address, quint16 port);
4344

@@ -57,6 +58,7 @@ private slots:
5758
void sendMessage(const QJsonObject& message);
5859
void handleMessage(const QJsonObject& message);
5960
void handlePing(const QJsonObject& message);
61+
void handleNotificationAction(const QJsonObject& message);
6062
void sendPong(const QString& pingId);
6163
NotificationData parseNotificationJson(const QJsonObject& json);
6264
void startReconnectTimer();

src/NotificationManager.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "NotificationManager.h"
22
#include "NotificationClient.h"
3+
#include "src/Logger.h"
34

45
#include <QTimer>
56
#include <QDateTime>
@@ -20,6 +21,8 @@ NotificationManager::NotificationManager(QObject *parent)
2021
m_client = new NotificationClient(this);
2122
connect(m_client, &NotificationClient::notificationReceived,
2223
this, &NotificationManager::onClientNotificationReceived);
24+
connect(m_client, &NotificationClient::notificationDismissed,
25+
this, &NotificationManager::onClientNotificationDismissed);
2326
connect(m_client, &NotificationClient::connected,
2427
this, &NotificationManager::onClientConnected);
2528
connect(m_client, &NotificationClient::disconnected,
@@ -52,8 +55,15 @@ void NotificationManager::removeNotification(int notificationId)
5255
{
5356
for (int i = 0; i < m_notifications.size(); ++i) {
5457
if (m_notifications[i].id == notificationId) {
58+
QString stringId = m_notifications[i].stringId;
5559
m_notifications.removeAt(i);
5660
emit notificationRemoved(notificationId);
61+
62+
// Send dismiss message to Android if we have a string ID and client
63+
if (!stringId.isEmpty() && m_client && m_client->isConnected()) {
64+
m_client->sendNotificationDismiss(stringId);
65+
}
66+
5767
break;
5868
}
5969
}
@@ -165,6 +175,19 @@ void NotificationManager::onClientNotificationReceived(const NotificationData& n
165175
addNotification(notification);
166176
}
167177

178+
void NotificationManager::onClientNotificationDismissed(const QString& notificationId)
179+
{
180+
// Find and remove the notification with the matching string ID
181+
for (int i = 0; i < m_notifications.size(); ++i) {
182+
if (m_notifications[i].stringId == notificationId) {
183+
int intId = m_notifications[i].id;
184+
m_notifications.removeAt(i);
185+
emit notificationRemoved(intId);
186+
break;
187+
}
188+
}
189+
}
190+
168191
void NotificationManager::onClientConnected()
169192
{
170193
emit serverConnected();

src/NotificationManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class NotificationManager : public QObject
4141
private slots:
4242
void generateTestNotification();
4343
void onClientNotificationReceived(const NotificationData& notification);
44+
void onClientNotificationDismissed(const QString& notificationId);
4445
void onClientConnected();
4546
void onClientDisconnected();
4647
void onClientError(const QString& error);

src/NotificationPanel.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,24 @@ void NotificationPanel::addNotification(const NotificationData& notification)
274274

275275
void NotificationPanel::removeNotification(int notificationId)
276276
{
277+
// Find the notification card and get its string ID for dismiss
278+
QString stringId;
277279
for (int i = 0; i < m_notificationCards.size(); ++i) {
278280
NotificationCard* card = m_notificationCards[i];
279281
if (card->getNotificationId() == notificationId) {
282+
stringId = card->getNotificationData().stringId;
280283
m_scrollLayout->removeWidget(card);
281284
m_notificationCards.removeAt(i);
282285
card->deleteLater();
283286
break;
284287
}
285288
}
286289

290+
// Notify the notification manager to handle the dismiss (which will send to Android)
291+
if (m_notificationManager && !stringId.isEmpty()) {
292+
m_notificationManager->removeNotification(notificationId);
293+
}
294+
287295
updateEmptyState();
288296
}
289297

0 commit comments

Comments
 (0)