TF-2464 Add sender name as title notification in iOS
Signed-off-by: dab246 <tdvu@linagora.com> (cherry picked from commit 7f54c0366e30f131b506a4b6e841b36a66d18087)
This commit is contained in:
@@ -6,4 +6,11 @@ struct Email: Codable {
|
|||||||
let preview: String?
|
let preview: String?
|
||||||
let from: [EmailAddress]?
|
let from: [EmailAddress]?
|
||||||
let receivedAt: String?
|
let receivedAt: String?
|
||||||
|
|
||||||
|
func getSenderName() -> String? {
|
||||||
|
if (from == nil || from?.isEmpty == true) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return from?.first?.name ?? from?.first?.email
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,37 +16,38 @@ class NotificationService: UNNotificationServiceExtension {
|
|||||||
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?.badge = NSNumber(value: 1)
|
|
||||||
|
|
||||||
guard let payloadData = request.content.userInfo as? [String: Any],
|
guard let payloadData = request.content.userInfo as? [String: Any],
|
||||||
!keychainController.retrieveSharingSessions().isEmpty else {
|
!keychainController.retrieveSharingSessions().isEmpty else {
|
||||||
self.modifiedContent?.body = NSLocalizedString(newNotificationDefaultMessageKey, comment: "Localizable")
|
self.showDefaultNotification(message: NSLocalizedString(self.newNotificationDefaultMessageKey, comment: "Localizable"))
|
||||||
return self.notify()
|
return self.notify()
|
||||||
}
|
}
|
||||||
|
|
||||||
if #available(iOSApplicationExtension 13.0, *) {
|
if #available(iOSApplicationExtension 13.0, *) {
|
||||||
Task {
|
Task {
|
||||||
await handleGetNewEmails(payloadData: payloadData)
|
await handlePushNotificationForNewEmail(payloadData: payloadData)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.modifiedContent?.body = NSLocalizedString(newEmailDefaultMessageKey, comment: "Localizable")
|
self.handleGetNewEmails(payloadData: payloadData)
|
||||||
return self.notify()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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.
|
||||||
notify()
|
self.showDefaultNotification(message: NSLocalizedString(self.newNotificationDefaultMessageKey, comment: "Localizable"))
|
||||||
|
self.notify()
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(iOSApplicationExtension 13.0.0, *)
|
@available(iOSApplicationExtension 13.0.0, *)
|
||||||
private func handleGetNewEmails(payloadData: [String: Any]) async {
|
private func handlePushNotificationForNewEmail(payloadData: [String: Any]) async {
|
||||||
|
self.handleGetNewEmails(payloadData: payloadData)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func handleGetNewEmails(payloadData: [String: Any]) {
|
||||||
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) {
|
||||||
self.modifiedContent?.body = NSLocalizedString(newNotificationDefaultMessageKey, comment: "Localizable")
|
self.showDefaultNotification(message: NSLocalizedString(self.newNotificationDefaultMessageKey, comment: "Localizable"))
|
||||||
return self.notify()
|
return self.notify()
|
||||||
} else {
|
} else {
|
||||||
guard let currentAccountId = mapStateChanges.keys.first,
|
guard let currentAccountId = mapStateChanges.keys.first,
|
||||||
@@ -54,13 +55,13 @@ class NotificationService: UNNotificationServiceExtension {
|
|||||||
keychainSharingSession.tokenOIDC != nil || keychainSharingSession.basicAuth != nil,
|
keychainSharingSession.tokenOIDC != nil || keychainSharingSession.basicAuth != nil,
|
||||||
let listStateOfAccount = mapStateChanges[currentAccountId],
|
let listStateOfAccount = mapStateChanges[currentAccountId],
|
||||||
let newEmailDeliveryState = listStateOfAccount[TypeName.emailDelivery] else {
|
let newEmailDeliveryState = listStateOfAccount[TypeName.emailDelivery] else {
|
||||||
self.modifiedContent?.body = NSLocalizedString(newNotificationDefaultMessageKey, comment: "Localizable")
|
self.showDefaultNotification(message: NSLocalizedString(self.newNotificationDefaultMessageKey, comment: "Localizable"))
|
||||||
return self.notify()
|
return self.notify()
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let oldEmailDeliveryState = keychainSharingSession.emailDeliveryState ?? keychainSharingSession.emailState,
|
guard let oldEmailDeliveryState = keychainSharingSession.emailDeliveryState ?? keychainSharingSession.emailState,
|
||||||
newEmailDeliveryState != oldEmailDeliveryState else {
|
newEmailDeliveryState != oldEmailDeliveryState else {
|
||||||
self.modifiedContent?.body = NSLocalizedString(newEmailDefaultMessageKey, comment: "Localizable")
|
self.showDefaultNotification(message: NSLocalizedString(self.newEmailDefaultMessageKey, comment: "Localizable"))
|
||||||
return self.notify()
|
return self.notify()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,56 +75,92 @@ class NotificationService: UNNotificationServiceExtension {
|
|||||||
tokenEndpointUrl: keychainSharingSession.tokenEndpoint,
|
tokenEndpointUrl: keychainSharingSession.tokenEndpoint,
|
||||||
oidcScopes: keychainSharingSession.oidcScopes,
|
oidcScopes: keychainSharingSession.oidcScopes,
|
||||||
onComplete: { (emails, errors) in
|
onComplete: { (emails, errors) in
|
||||||
if emails.isEmpty {
|
do {
|
||||||
self.modifiedContent?.body = NSLocalizedString(self.newNotificationDefaultMessageKey, comment: "Localizable")
|
if emails.isEmpty {
|
||||||
return self.notify()
|
self.showDefaultNotification(message: NSLocalizedString(self.newEmailDefaultMessageKey, comment: "Localizable"))
|
||||||
} else {
|
|
||||||
self.keychainController.updateEmailDeliveryStateToKeychain(
|
|
||||||
accountId: keychainSharingSession.accountId,
|
|
||||||
newEmailDeliveryState: newEmailDeliveryState
|
|
||||||
)
|
|
||||||
|
|
||||||
if (emails.count > 1) {
|
|
||||||
for email in emails {
|
|
||||||
if (email.id == emails.last?.id) {
|
|
||||||
self.modifiedContent?.subtitle = email.subject ?? ""
|
|
||||||
self.modifiedContent?.body = email.preview ?? ""
|
|
||||||
self.modifiedContent?.sound = .default
|
|
||||||
self.modifiedContent?.badge = NSNumber(value: emails.count)
|
|
||||||
self.modifiedContent?.userInfo[JmapConstants.EMAIL_ID] = email.id
|
|
||||||
return self.notify()
|
|
||||||
}
|
|
||||||
self.scheduleLocalNotification(email: email)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
self.modifiedContent?.subtitle = emails.first?.subject ?? ""
|
|
||||||
self.modifiedContent?.body = emails.first?.preview ?? ""
|
|
||||||
self.modifiedContent?.badge = NSNumber(value: 1)
|
|
||||||
self.modifiedContent?.sound = .default
|
|
||||||
self.modifiedContent?.userInfo[JmapConstants.EMAIL_ID] = emails.first?.id ?? ""
|
|
||||||
return self.notify()
|
return self.notify()
|
||||||
|
} else {
|
||||||
|
self.keychainController.updateEmailDeliveryStateToKeychain(
|
||||||
|
accountId: keychainSharingSession.accountId,
|
||||||
|
newEmailDeliveryState: newEmailDeliveryState
|
||||||
|
)
|
||||||
|
|
||||||
|
if (emails.count > 1) {
|
||||||
|
for email in emails {
|
||||||
|
if (email.id == emails.last?.id) {
|
||||||
|
self.showModifiedNotification(title: email.getSenderName(),
|
||||||
|
subtitle: email.subject,
|
||||||
|
body: email.preview,
|
||||||
|
badgeCount: emails.count,
|
||||||
|
userInfo: [JmapConstants.EMAIL_ID : email.id])
|
||||||
|
return self.notify()
|
||||||
|
}
|
||||||
|
self.showNewNotification(title: email.getSenderName(),
|
||||||
|
subtitle: email.subject,
|
||||||
|
body: email.preview,
|
||||||
|
badgeCount: emails.count,
|
||||||
|
notificationId: email.id,
|
||||||
|
userInfo: [JmapConstants.EMAIL_ID : email.id])
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.showModifiedNotification(title: emails.first!.getSenderName(),
|
||||||
|
subtitle: emails.first!.subject,
|
||||||
|
body: emails.first!.preview,
|
||||||
|
badgeCount: 1,
|
||||||
|
userInfo: [JmapConstants.EMAIL_ID : emails.first!.id])
|
||||||
|
return self.notify()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
|
TwakeLogger.shared.log(message: "JmapClient.shared.getNewEmails: \(error)")
|
||||||
|
self.showDefaultNotification(message: NSLocalizedString(self.newEmailDefaultMessageKey, comment: "Localizable"))
|
||||||
|
return self.notify()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func scheduleLocalNotification(email: Email) {
|
private func showDefaultNotification(message: String) {
|
||||||
|
self.modifiedContent?.title = InfoPlistReader(bundle: .app).bundleDisplayName
|
||||||
|
self.modifiedContent?.body = message
|
||||||
|
self.modifiedContent?.badge = NSNumber(value: 1)
|
||||||
|
self.modifiedContent?.sound = .default
|
||||||
|
}
|
||||||
|
|
||||||
|
private func showModifiedNotification(title: String?,
|
||||||
|
subtitle: String?,
|
||||||
|
body: String?,
|
||||||
|
badgeCount: Int,
|
||||||
|
userInfo: [String: Any]) {
|
||||||
|
self.modifiedContent?.title = title ?? InfoPlistReader(bundle: .app).bundleDisplayName
|
||||||
|
self.modifiedContent?.subtitle = subtitle ?? ""
|
||||||
|
self.modifiedContent?.body = body ?? ""
|
||||||
|
self.modifiedContent?.badge = NSNumber(value: badgeCount)
|
||||||
|
self.modifiedContent?.sound = .default
|
||||||
|
self.modifiedContent?.userInfo = userInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
private func showNewNotification(title: String?,
|
||||||
|
subtitle: String?,
|
||||||
|
body: String?,
|
||||||
|
badgeCount: Int,
|
||||||
|
notificationId: String,
|
||||||
|
userInfo: [String: Any]) {
|
||||||
// Create a notification content
|
// Create a notification content
|
||||||
let content = UNMutableNotificationContent()
|
let content = UNMutableNotificationContent()
|
||||||
content.title = InfoPlistReader(bundle: .app).bundleDisplayName
|
content.title = title ?? InfoPlistReader(bundle: .app).bundleDisplayName
|
||||||
content.subtitle = email.subject ?? ""
|
content.subtitle = subtitle ?? ""
|
||||||
content.body = email.preview ?? ""
|
content.body = body ?? ""
|
||||||
content.sound = .default
|
content.sound = .default
|
||||||
content.badge = 1
|
content.badge = NSNumber(value: badgeCount)
|
||||||
content.userInfo[JmapConstants.EMAIL_ID] = "\(email.id)"
|
content.userInfo = userInfo
|
||||||
|
|
||||||
// Create a notification trigger
|
// Create a notification trigger
|
||||||
let triggerDateTime = Calendar.current.dateComponents([.hour, .minute, .second], from: Date())
|
let triggerDateTime = Calendar.current.dateComponents([.hour, .minute, .second], from: Date())
|
||||||
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDateTime, repeats: false)
|
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDateTime, repeats: false)
|
||||||
// Create a notification request
|
// Create a notification request
|
||||||
let request = UNNotificationRequest(identifier: "\(email.id)", content: content, trigger: trigger)
|
let request = UNNotificationRequest(identifier: notificationId, content: content, trigger: trigger)
|
||||||
|
|
||||||
// Schedule the notification
|
// Schedule the notification
|
||||||
UNUserNotificationCenter.current().add(request) { error in
|
UNUserNotificationCenter.current().add(request) { error in
|
||||||
|
|||||||
Reference in New Issue
Block a user