From 447d24898a84ce2663298e3f6d6594cf8ca38e27 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Fri, 17 Jun 2022 14:26:23 +1000 Subject: [PATCH] feat: Notification --- Session/Notifications/AppNotifications.swift | 1 + .../NSENotificationPresenter.swift | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/Session/Notifications/AppNotifications.swift b/Session/Notifications/AppNotifications.swift index 0fb20102d..b26198a89 100644 --- a/Session/Notifications/AppNotifications.swift +++ b/Session/Notifications/AppNotifications.swift @@ -306,6 +306,7 @@ public class NotificationPresenter: NSObject, NotificationsProtocol { public func notifyUser(forReaction reactMessage: ReactMessage, in thread: TSThread, transaction: YapDatabaseReadTransaction) { guard !thread.isMuted else { return } guard !thread.isGroupThread() else { return } // We do NOT notify emoji reacts in groups + guard !thread.isMessageRequest(using: transaction) else { return } guard let sender = reactMessage.sender, let emoji = reactMessage.emoji else { return } guard let threadId = thread.uniqueId else { return } diff --git a/SessionNotificationServiceExtension/NSENotificationPresenter.swift b/SessionNotificationServiceExtension/NSENotificationPresenter.swift index b7b747db1..69f75660f 100644 --- a/SessionNotificationServiceExtension/NSENotificationPresenter.swift +++ b/SessionNotificationServiceExtension/NSENotificationPresenter.swift @@ -142,7 +142,33 @@ public class NSENotificationPresenter: NSObject, NotificationsProtocol { } public func notifyUser(forReaction reactMessage: ReactMessage, in thread: TSThread, transaction: YapDatabaseReadTransaction) { + guard !thread.isMuted else { return } + guard !thread.isGroupThread() else { return } // We do NOT notify emoji reacts in groups + guard !thread.isMessageRequest(using: transaction) else { return } + guard let sender = reactMessage.sender, let emoji = reactMessage.emoji else { return } + guard let threadID = thread.uniqueId else { return } + + let context = Contact.context(for: thread) + let senderName = Storage.shared.getContact(with: sender, using: transaction)?.displayName(for: context) ?? sender + + let notificationTitle = "Session" + var notificationBody = String(format: "EMOJI_REACTS_NOTIFICATION".localized(), senderName, emoji) + let notificationsPreference = Environment.shared.preferences!.notificationPreviewType() + switch notificationsPreference { + case .namePreview: break + default: notificationBody = NotificationStrings.incomingMessageBody + } + + var userInfo: [String:Any] = [ NotificationServiceExtension.isFromRemoteKey : true ] + userInfo[NotificationServiceExtension.threadIdKey] = threadID + + let notificationContent = UNMutableNotificationContent() + notificationContent.userInfo = userInfo + notificationContent.sound = OWSSounds.notificationSound(for: thread).notificationSound(isQuiet: false) + notificationContent.title = notificationTitle + notificationContent.body = notificationBody + addNotifcationRequest(identifier: UUID().uuidString, notificationContent: notificationContent, trigger: nil) } public func cancelNotification(_ identifier: String) {