From a951c84e48af21849ae44f97dcf8724fcd2e73c7 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Wed, 28 Jul 2021 16:13:43 +1000 Subject: [PATCH] prepare for push notification filter --- .../NotificationServiceExtension.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/SessionNotificationServiceExtension/NotificationServiceExtension.swift b/SessionNotificationServiceExtension/NotificationServiceExtension.swift index 38e2f6966..35cd7f1ac 100644 --- a/SessionNotificationServiceExtension/NotificationServiceExtension.swift +++ b/SessionNotificationServiceExtension/NotificationServiceExtension.swift @@ -14,6 +14,7 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension override public func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { self.contentHandler = contentHandler self.notificationContent = request.content.mutableCopy() as? UNMutableNotificationContent + let userPublicKey = SNGeneralUtilities.getUserPublicKey() // Abort if the main app is running var isMainAppAndActive = false @@ -36,6 +37,10 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension do { let (message, proto) = try MessageReceiver.parse(envelopeAsData, openGroupMessageServerID: nil, using: transaction) let senderPublicKey = message.sender! + if (senderPublicKey == userPublicKey) { + // Ignore PNs of messages sent from current user. + return self.completeSilenty() + } var senderDisplayName = Storage.shared.getContact(with: senderPublicKey)?.displayName(for: .regular) ?? senderPublicKey let snippet: String var userInfo: [String:Any] = [ NotificationServiceExtension.isFromRemoteKey : true ] @@ -56,6 +61,10 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension if let thread = TSThread.fetch(uniqueId: threadID, transaction: transaction), let group = thread as? TSGroupThread, group.groupModel.groupType == .closedGroup { // Should always be true because we don't get PNs for open groups senderDisplayName = String(format: NotificationStrings.incomingGroupMessageTitleFormat, senderDisplayName, group.groupModel.groupName ?? MessageStrings.newGroupDefaultTitle) + if let messageBody = tsIncomingMessage.previewText(with: transaction).filterForDisplay, !messageBody.contains("@\(userPublicKey)") && group.isOnlyNotifyMentions { + // Ignore PNs if the group is set to only notify mentions + return self.completeSilenty() + } } case let closedGroupControlMessage as ClosedGroupControlMessage: // TODO: We could consider actually handling the update here. Not sure if there's enough time though, seeing as though