diff --git a/Riot/Managers/PushNotification/PushNotificationService.m b/Riot/Managers/PushNotification/PushNotificationService.m index 8bf821b77..31f45be07 100644 --- a/Riot/Managers/PushNotification/PushNotificationService.m +++ b/Riot/Managers/PushNotification/PushNotificationService.m @@ -323,6 +323,20 @@ Matrix session observer used to detect new opened sessions. #pragma mark - UNUserNotificationCenterDelegate +- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler +{ + if (notification.request.content.userInfo[Constants.userInfoKeyPresentNotificationAlways]) + { + completionHandler(UNNotificationPresentationOptionBadge + | UNNotificationPresentationOptionSound + | UNNotificationPresentationOptionAlert); + } + else + { + completionHandler(UNNotificationPresentationOptionNone); + } +} + // iOS 10+, see application:handleActionWithIdentifier:forLocalNotification:withResponseInfo:completionHandler: - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { diff --git a/Riot/Utils/Constants.swift b/Riot/Utils/Constants.swift index 886615072..2a3c28e58 100644 --- a/Riot/Utils/Constants.swift +++ b/Riot/Utils/Constants.swift @@ -16,9 +16,13 @@ import Foundation -enum Constants { +@objcMembers +class Constants: NSObject { - static let toBeRemovedNotificationCategoryIdentifier = "TO_BE_REMOVED" - static let callInviteNotificationCategoryIdentifier = "CALL_INVITE" + static let toBeRemovedNotificationCategoryIdentifier: String = "TO_BE_REMOVED" + static let callInviteNotificationCategoryIdentifier: String = "CALL_INVITE" + + /// Notification userInfo key to present a notification even if the app is on foreground. Value should be set as a Bool for this key. + static let userInfoKeyPresentNotificationAlways: String = "ALWAYS_PRESENT_NOTIFICATION" } diff --git a/RiotNSE/NotificationService.swift b/RiotNSE/NotificationService.swift index ef594ff92..1a32564cf 100644 --- a/RiotNSE/NotificationService.swift +++ b/RiotNSE/NotificationService.swift @@ -269,6 +269,7 @@ class NotificationService: UNNotificationServiceExtension { case .success(let roomState): var notificationTitle: String? var notificationBody: String? + var additionalUserInfo: [AnyHashable: Any]? var threadIdentifier: String? = roomId let eventSenderName = roomState.members.memberName(event.sender) @@ -384,6 +385,8 @@ class NotificationService: UNNotificationServiceExtension { // only send VoIP pushes if ringing is enabled for group calls if RiotSettings.shared.enableRingingForGroupCalls { self.sendVoipPush(forEvent: event) + } else { + additionalUserInfo = [Constants.userInfoKeyPresentNotificationAlways: true] } } } @@ -409,7 +412,8 @@ class NotificationService: UNNotificationServiceExtension { threadIdentifier: threadIdentifier, userId: currentUserId, event: event, - pushRule: pushRule) + pushRule: pushRule, + additionalInfo: additionalUserInfo) NSLog("[NotificationService] notificationContentForEvent: Calling onComplete.") onComplete(notificationContent) @@ -425,7 +429,8 @@ class NotificationService: UNNotificationServiceExtension { threadIdentifier: String?, userId: String?, event: MXEvent, - pushRule: MXPushRule?) -> UNNotificationContent { + pushRule: MXPushRule?, + additionalInfo: [AnyHashable: Any]? = nil) -> UNNotificationContent { let notificationContent = UNMutableNotificationContent() if let title = title { @@ -443,12 +448,16 @@ class NotificationService: UNNotificationServiceExtension { if let soundName = notificationSoundName(fromPushRule: pushRule) { notificationContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: soundName)) } - notificationContent.userInfo = notificationUserInfo(forEvent: event, andUserId: userId) + notificationContent.userInfo = notificationUserInfo(forEvent: event, + andUserId: userId, + additionalInfo: additionalInfo) return notificationContent } - private func notificationUserInfo(forEvent event: MXEvent, andUserId userId: String?) -> [AnyHashable: Any] { + private func notificationUserInfo(forEvent event: MXEvent, + andUserId userId: String?, + additionalInfo: [AnyHashable: Any]? = nil) -> [AnyHashable: Any] { var notificationUserInfo: [AnyHashable: Any] = [ "type": "full", "room_id": event.roomId as Any, @@ -457,6 +466,11 @@ class NotificationService: UNNotificationServiceExtension { if let userId = userId { notificationUserInfo["user_id"] = userId } + if let additionalInfo = additionalInfo { + for (key, value) in additionalInfo { + notificationUserInfo[key] = value + } + } return notificationUserInfo }