[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

@@ -0,0 +1,53 @@
# Communication Notifications (avatar + app-icon badge)
The Notification Service Extension (`NotificationService.swift`) turns incoming
pushes into **communication notifications** on iOS 15+: a large circular avatar
(the sending network's photo) with the app icon badged in the corner.
## How it works
1. The server (`frm.so`, `notifications.js`) sets `mutableContent: 1` and adds to
the push payload:
- `imageUrl` — a PNG of the network's logo (the server rasterizes SVG logos to
PNG via `sharp`, since iOS can't render SVG avatars).
- `senderName` — the network name (used as the conversation title; falls back
to the notification's own title).
- `isGroup` — `true`, so iOS uses the group-avatar style.
2. The extension downloads `imageUrl`, builds an `INSendMessageIntent` with the
image as the sender/group avatar, donates it, and returns
`content.updating(from: intent)`.
3. On iOS 14 the extension falls back to attaching the image as a plain thumbnail.
## Required setup (both pieces are mandatory)
Two things must be in place or the avatar silently won't render:
### 1. `NSUserActivityTypes` declares the intent (already in this repo)
`INSendMessageIntent` is declared under `NSUserActivityTypes` in **both**
`App/Info.plist` and `NotificationService/Info.plist`. Without this the donation
fails with *"Donating intent 'INSendMessageIntent' is not supported by this
extension"* and the communication styling never applies.
### 2. Communication Notifications capability (one-time, in Xcode — cannot be scripted)
The capability must be registered on the provisioning profile, not just present
in the entitlements file:
1. Open `ios/App/App.xcworkspace` in Xcode.
2. Select the **App** target → **Signing & Capabilities** → **+ Capability** →
add **Communication Notifications**.
- This adds `com.apple.developer.usernotifications.communication` to
`App.entitlements` (already in this repo) and, crucially, enables the
capability on the App ID / provisioning profile. Requires a paid Apple
Developer account.
3. If the avatar still doesn't appear, add the same capability to the
**NotificationService** target as well.
4. Rebuild and reinstall on a device (communication notifications don't reliably
render on the Simulator).
## Notes
- Requires iOS 15.0+ for the communication style; older versions get the plain
attachment fallback automatically.
- The app's deployment target is currently 14.0, so no bump is required.

View File

@@ -2,6 +2,10 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSUserActivityTypes</key>
<array>
<string>INSendMessageIntent</string>
</array>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>

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)