Skip to content

Commit

Permalink
notify reactions while app is running
Browse files Browse the repository at this point in the history
  • Loading branch information
r10s committed Nov 23, 2024
1 parent 1e62c33 commit 0480cbb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
10 changes: 10 additions & 0 deletions DcCore/DcCore/DC/events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum Event {
public static let messageReadDeliveredFailedReaction = Notification.Name(rawValue: "messageReadDeliveredFailedReaction")
public static let incomingMessage = Notification.Name(rawValue: "incomingMessage")
public static let incomingMessageOnAnyAccount = Notification.Name(rawValue: "incomingMessageOnAnyAccount")
public static let incomingReaction = Notification.Name(rawValue: "incomingReaction")
public static let messagesNoticed = Notification.Name(rawValue: "messagesNoticed")

// Chats
Expand Down Expand Up @@ -145,6 +146,15 @@ public class DcEventHandler {
"account_id": accountId
])

case DC_EVENT_INCOMING_REACTION:
logger.info("📡[\(accountId)] incoming reaction")
NotificationCenter.default.post(name: Event.incomingReaction, object: nil, userInfo: [
"account_id": Int(accountId),
"contact_id": Int(data1),
"msg_id": Int(data2),
"reaction": event.data2String
])

case DC_EVENT_CONTACTS_CHANGED:
if accountId != dcAccounts.getSelected().id {
return
Expand Down
2 changes: 1 addition & 1 deletion DcNotificationService/NotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class NotificationService: UNNotificationServiceExtension {
let msg = dcContext.getMessage(id: event.data2Int)
let chat = dcContext.getChat(chatId: msg.chatId)
if !chat.isMuted {
let sender = msg.getSenderName(dcContext.getContact(id: event.data1Int))
let sender = dcContext.getContact(id: event.data1Int).displayName
let summary = (msg.summary(chars: 80) ?? "")
bestAttemptContent.title = chat.name
bestAttemptContent.body = String.localized(stringID: "reaction_by_other", parameter: sender, event.data2String, summary)
Expand Down
33 changes: 33 additions & 0 deletions deltachat-ios/Helper/NotificationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class NotificationManager {
self.dcContext = dcAccounts.getSelected()

NotificationCenter.default.addObserver(self, selector: #selector(NotificationManager.handleIncomingMessageOnAnyAccount(_:)), name: Event.incomingMessageOnAnyAccount, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(NotificationManager.handleIncomingReaction(_:)), name: Event.incomingReaction, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(NotificationManager.handleMessagesNoticed(_:)), name: Event.messagesNoticed, object: nil)
}

Expand Down Expand Up @@ -122,4 +123,36 @@ public class NotificationManager {
UIApplication.shared.endBackgroundTask(backgroundTask)
}
}

@objc private func handleIncomingReaction(_ notification: Notification) {
let backgroundTask = UIApplication.shared.beginBackgroundTask {
logger.info("incoming-reaction-task will end soon")
}

DispatchQueue.global().async { [weak self] in
guard let self, let ui = notification.userInfo else { return }
let eventContext = dcAccounts.get(id: ui["account_id"] as? Int ?? 0)
if !eventContext.isMuted() {
let msg = eventContext.getMessage(id: ui["msg_id"] as? Int ?? 0)
let chat = eventContext.getChat(chatId: msg.chatId)
if !chat.isMuted {
let contact = eventContext.getContact(id: ui["contact_id"] as? Int ?? 0)
let summary = (msg.summary(chars: 80) ?? "")
let reaction = ui["reaction"] as? String ?? ""

let content = UNMutableNotificationContent()
content.title = chat.name
content.body = String.localized(stringID: "reaction_by_other", parameter: contact.displayName, reaction, summary)
content.userInfo["account_id"] = eventContext.id
content.userInfo["chat_id"] = chat.id
content.userInfo["message_id"] = msg.id

let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}

UIApplication.shared.endBackgroundTask(backgroundTask) // this line must be reached to balance call to `beginBackgroundTask` above
}
}
}

0 comments on commit 0480cbb

Please sign in to comment.