diff --git a/Session/Notifications/AppNotifications.swift b/Session/Notifications/AppNotifications.swift index ce8f3ad91..32aee5555 100644 --- a/Session/Notifications/AppNotifications.swift +++ b/Session/Notifications/AppNotifications.swift @@ -95,6 +95,7 @@ protocol NotificationPresenterAdaptee: class { func notify(category: AppNotificationCategory, title: String?, body: String, userInfo: [AnyHashable: Any], sound: OWSSound?, replacingIdentifier: String?) func cancelNotifications(threadId: String) + func cancelNotification(identifier: String) func clearAllNotifications() } @@ -219,6 +220,10 @@ public class NotificationPresenter: NSObject, NotificationsProtocol { let userInfo = [ AppNotificationUserInfoKey.threadId: threadId ] + + let transaction = transaction as! YapDatabaseReadWriteTransaction + let identifier: String = UUID().uuidString + incomingMessage.setNotificationIdentifier(identifier, transaction: transaction) DispatchQueue.main.async { notificationBody = MentionUtilities.highlightMentions(in: notificationBody!, threadID: thread.uniqueId!) @@ -227,7 +232,8 @@ public class NotificationPresenter: NSObject, NotificationsProtocol { title: notificationTitle, body: notificationBody ?? "", userInfo: userInfo, - sound: sound) + sound: sound, + replacingIdentifier: identifier) } } @@ -260,6 +266,11 @@ public class NotificationPresenter: NSObject, NotificationsProtocol { sound: sound) } } + + @objc + public func cancelNotification(_ identifier: String) { + self.adaptee.cancelNotification(identifier: identifier) + } @objc public func cancelNotifications(threadId: String) { diff --git a/SessionMessagingKit/Messages/Signal/TSIncomingMessage.h b/SessionMessagingKit/Messages/Signal/TSIncomingMessage.h index 7cd8dd78b..d87f8aa39 100644 --- a/SessionMessagingKit/Messages/Signal/TSIncomingMessage.h +++ b/SessionMessagingKit/Messages/Signal/TSIncomingMessage.h @@ -18,6 +18,8 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, readonly) BOOL isUserMentioned; +@property (nonatomic, readonly, nullable) NSString *notificationIdentifier; + - (instancetype)initMessageWithTimestamp:(uint64_t)timestamp inThread:(nullable TSThread *)thread messageBody:(nullable NSString *)body @@ -88,6 +90,9 @@ NS_ASSUME_NONNULL_BEGIN - (void)markAsReadNowWithSendReadReceipt:(BOOL)sendReadReceipt transaction:(YapDatabaseReadWriteTransaction *)transaction; +- (void)setNotificationIdentifier:(NSString * _Nullable)notificationIdentifier + transaction:(YapDatabaseReadWriteTransaction *)transaction; + @end NS_ASSUME_NONNULL_END diff --git a/SessionMessagingKit/Messages/Signal/TSIncomingMessage.m b/SessionMessagingKit/Messages/Signal/TSIncomingMessage.m index a7e004b93..090c0c4a5 100644 --- a/SessionMessagingKit/Messages/Signal/TSIncomingMessage.m +++ b/SessionMessagingKit/Messages/Signal/TSIncomingMessage.m @@ -77,6 +77,7 @@ NS_ASSUME_NONNULL_BEGIN _read = NO; _serverTimestamp = serverTimestamp; _wasReceivedByUD = wasReceivedByUD; + _notificationIdentifier = nil; return self; } @@ -128,6 +129,12 @@ NS_ASSUME_NONNULL_BEGIN return (self.body != nil && [self.body containsString:[NSString stringWithFormat:@"@%@", userPublicKey]]) || (self.quotedMessage != nil && [self.quotedMessage.authorId isEqualToString:userPublicKey]); } +- (void)setNotificationIdentifier:(NSString * _Nullable)notificationIdentifier transaction:(nonnull YapDatabaseReadWriteTransaction *)transaction +{ + _notificationIdentifier = notificationIdentifier; + [self saveWithTransaction:transaction]; +} + #pragma mark - OWSReadTracking - (BOOL)shouldAffectUnreadCounts diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift index 684d5e176..36cb16529 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift @@ -227,6 +227,9 @@ extension MessageReceiver { let transaction = transaction as! YapDatabaseReadWriteTransaction if let author = message.author, let timestamp = message.timestamp, let messageToDelete = userPublicKey == message.sender ? TSOutgoingMessage.find(withTimestamp: timestamp) : TSIncomingMessage.find(withAuthorId: author, timestamp: timestamp, transaction: transaction) { + if let incomingMessage = messageToDelete as? TSIncomingMessage, let notificationIdentifier = incomingMessage.notificationIdentifier, !notificationIdentifier.isEmpty { + SSKEnvironment.shared.notificationsManager!.cancelNotification(notificationIdentifier) + } messageToDelete.remove(with: transaction) } } diff --git a/SessionMessagingKit/Sending & Receiving/Notifications/NotificationsProtocol.h b/SessionMessagingKit/Sending & Receiving/Notifications/NotificationsProtocol.h index 9fdeb18e4..4408ea47d 100644 --- a/SessionMessagingKit/Sending & Receiving/Notifications/NotificationsProtocol.h +++ b/SessionMessagingKit/Sending & Receiving/Notifications/NotificationsProtocol.h @@ -20,6 +20,7 @@ NS_ASSUME_NONNULL_BEGIN inThread:(TSThread *)thread transaction:(YapDatabaseReadTransaction *)transaction; +- (void)cancelNotification:(NSString *)identifier; - (void)clearAllNotifications; @end diff --git a/SignalUtilitiesKit/Utilities/NoopNotificationsManager.swift b/SignalUtilitiesKit/Utilities/NoopNotificationsManager.swift index 50e95bb6c..ffe624689 100644 --- a/SignalUtilitiesKit/Utilities/NoopNotificationsManager.swift +++ b/SignalUtilitiesKit/Utilities/NoopNotificationsManager.swift @@ -8,6 +8,10 @@ public class NoopNotificationsManager: NSObject, NotificationsProtocol { public func notifyUser(for incomingMessage: TSIncomingMessage, in thread: TSThread, transaction: YapDatabaseReadTransaction) { owsFailDebug("") } + + public func cancelNotification(_ identifier: String) { + owsFailDebug("") + } public func clearAllNotifications() { Logger.warn("clearAllNotifications")