From 689cd5c3a9a433af86201a73fa3a3c6600a522c1 Mon Sep 17 00:00:00 2001 From: Anian Schleyer <98647423+anian03@users.noreply.github.com> Date: Mon, 18 Nov 2024 12:11:02 +0100 Subject: [PATCH] `Notifications`: Add helper function to get conversation id (#104) --- .../PushNotificationResponseHandler.swift | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Sources/PushNotifications/PushNotificationResponseHandler.swift b/Sources/PushNotifications/PushNotificationResponseHandler.swift index 80353a8..c04b92e 100644 --- a/Sources/PushNotifications/PushNotificationResponseHandler.swift +++ b/Sources/PushNotifications/PushNotificationResponseHandler.swift @@ -9,6 +9,28 @@ import Foundation public class PushNotificationResponseHandler { + public static func getConversationId(from userInfo: [AnyHashable: Any]) -> Int? { + guard let targetString = userInfo[PushNotificationUserInfoKeys.target] as? String, + let typeString = userInfo[PushNotificationUserInfoKeys.type] as? String else { + return nil + } + + guard let type = PushNotificationType(rawValue: typeString) else { return nil } + + switch type { + case .newReplyForCoursePost, .newCoursePost, .newAnnouncementPost, + .newExercisePost, .newReplyForExercisePost, .newLecturePost, + .newReplyForLecturePost, .conversationCreateGroupChat, + .conversationAddUserChannel, .conversationAddUserGroupChat, + .conversationRemoveUserChannel, .conversationRemoveUserGroupChat, + .conversationNewMessage: + guard let target = try? JSONDecoder().decode(ConversationTarget.self, from: Data(targetString.utf8)) else { return nil } + return target.conversation + default: + return nil + } + } + public static func getTarget(userInfo: [AnyHashable: Any]) -> String? { guard let targetString = userInfo[PushNotificationUserInfoKeys.target] as? String, let typeString = userInfo[PushNotificationUserInfoKeys.type] as? String else {