2024-07-20 08:12:26 +00:00
|
|
|
//
|
|
|
|
// NotificationService.swift
|
|
|
|
// SolianNotifyExt
|
|
|
|
//
|
|
|
|
// Created by LittleSheep on 2024/7/19.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UserNotifications
|
|
|
|
import Intents
|
|
|
|
|
2024-07-21 15:43:18 +00:00
|
|
|
enum ParseNotificationPayloadError: Error {
|
|
|
|
case noMetadata(String)
|
|
|
|
case noAvatarUrl(String)
|
|
|
|
}
|
|
|
|
|
2024-07-20 08:12:26 +00:00
|
|
|
class NotificationService: UNNotificationServiceExtension {
|
|
|
|
|
|
|
|
var contentHandler: ((UNNotificationContent) -> Void)?
|
|
|
|
var bestAttemptContent: UNMutableNotificationContent?
|
|
|
|
|
2024-07-21 15:43:18 +00:00
|
|
|
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
|
2024-07-20 08:12:26 +00:00
|
|
|
self.contentHandler = contentHandler
|
|
|
|
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
|
|
|
|
|
|
|
|
if let bestAttemptContent = bestAttemptContent {
|
2024-07-21 15:43:18 +00:00
|
|
|
do {
|
|
|
|
switch bestAttemptContent.categoryIdentifier {
|
|
|
|
case "messaging.message", "messaging.callStart":
|
|
|
|
guard let metadata = bestAttemptContent.userInfo["metadata"] as? [AnyHashable : Any] else {
|
|
|
|
throw ParseNotificationPayloadError.noMetadata("The notification has no metadata.")
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let avatarUrl = bestAttemptContent.userInfo["avatar"] as? String else {
|
|
|
|
throw ParseNotificationPayloadError.noMetadata("The notification has no avatar url.")
|
|
|
|
}
|
|
|
|
|
2024-07-23 03:18:06 +00:00
|
|
|
let handle = INPersonHandle(value: String(metadata["user_id"] as! Int), type: .unknown)
|
2024-07-21 15:43:18 +00:00
|
|
|
let avatar = INImage(
|
|
|
|
url: URL(string: avatarUrl)!
|
|
|
|
)!
|
|
|
|
let sender = INPerson(personHandle: handle,
|
|
|
|
nameComponents: nil,
|
2024-07-21 16:04:12 +00:00
|
|
|
displayName: bestAttemptContent.title,
|
2024-07-21 15:43:18 +00:00
|
|
|
image: avatar,
|
|
|
|
contactIdentifier: nil,
|
2024-07-23 03:18:06 +00:00
|
|
|
customIdentifier: nil)
|
2024-07-21 15:43:18 +00:00
|
|
|
let intent = INSendMessageIntent(recipients: nil,
|
|
|
|
outgoingMessageType: .outgoingMessageText,
|
|
|
|
content: bestAttemptContent.body,
|
|
|
|
speakableGroupName: nil,
|
|
|
|
conversationIdentifier: String(metadata["channel_id"] as! Int),
|
2024-07-23 03:18:06 +00:00
|
|
|
serviceName: nil,
|
2024-07-21 15:43:18 +00:00
|
|
|
sender: sender,
|
|
|
|
attachments: nil)
|
2024-07-20 08:12:26 +00:00
|
|
|
|
2024-07-21 15:43:18 +00:00
|
|
|
let interaction = INInteraction(intent: intent, response: nil)
|
|
|
|
interaction.direction = .incoming
|
|
|
|
interaction.donate(completion: nil)
|
2024-07-23 03:18:06 +00:00
|
|
|
|
|
|
|
let updatedContent = try request.content.updating(from: intent)
|
|
|
|
contentHandler(updatedContent)
|
2024-07-21 15:43:18 +00:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
break
|
2024-07-20 08:12:26 +00:00
|
|
|
}
|
2024-07-21 15:43:18 +00:00
|
|
|
} catch {
|
2024-07-20 08:12:26 +00:00
|
|
|
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 = contentHandler, let bestAttemptContent = bestAttemptContent {
|
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|