feat: Add iOS Notification Service Extension for localizable push notifications

This commit is contained in:
Christian Kußowski 2025-08-31 09:29:32 +02:00
commit c3d8fed8a9
No known key found for this signature in database
GPG key ID: E067ECD60F1A0652
7 changed files with 349 additions and 8 deletions

View file

@ -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>

View file

@ -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>com.apple.security.application-groups</key>
<array>
<string>group.im.fluffychat.app</string>
</array>
</dict>
</plist>

View file

@ -0,0 +1,63 @@
//
// NotificationService.swift
// Notification Extension
//
// Created by Christian Pauly on 26.08.25.
//
import UserNotifications
import os
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Uncomment to read the push message payload:
// os_log("[FluffyChatPushHelper] New message received: %{public}@", log: .default, type: .error, bestAttemptContent.userInfo)
os_log("[FluffyChatPushHelper] New message received")
guard let roomId = bestAttemptContent.userInfo["room_id"] as? String,
let eventId = bestAttemptContent.userInfo["event_id"] as? String else {
os_log("[FluffyChatPushHelper] Room ID or Event ID is missing!")
let emptyContent = UNMutableNotificationContent()
contentHandler(emptyContent)
return
}
bestAttemptContent.threadIdentifier = roomId
if
let jsonString = bestAttemptContent.userInfo["counts"] as? String,
let jsonData = jsonString.data(using: .utf8),
let jsonMap = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any],
let unread = jsonMap["unread"] as? Int {
bestAttemptContent.title = String(
localized: "\(unread) unread messages",
comment: "Default notification title"
)
bestAttemptContent.badge = NSNumber(integerLiteral: unread)
}
// TODO: Download and decrypt event to display a better body:
bestAttemptContent.body = String(
localized: "New message - open app to read",
comment: "Default notification body"
)
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)
}
}
}