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 Oct 20, 2024
1 parent fd1c7a8 commit d7b19d0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
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 @@ -140,6 +141,15 @@ public class DcEventHandler {
"chat_id": Int(data1),
])

case DC_EVENT_INCOMING_REACTION:

Check failure on line 144 in DcCore/DcCore/DC/events.swift

View workflow job for this annotation

GitHub Actions / build

cannot find 'DC_EVENT_INCOMING_REACTION' in scope
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
36 changes: 34 additions & 2 deletions deltachat-ios/Helper/NotificationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class NotificationManager {

NotificationCenter.default.addObserver(self, selector: #selector(NotificationManager.handleIncomingMessageOnAnyAccount(_:)), name: Event.incomingMessageOnAnyAccount, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(NotificationManager.handleIncomingMessage(_:)), name: Event.incomingMessage, 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 @@ -86,8 +87,6 @@ public class NotificationManager {
}

@objc private func handleIncomingMessage(_ notification: Notification) {
// TODO: handle DC_EVENT_INCOMING_REACTION

// make sure to balance each call to `beginBackgroundTask` with `endBackgroundTask`
let backgroundTask = UIApplication.shared.beginBackgroundTask {
// we cannot easily stop the task,
Expand Down Expand Up @@ -127,4 +126,37 @@ 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
content.sound = .default

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

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

0 comments on commit d7b19d0

Please sign in to comment.