[Notifications] Network photos in notifications

This commit is contained in:
metacryst
2026-06-21 05:00:55 -05:00
parent 8453819f50
commit 21cc89f0af
7 changed files with 173 additions and 33 deletions

View File

@@ -6,6 +6,7 @@
//
import UserNotifications
import Intents
class NotificationService: UNNotificationServiceExtension {
@@ -25,21 +26,91 @@ class NotificationService: UNNotificationServiceExtension {
return
}
URLSession.shared.downloadTask(with: imageUrl) { location, _, error in
defer { contentHandler(content) }
guard let location = location, error == nil else { return }
let userInfo = request.content.userInfo
let tmpFile = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent(imageUrl.lastPathComponent)
// Download the avatar (the sending network's photo), then render the
// notification. A data task lets us hand the bytes straight to INImage.
URLSession.shared.dataTask(with: imageUrl) { data, _, error in
guard let data = data, !data.isEmpty, error == nil else {
contentHandler(content)
return
}
try? FileManager.default.moveItem(at: location, to: tmpFile)
if let attachment = try? UNNotificationAttachment(identifier: "avatar", url: tmpFile) {
content.attachments = [attachment]
// iOS 15+: render as a communication notification — large circular
// avatar with the app icon badged in the corner.
if #available(iOS 15.0, *) {
self.deliverAsCommunicationNotification(content: content, imageData: data, userInfo: userInfo, contentHandler: contentHandler)
} else {
// iOS 14 fallback: attach the image as a plain thumbnail.
let tmpFile = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("avatar.png")
try? data.write(to: tmpFile)
if let attachment = try? UNNotificationAttachment(identifier: "avatar", url: tmpFile) {
content.attachments = [attachment]
}
contentHandler(content)
}
}.resume()
}
@available(iOS 15.0, *)
private func deliverAsCommunicationNotification(
content: UNMutableNotificationContent,
imageData: Data,
userInfo: [AnyHashable: Any],
contentHandler: @escaping (UNNotificationContent) -> Void
) {
// The conversation title shown in bold. Defaults to the notification's own
// title so existing copy (e.g. "Group | Alice") is preserved.
let senderName = (userInfo["senderName"] as? String) ?? content.title
let isGroup = (userInfo["isGroup"] as? Bool) ?? false
// Group related notifications into one conversation so iOS stacks them.
let conversationId = (userInfo["conversationId"] as? String)
?? (userInfo["threadId"] as? String)
?? content.threadIdentifier
let avatar = INImage(imageData: imageData)
let handle = INPersonHandle(value: conversationId, type: .unknown)
let sender = INPerson(
personHandle: handle,
nameComponents: nil,
displayName: senderName,
image: avatar,
contactIdentifier: nil,
customIdentifier: conversationId
)
let intent = INSendMessageIntent(
recipients: nil,
outgoingMessageType: .outgoingMessageText,
content: content.body,
speakableGroupName: isGroup ? INSpeakableString(spokenPhrase: senderName) : nil,
conversationIdentifier: conversationId,
serviceName: nil,
sender: sender,
attachments: nil
)
// For a group conversation the avatar comes from the group image; for a
// 1:1 it comes from the sender's INPerson image (set above).
if isGroup {
intent.setImage(avatar, forParameterNamed: \.speakableGroupName)
}
let interaction = INInteraction(intent: intent, response: nil)
interaction.direction = .incoming
interaction.donate(completion: nil)
do {
let updated = try content.updating(from: intent)
contentHandler(updated)
} catch {
// If the intent can't be applied, fall back to the unmodified content
// so the notification still shows.
contentHandler(content)
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let content = bestAttemptContent {
contentHandler(content)