             NSLog("[NotificationService] preprocessPayload: Do not preprocess because app protection is set")
             return
         }
-        guard let session = NotificationService.mxSession else { return }
-        guard let roomDisplayName = session.store.summary?(ofRoom: roomId)?.displayname else { return }
-        let isDirect = session.directUserId(inRoom: roomId) != nil
-        if isDirect {
+        guard let roomSummary = NotificationService.backgroundSyncService.roomSummary(forRoomId: roomId) else { return }
+        guard let roomDisplayName = roomSummary.displayname else { return }
+        if roomSummary.isDirect == true {
             bestAttemptContents[eventId]?.body = NSString.localizedUserNotificationString(forKey: "MESSAGE_FROM_X", arguments: [roomDisplayName as Any])
         } else {
             bestAttemptContents[eventId]?.body = NSString.localizedUserNotificationString(forKey: "MESSAGE_IN_X", arguments: [roomDisplayName as Any])

        }
    }
    
    func fallbackToBestAttemptContent(forEventId eventId: String) {
        NSLog("[NotificationService] fallbackToBestAttemptContent: method called.")
        
        guard let content = bestAttemptContents[eventId] else {

        contentHandlers[eventId]?(content)
    }
    
    private func notificationContent(forEvent event: MXEvent, forAccount account: MXKAccount, onComplete: @escaping (UNNotificationContent?) -> Void) {
        guard let content = event.content, content.count > 0 else {
            NSLog("[NotificationService] notificationContentForEvent: empty event content")
            onComplete(nil)
            return
        }
        
        let roomId = event.roomId!
        let isRoomMentionsOnly = NotificationService.backgroundSyncService.isRoomMentionsOnly(roomId)
        let roomSummary = NotificationService.backgroundSyncService.roomSummary(forRoomId: roomId)
        
        NSLog("[NotificationService] notificationContentForEvent: Attempt to fetch the room state")
        
        NotificationService.backgroundSyncService.roomState(forRoomId: roomId, completion: { (response) in
            switch response {
            case .success(let roomState):
                var notificationTitle: String?
                var notificationBody: String?
                
                var threadIdentifier: String? = roomId
                let eventSenderName = roomState.members.memberName(event.sender)
                let currentUserId = account.mxCredentials.userId
                let roomDisplayName = roomSummary?.displayname
                let pushRule = NotificationService.backgroundSyncService.pushRule(matching: event, roomState: roomState)
                
                switch event.eventType {
                case .callInvite:
                    let offer = event.content["offer"] as? [AnyHashable: Any]
                    let sdp = offer?["sdp"] as? String
                    let isVideoCall = sdp?.contains("m=video") ?? false
                    
                    if isVideoCall {
                        notificationBody = NSString.localizedUserNotificationString(forKey: "VIDEO_CALL_FROM_USER", arguments: [eventSenderName as Any])
                    } else {
                        notificationBody = NSString.localizedUserNotificationString(forKey: "VOICE_CALL_FROM_USER", arguments: [eventSenderName as Any])
                    }
                    
                    // call notifications should stand out from normal messages, so we don't stack them
                    threadIdentifier = nil
                    self.sendVoipPush(forEvent: event)
                case .roomMessage, .roomEncrypted:
                    if isRoomMentionsOnly {
                        // A local notification will be displayed only for highlighted notification.
                        var isHighlighted = false
                        
                        // Check whether is there an highlight tweak on it
                        for ruleAction in pushRule?.actions ?? [] {
                            guard let action = ruleAction as? MXPushRuleAction else { continue }
                            guard action.actionType == MXPushRuleActionTypeSetTweak else { continue }
                            guard action.parameters["set_tweak"] as? String == "highlight" else { continue }
                            // Check the highlight tweak "value"
                            // If not present, highlight. Else check its value before highlighting
                            if nil == action.parameters["value"] || true == (action.parameters["value"] as? Bool) {
                                isHighlighted = true
                                break
                            }
                        }
                        
                        if !isHighlighted {
                            // Ignore this notif.
                            NSLog("[NotificationService] notificationContentForEvent: Ignore non highlighted notif in mentions only room")
                            onComplete(nil)
                            return
                        }
                    }
                    
                    var msgType = event.content["msgtype"] as? String
                    let messageContent = event.content["body"] as? String
                    
                    if event.isEncrypted && !self.showDecryptedContentInNotifications {
                        // Hide the content
                        msgType = nil
                    }
                    
                    let isIncomingEvent = event.sender != currentUserId
                    
                    // Display the room name only if it is different than the sender name
                    if roomDisplayName != nil && roomDisplayName != eventSenderName {
                        notificationTitle = NSString.localizedUserNotificationString(forKey: "MSG_FROM_USER_IN_ROOM_TITLE", arguments: [eventSenderName as Any, roomDisplayName as Any])
                        
                        if msgType == kMXMessageTypeText {
                            notificationBody = messageContent
                        } else if msgType == kMXMessageTypeEmote {
                            notificationBody = NSString.localizedUserNotificationString(forKey: "ACTION_FROM_USER", arguments: [eventSenderName as Any, messageContent as Any])
                        } else if msgType == kMXMessageTypeImage {
                            notificationBody = NSString.localizedUserNotificationString(forKey: "IMAGE_FROM_USER", arguments: [eventSenderName as Any, messageContent as Any])
                        } else {
                            // Encrypted messages falls here
                            notificationBody = NSString.localizedUserNotificationString(forKey: "MESSAGE", arguments: [])
                        }
                    } else {
                        notificationTitle = eventSenderName
                        
                        switch msgType {
                        case kMXMessageTypeText:
                            notificationBody = messageContent
                            break
                        case kMXMessageTypeEmote:
                            notificationBody = NSString.localizedUserNotificationString(forKey: "ACTION_FROM_USER", arguments: [eventSenderName as Any, messageContent as Any])
                            break
                        case kMXMessageTypeImage:
                            notificationBody = NSString.localizedUserNotificationString(forKey: "IMAGE_FROM_USER", arguments: [eventSenderName as Any, messageContent as Any])
                            break
                        default:
                            // Encrypted messages falls here
                            notificationBody = NSString.localizedUserNotificationString(forKey: "MESSAGE", arguments: [])
                            break
                        }
                    }
                    break
                case .roomMember:
                    if roomDisplayName != nil && roomDisplayName != eventSenderName {
                        notificationBody = NSString.localizedUserNotificationString(forKey: "USER_INVITE_TO_NAMED_ROOM", arguments: [eventSenderName as Any, roomDisplayName as Any])
                    } else {
                        notificationBody = NSString.localizedUserNotificationString(forKey: "USER_INVITE_TO_CHAT", arguments: [eventSenderName as Any])
                    }
                case .sticker:
                    if roomDisplayName != nil && roomDisplayName != eventSenderName {
                        notificationTitle = NSString.localizedUserNotificationString(forKey: "MSG_FROM_USER_IN_ROOM_TITLE", arguments: [eventSenderName as Any, roomDisplayName as Any])
                    } else {
                        notificationTitle = eventSenderName
                    }
                    
                    notificationBody = NSString.localizedUserNotificationString(forKey: "STICKER_FROM_USER", arguments: [eventSenderName as Any])
                default:
                    break
                }
                
                if self.localAuthenticationService.isProtectionSet {
                    NSLog("[NotificationService] notificationContentForEvent: Resetting title and body because app protection is set")
                    notificationBody = NSString.localizedUserNotificationString(forKey: "MESSAGE_PROTECTED", arguments: [])
                    notificationTitle = nil
                }
                
                guard notificationBody != nil else {
                    NSLog("[NotificationService] notificationContentForEvent: notificationBody is nil")
                    onComplete(nil)
                    return
                }
                
                let notificationContent = self.notificationContent(withTitle: notificationTitle,
                                                                   body: notificationBody,
                                                                   threadIdentifier: threadIdentifier,
                                                                   userId: currentUserId,
                                                                   event: event,
                                                                   pushRule: pushRule)
                
                NSLog("[NotificationService] notificationContentForEvent: Calling onComplete.")
                onComplete(notificationContent)
            case .failure(let error):
                NSLog("[NotificationService] notificationContentForEvent: error: \(error)")
                onComplete(nil)
            }
        })
    }
    
    func notificationContent(withTitle title: String?,
