-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notifications
: Improve notification delivery (#213)
* Add notification extension * Add keychain group
- Loading branch information
Showing
5 changed files
with
258 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
10 changes: 10 additions & 0 deletions
10
ArtemisNotificationExtension/ArtemisNotificationExtension.entitlements
This file contains 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>keychain-access-groups</key> | ||
<array> | ||
<string>$(AppIdentifierPrefix)de.tum.cit.ase.artemis</string> | ||
</array> | ||
</dict> | ||
</plist> |
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>NSExtension</key> | ||
<dict> | ||
<key>NSExtensionPointIdentifier</key> | ||
<string>com.apple.usernotifications.service</string> | ||
<key>NSExtensionPrincipalClass</key> | ||
<string>$(PRODUCT_MODULE_NAME).NotificationService</string> | ||
</dict> | ||
</dict> | ||
</plist> |
This file contains 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// NotificationService.swift | ||
// ArtemisNotificationExtension | ||
// | ||
// Created by Anian Schleyer on 14.11.24. | ||
// Copyright © 2024 TUM. All rights reserved. | ||
// | ||
|
||
import PushNotifications | ||
import UserNotifications | ||
|
||
class NotificationService: UNNotificationServiceExtension { | ||
|
||
private var contentHandler: ((UNNotificationContent) -> Void)? | ||
private var bestAttemptContent: UNMutableNotificationContent? | ||
|
||
override func didReceive(_ request: UNNotificationRequest, | ||
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { | ||
self.contentHandler = contentHandler | ||
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) | ||
|
||
guard var bestAttemptContent else { | ||
contentHandler(request.content) | ||
return | ||
} | ||
|
||
// Decrypt notification and deliver it | ||
let payload = bestAttemptContent.userInfo | ||
guard let payloadString = payload["payload"] as? String, | ||
let initVector = payload["iv"] as? String else { | ||
return | ||
} | ||
|
||
Task { | ||
bestAttemptContent = await PushNotificationHandler | ||
.extractNotification(from: payloadString, iv: initVector) ?? bestAttemptContent | ||
|
||
contentHandler(bestAttemptContent) | ||
} | ||
} | ||
|
||
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, let bestAttemptContent { | ||
contentHandler(bestAttemptContent) | ||
} | ||
} | ||
|
||
} |