This commit adds support for the unfurling of author profile names on remote push notifications It also makes the following changes: - Notification extension now uses NdbNote - Some of the logic between push notifications and local notifications was unified Testing ------- Device: iPhone 15 Pro simulator iOS: 17.0.1 Damus: This commit Coverage: 1. Basic smoke tests on the app by browsing different notes and different tabs 2. Sent test push notifications for mentions and DMs to check the unfurling of profile names 3. Ran unit tests Closes: https://github.com/damus-io/damus/issues/1703 Changelog-Added: Unfurl profile name on remote push notifications Signed-off-by: Daniel D’Aquino <daniel@daquino.me> Signed-off-by: William Casarin <jb55@jb55.com>
48 lines
1.8 KiB
Swift
48 lines
1.8 KiB
Swift
//
|
||
// NotificationService.swift
|
||
// DamusNotificationService
|
||
//
|
||
// Created by Daniel D’Aquino on 2023-11-10.
|
||
//
|
||
|
||
import UserNotifications
|
||
import Foundation
|
||
|
||
class NotificationService: UNNotificationServiceExtension {
|
||
|
||
var contentHandler: ((UNNotificationContent) -> Void)?
|
||
var bestAttemptContent: UNMutableNotificationContent?
|
||
|
||
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
|
||
self.contentHandler = contentHandler
|
||
|
||
let ndb: Ndb? = try? Ndb(owns_db_file: false)
|
||
|
||
// Modify the notification content here...
|
||
guard let nostrEventJSON = request.content.userInfo["nostr_event"] as? String,
|
||
let nostrEvent = NdbNote.owned_from_json(json: nostrEventJSON)
|
||
else {
|
||
contentHandler(request.content)
|
||
return;
|
||
}
|
||
|
||
// Log that we got a push notification
|
||
if let txn = ndb?.lookup_profile(nostrEvent.pubkey) {
|
||
Log.debug("Got push notification from %s (%s)", for: .push_notifications, (txn.unsafeUnownedValue?.profile?.display_name ?? "Unknown"), nostrEvent.pubkey.hex())
|
||
}
|
||
|
||
if let improvedContent = NotificationFormatter.shared.format_message(event: nostrEvent, ndb: ndb) {
|
||
contentHandler(improvedContent)
|
||
}
|
||
}
|
||
|
||
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)
|
||
}
|
||
}
|
||
|
||
}
|