mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-11-22 00:11:00 +00:00
Use communication notifications
This commit is contained in:
parent
05815e6d35
commit
d73b6952d9
4 changed files with 61 additions and 4 deletions
|
@ -8,6 +8,8 @@
|
|||
<array>
|
||||
<string>app-usage</string>
|
||||
</array>
|
||||
<key>com.apple.developer.usernotifications.communication</key>
|
||||
<true/>
|
||||
<key>com.apple.security.app-sandbox</key>
|
||||
<true/>
|
||||
<key>com.apple.security.application-groups</key>
|
||||
|
|
|
@ -17,6 +17,10 @@
|
|||
</array>
|
||||
<key>ITSAppUsesNonExemptEncryption</key>
|
||||
<false/>
|
||||
<key>NSUserActivityTypes</key>
|
||||
<array>
|
||||
<string>INSendMessageIntent</string>
|
||||
</array>
|
||||
<key>UIBackgroundModes</key>
|
||||
<array>
|
||||
<string>remote-notification</string>
|
||||
|
|
|
@ -4,6 +4,13 @@
|
|||
<dict>
|
||||
<key>NSExtension</key>
|
||||
<dict>
|
||||
<key>NSExtensionAttributes</key>
|
||||
<dict>
|
||||
<key>IntentsSupported</key>
|
||||
<array>
|
||||
<string>INSendMessageIntent</string>
|
||||
</array>
|
||||
</dict>
|
||||
<key>NSExtensionPointIdentifier</key>
|
||||
<string>com.apple.usernotifications.service</string>
|
||||
<key>NSExtensionPrincipalClass</key>
|
||||
|
|
|
@ -5,6 +5,8 @@ import KeychainSwift
|
|||
import Models
|
||||
import UIKit
|
||||
import UserNotifications
|
||||
import Intents
|
||||
import Network
|
||||
|
||||
@MainActor
|
||||
class NotificationService: UNNotificationServiceExtension {
|
||||
|
@ -15,7 +17,7 @@ class NotificationService: UNNotificationServiceExtension {
|
|||
self.contentHandler = contentHandler
|
||||
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
|
||||
|
||||
if let bestAttemptContent {
|
||||
if var bestAttemptContent {
|
||||
let privateKey = PushNotificationsService.shared.notificationsPrivateKeyAsKey
|
||||
let auth = PushNotificationsService.shared.notificationsAuthKeyAsKey
|
||||
|
||||
|
@ -58,6 +60,7 @@ class NotificationService: UNNotificationServiceExtension {
|
|||
bestAttemptContent.userInfo["plaintext"] = plaintextData
|
||||
bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "glass.caf"))
|
||||
|
||||
|
||||
let preferences = UserPreferences.shared
|
||||
preferences.pushNotificationsCount += 1
|
||||
|
||||
|
@ -75,10 +78,18 @@ class NotificationService: UNNotificationServiceExtension {
|
|||
if let (data, _) = try? await URLSession.shared.data(for: .init(url: url)) {
|
||||
if let image = UIImage(data: data) {
|
||||
try? image.pngData()?.write(to: fileURL)
|
||||
|
||||
if let remoteNotification = await toRemoteNotification(localNotification: notification) {
|
||||
let intent = buildMessageIntent(remoteNotification: remoteNotification, avatarURL: fileURL)
|
||||
bestAttemptContent = try bestAttemptContent.updating(from: intent) as! UNMutableNotificationContent
|
||||
let newBody = "\(bestAttemptContent.userInfo["i"] as? String ?? "") \n\(notification.title)\n\(notification.body.escape())"
|
||||
bestAttemptContent.body = newBody
|
||||
} else {
|
||||
if let attachment = try? UNNotificationAttachment(identifier: filename, url: fileURL, options: nil) {
|
||||
bestAttemptContent.attachments = [attachment]
|
||||
}
|
||||
}
|
||||
}
|
||||
contentHandler(bestAttemptContent)
|
||||
} else {
|
||||
contentHandler(bestAttemptContent)
|
||||
|
@ -89,4 +100,37 @@ class NotificationService: UNNotificationServiceExtension {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func toRemoteNotification(localNotification: MastodonPushNotification) async -> Models.Notification? {
|
||||
do {
|
||||
if let account = AppAccountsManager.shared.availableAccounts.first(where: { $0.oauthToken?.accessToken == localNotification.accessToken }) {
|
||||
let client = Client(server: account.server, oauthToken: account.oauthToken)
|
||||
let remoteNotification: Models.Notification = try await client.get(endpoint: Notifications.notification(id: String(localNotification.notificationID)))
|
||||
return remoteNotification
|
||||
}
|
||||
} catch {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
private func buildMessageIntent(remoteNotification: Models.Notification, avatarURL: URL) -> INSendMessageIntent {
|
||||
let handle = INPersonHandle(value: remoteNotification.account.id, type: .unknown)
|
||||
let avatar = INImage(url: avatarURL)
|
||||
let sender = INPerson(personHandle: handle,
|
||||
nameComponents: nil,
|
||||
displayName: remoteNotification.account.safeDisplayName,
|
||||
image: avatar,
|
||||
contactIdentifier: nil,
|
||||
customIdentifier: nil)
|
||||
let intent = INSendMessageIntent(recipients: nil,
|
||||
outgoingMessageType: .outgoingMessageText,
|
||||
content: nil,
|
||||
speakableGroupName: nil,
|
||||
conversationIdentifier: remoteNotification.account.id,
|
||||
serviceName: nil,
|
||||
sender: sender,
|
||||
attachments: nil)
|
||||
return intent
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue