TF-2384 Show multiple notification with list emails
Signed-off-by: dab246 <tdvu@linagora.com> (cherry picked from commit e19c37c724f5e1b06e78de459a05a34f2437fe5e)
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
import UserNotifications
|
import UserNotifications
|
||||||
|
|
||||||
class NotificationService: UNNotificationServiceExtension {
|
class NotificationService: UNNotificationServiceExtension {
|
||||||
|
|
||||||
|
private let timeIntervalNotificationTrigger: Int = 2
|
||||||
|
private let newEmailDefaultMessage: String = "You have new emails"
|
||||||
|
|
||||||
private var handler: ((UNNotificationContent) -> Void)?
|
private var handler: ((UNNotificationContent) -> Void)?
|
||||||
private var modifiedContent: UNMutableNotificationContent?
|
private var modifiedContent: UNMutableNotificationContent?
|
||||||
|
|
||||||
@@ -9,22 +12,22 @@ class NotificationService: UNNotificationServiceExtension {
|
|||||||
accessGroup: InfoPlistReader.main.keychainAccessGroupIdentifier)
|
accessGroup: InfoPlistReader.main.keychainAccessGroupIdentifier)
|
||||||
|
|
||||||
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
|
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
|
||||||
guard let payloadData = request.content.userInfo as? [String: Any],
|
|
||||||
!keychainController.retrieveSharingSessions().isEmpty else {
|
|
||||||
return self.discard()
|
|
||||||
}
|
|
||||||
|
|
||||||
handler = contentHandler
|
handler = contentHandler
|
||||||
modifiedContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
|
modifiedContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
|
||||||
|
|
||||||
self.modifiedContent?.title = InfoPlistReader(bundle: .app).bundleDisplayName
|
self.modifiedContent?.title = InfoPlistReader(bundle: .app).bundleDisplayName
|
||||||
|
|
||||||
|
guard let payloadData = request.content.userInfo as? [String: Any],
|
||||||
|
!keychainController.retrieveSharingSessions().isEmpty else {
|
||||||
|
self.modifiedContent?.body = newEmailDefaultMessage
|
||||||
|
self.modifiedContent?.badge = NSNumber(value: 1)
|
||||||
|
return self.notify()
|
||||||
|
}
|
||||||
|
|
||||||
Task {
|
Task {
|
||||||
await handleGetNewEmails(payloadData: payloadData)
|
await handleGetNewEmails(payloadData: payloadData)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
override func serviceExtensionTimeWillExpire() {
|
override func serviceExtensionTimeWillExpire() {
|
||||||
// Called just before the extension will be terminated by the system.
|
// Called just before the extension will be terminated by the system.
|
||||||
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
|
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
|
||||||
@@ -35,7 +38,9 @@ class NotificationService: UNNotificationServiceExtension {
|
|||||||
let mapStateChanges: [String: [TypeName: String]] = PayloadParser.shared.parsingPayloadNotification(payloadData: payloadData)
|
let mapStateChanges: [String: [TypeName: String]] = PayloadParser.shared.parsingPayloadNotification(payloadData: payloadData)
|
||||||
|
|
||||||
if (mapStateChanges.isEmpty) {
|
if (mapStateChanges.isEmpty) {
|
||||||
return self.discard()
|
self.modifiedContent?.body = newEmailDefaultMessage
|
||||||
|
self.modifiedContent?.badge = NSNumber(value: 1)
|
||||||
|
return self.notify()
|
||||||
} else {
|
} else {
|
||||||
guard let currentAccountId = mapStateChanges.keys.first,
|
guard let currentAccountId = mapStateChanges.keys.first,
|
||||||
let keychainSharingSession = keychainController.retrieveSharingSessionFromKeychain(accountId: currentAccountId),
|
let keychainSharingSession = keychainController.retrieveSharingSessionFromKeychain(accountId: currentAccountId),
|
||||||
@@ -44,7 +49,9 @@ class NotificationService: UNNotificationServiceExtension {
|
|||||||
let oldEmailState = keychainSharingSession.emailState,
|
let oldEmailState = keychainSharingSession.emailState,
|
||||||
newEmailState != oldEmailState,
|
newEmailState != oldEmailState,
|
||||||
keychainSharingSession.tokenOIDC != nil || keychainSharingSession.basicAuth != nil else {
|
keychainSharingSession.tokenOIDC != nil || keychainSharingSession.basicAuth != nil else {
|
||||||
return self.discard()
|
self.modifiedContent?.body = newEmailDefaultMessage
|
||||||
|
self.modifiedContent?.badge = NSNumber(value: 1)
|
||||||
|
return self.notify()
|
||||||
}
|
}
|
||||||
|
|
||||||
JmapClient.shared.getNewEmails(
|
JmapClient.shared.getNewEmails(
|
||||||
@@ -57,24 +64,65 @@ class NotificationService: UNNotificationServiceExtension {
|
|||||||
onSuccess: { emails in
|
onSuccess: { emails in
|
||||||
self.keychainController.updateEmailStateToKeychain(accountId: keychainSharingSession.accountId, newState: newEmailState)
|
self.keychainController.updateEmailStateToKeychain(accountId: keychainSharingSession.accountId, newState: newEmailState)
|
||||||
|
|
||||||
self.modifiedContent?.subtitle = emails.first?.subject ?? ""
|
if (emails.count > 1) {
|
||||||
self.modifiedContent?.body = emails.first?.preview ?? ""
|
for email in emails {
|
||||||
self.modifiedContent?.badge = NSNumber(value: emails.count)
|
if (email.id == emails.last?.id) {
|
||||||
return self.notify()
|
break
|
||||||
},
|
}
|
||||||
onFailure: { error in
|
self.scheduleLocalNotification(email: email)
|
||||||
if let errorJmap = error as? JmapExceptions, errorJmap == JmapExceptions.notFoundNewEmails {
|
}
|
||||||
return self.discard()
|
|
||||||
|
let delayTimeIntervalNotification: TimeInterval = TimeInterval(self.timeIntervalNotificationTrigger * (emails.count - 1))
|
||||||
|
|
||||||
|
DispatchQueue.main.asyncAfter(deadline: .now() + delayTimeIntervalNotification) {
|
||||||
|
self.modifiedContent?.subtitle = emails.last?.subject ?? ""
|
||||||
|
self.modifiedContent?.body = emails.last?.preview ?? ""
|
||||||
|
self.modifiedContent?.badge = NSNumber(value: emails.count)
|
||||||
|
self.modifiedContent?.userInfo[JmapConstants.EMAIL_ID] = emails.last?.id ?? ""
|
||||||
|
return self.notify()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
self.modifiedContent?.body = "You have new emails"
|
self.modifiedContent?.subtitle = emails.first?.subject ?? ""
|
||||||
|
self.modifiedContent?.body = emails.first?.preview ?? ""
|
||||||
self.modifiedContent?.badge = NSNumber(value: 1)
|
self.modifiedContent?.badge = NSNumber(value: 1)
|
||||||
|
self.modifiedContent?.userInfo[JmapConstants.EMAIL_ID] = emails.first?.id ?? ""
|
||||||
return self.notify()
|
return self.notify()
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
onFailure: { error in
|
||||||
|
self.modifiedContent?.body = newEmailDefaultMessage
|
||||||
|
self.modifiedContent?.badge = NSNumber(value: 1)
|
||||||
|
return self.notify()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func scheduleLocalNotification(email: Email) {
|
||||||
|
// Create a notification content
|
||||||
|
let content = UNMutableNotificationContent()
|
||||||
|
content.title = InfoPlistReader(bundle: .app).bundleDisplayName
|
||||||
|
content.subtitle = email.subject ?? ""
|
||||||
|
content.body = email.preview ?? ""
|
||||||
|
content.sound = .default
|
||||||
|
content.userInfo[JmapConstants.EMAIL_ID] = "\(email.id)"
|
||||||
|
|
||||||
|
// Create a notification trigger
|
||||||
|
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timeIntervalNotificationTrigger), repeats: false)
|
||||||
|
|
||||||
|
// Create a notification request
|
||||||
|
let request = UNNotificationRequest(identifier: "\(email.id)", content: content, trigger: trigger)
|
||||||
|
|
||||||
|
// Schedule the notification
|
||||||
|
UNUserNotificationCenter.current().add(request) { error in
|
||||||
|
if let error = error {
|
||||||
|
TwakeLogger.shared.log(message: "Error scheduling notification: \(error.localizedDescription)")
|
||||||
|
} else {
|
||||||
|
TwakeLogger.shared.log(message: "Notification scheduled successfully")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private func notify() {
|
private func notify() {
|
||||||
guard let modifiedContent else {
|
guard let modifiedContent else {
|
||||||
return discard()
|
return discard()
|
||||||
|
|||||||
Reference in New Issue
Block a user