-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Rerequest push notifications #4174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mpivchev
wants to merge
3
commits into
master
Choose a base branch
from
push-notif-request
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+85
−41
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,12 +21,14 @@ | |
|
|
||
| import UIKit | ||
| import UserNotifications | ||
| import os | ||
| import NextcloudKit | ||
|
|
||
| class NotificationService: UNNotificationServiceExtension { | ||
| var contentHandler: ((UNNotificationContent) -> Void)? | ||
| var bestAttemptContent: UNMutableNotificationContent? | ||
| var request: UNNotificationRequest? | ||
| private let deliveryLock = OSAllocatedUnfairLock() | ||
|
|
||
| override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { | ||
| self.contentHandler = contentHandler | ||
|
|
@@ -35,59 +37,101 @@ class NotificationService: UNNotificationServiceExtension { | |
|
|
||
| NextcloudKit.configureLogger(logLevel: .verbose) | ||
|
|
||
| if let bestAttemptContent = bestAttemptContent { | ||
| bestAttemptContent.title = "" | ||
| bestAttemptContent.body = "Nextcloud notification" | ||
| do { | ||
| if let message = bestAttemptContent.userInfo["subject"] as? String { | ||
| for tableAccount in NCManageDatabase.shared.getAllTableAccount() { | ||
| guard let privateKey = NCPreferences().getPushNotificationPrivateKey(account: tableAccount.account) else { | ||
| bestAttemptContent.body = "Error retrieving private key for \(tableAccount.account)" | ||
| continue | ||
| } | ||
| guard let bestAttemptContent else { return } | ||
|
|
||
| let prefixData = Data(privateKey.prefix(8)) | ||
| let prefixBase64 = prefixData.base64EncodedString() | ||
| nkLog(debug: "🔑 Loaded private key for \(tableAccount.account): prefix(Base64)=\(prefixBase64)") | ||
| bestAttemptContent.title = "" | ||
| bestAttemptContent.body = "Nextcloud notification" | ||
|
|
||
| guard let decryptedMessage = NCPushNotificationEncryption.shared().decryptPushNotification(message, withDevicePrivateKey: privateKey) else { | ||
| bestAttemptContent.body = "Error decryption for \(tableAccount.account)" | ||
| continue | ||
| } | ||
| guard let data = decryptedMessage.data(using: .utf8) else { | ||
| bestAttemptContent.body = "Error decryption data utf8 for \(tableAccount.account)" | ||
| continue | ||
| } | ||
| var matchedAccount: tableAccount? | ||
| var payload: [String: AnyObject]? | ||
|
|
||
| do { | ||
| if let message = bestAttemptContent.userInfo["subject"] as? String { | ||
| for tableAccount in NCManageDatabase.shared.getAllTableAccount() { | ||
| guard let privateKey = NCPreferences().getPushNotificationPrivateKey(account: tableAccount.account) else { | ||
| bestAttemptContent.body = "Error retrieving private key for \(tableAccount.account)" | ||
| continue | ||
| } | ||
|
|
||
| let prefixData = Data(privateKey.prefix(8)) | ||
| let prefixBase64 = prefixData.base64EncodedString() | ||
| nkLog(debug: "🔑 Loaded private key for \(tableAccount.account): prefix(Base64)=\(prefixBase64)") | ||
|
|
||
| if var json = try JSONSerialization.jsonObject(with: data) as? [String: AnyObject], | ||
| let subject = json["subject"] as? String { | ||
| bestAttemptContent.body = subject | ||
| if let pref = UserDefaults(suiteName: NCBrandOptions.shared.capabilitiesGroup) { | ||
| json["account"] = tableAccount.account as AnyObject | ||
| pref.set(json, forKey: "NOTIFICATION_DATA") | ||
| pref.synchronize() | ||
| } | ||
| } else { | ||
| bestAttemptContent.body = "Error JSON Serialization for \(tableAccount.account)" | ||
| guard let decryptedMessage = NCPushNotificationEncryption.shared().decryptPushNotification(message, withDevicePrivateKey: privateKey) else { | ||
| bestAttemptContent.body = "Error decrypting notification for \(tableAccount.account)" | ||
| continue | ||
| } | ||
| guard let data = decryptedMessage.data(using: .utf8) else { | ||
| bestAttemptContent.body = "Error decrypting UTF8 notification data for \(tableAccount.account)" | ||
| continue | ||
| } | ||
|
|
||
| if var json = try JSONSerialization.jsonObject(with: data) as? [String: AnyObject], | ||
| let subject = json["subject"] as? String { | ||
| bestAttemptContent.body = subject | ||
| if let pref = UserDefaults(suiteName: NCBrandOptions.shared.capabilitiesGroup) { | ||
| json["account"] = tableAccount.account as AnyObject | ||
| pref.set(json, forKey: "NOTIFICATION_DATA") | ||
| pref.synchronize() | ||
| } | ||
| break | ||
|
|
||
| matchedAccount = tableAccount | ||
| payload = json | ||
| } else { | ||
| bestAttemptContent.body = "Error with notification JSON for \(tableAccount.account)" | ||
| } | ||
| break | ||
|
mpivchev marked this conversation as resolved.
|
||
| } | ||
|
mpivchev marked this conversation as resolved.
|
||
| } catch let error as NSError { | ||
| nkLog(error: "Failed : \(error.localizedDescription)") | ||
| } | ||
| } catch let error as NSError { | ||
| nkLog(error: "Failed : \(error.localizedDescription)") | ||
| } | ||
|
|
||
| contentHandler(bestAttemptContent) | ||
| guard let tblAccount = matchedAccount, | ||
| let nid = payload?["nid"] as? Int, | ||
| payload?["delete"] as? Bool != true, | ||
| payload?["delete-all"] as? Bool != true else { | ||
| deliver() | ||
| return | ||
| } | ||
|
|
||
| // NextcloudKit Session | ||
| NextcloudKit.shared.setup(groupIdentifier: NCBrandOptions.shared.capabilitiesGroup, delegate: NCNetworking.shared) | ||
| NextcloudKit.shared.appendSession(account: tblAccount.account, | ||
| urlBase: tblAccount.urlBase, | ||
| user: tblAccount.user, | ||
| userId: tblAccount.userId, | ||
| password: NCPreferences().getPassword(account: tblAccount.account), | ||
| userAgent: userAgent, | ||
| groupIdentifier: NCBrandOptions.shared.capabilitiesGroup) | ||
|
|
||
| Task { | ||
| let results = await NextcloudKit.shared.getNotificationsAsync(idNotification: nid, | ||
| account: tblAccount.account, | ||
| options: NKRequestOptions(timeout: 20)) | ||
|
|
||
| if results.error == .success, let notification = results.notifications?.first { | ||
| bestAttemptContent.title = notification.message.isEmpty ? NCBrandOptions.shared.brand : notification.subject | ||
| bestAttemptContent.body = notification.message.isEmpty ? notification.subject : notification.message | ||
| } | ||
|
mpivchev marked this conversation as resolved.
|
||
|
|
||
| deliver() | ||
| } | ||
| } | ||
|
|
||
| override func serviceExtensionTimeWillExpire() { | ||
| // 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. | ||
| if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { | ||
| bestAttemptContent.title = "" | ||
| bestAttemptContent.body = "Nextcloud Notification Time Will Expire" | ||
| contentHandler(bestAttemptContent) | ||
| deliver() | ||
| } | ||
|
|
||
| private func deliver() { | ||
| let handler = deliveryLock.withLock { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lock is added as |
||
| let handler = contentHandler | ||
| contentHandler = nil | ||
| return handler | ||
| } | ||
|
|
||
| if let handler, let bestAttemptContent { | ||
| handler(bestAttemptContent) | ||
| } | ||
| } | ||
| } | ||
|
mpivchev marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.