Skip to content

Commit

Permalink
Notifications: Add helper function to get conversation id (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
anian03 authored Nov 18, 2024
1 parent 03f85a1 commit 689cd5c
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Sources/PushNotifications/PushNotificationResponseHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 689cd5c

Please sign in to comment.