Reuse local notification logic with push notifications

Testing
-------

Conditional pass

Device: iPhone 15 Pro simulator
iOS: 17.0.1
Damus: This commit
Coverage:

1. Mention notification works (local and push). PASS

2. Thread replies do not appear (but upon code inspection it seems like
   it was not supported before). PASS?

3. DM notification works with decryption (local and push). PASS

4. Zaps not yet implemented. Coming later.

Closes: https://github.com/damus-io/damus/issues/1702
Closes: https://github.com/damus-io/damus/issues/1703
Changelog-Changed: Improve push notification support to match local notification support
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
Daniel D’Aquino
2023-12-01 21:26:21 +00:00
committed by William Casarin
parent 5db22ae244
commit c4f0e833ff
8 changed files with 134 additions and 98 deletions

View File

@@ -13,46 +13,6 @@ struct NotificationFormatter {
// MARK: - Formatting with NdbNote
// TODO: Prepare a `LocalNotification` object from `NdbNote` to reuse Notification formatting code from Local notifications
func format_message(event: NdbNote, ndb: Ndb?) -> UNMutableNotificationContent? {
guard let txn = ndb?.lookup_profile(event.pubkey),
let display_name = txn.unsafeUnownedValue?.profile?.display_name
else {
return self.format_message(event: event)
}
return self.format_message(event: event, display_name: display_name)
}
func format_message(event: NdbNote, display_name: String) -> UNMutableNotificationContent? {
guard let best_attempt_content: UNMutableNotificationContent = self.format_message(event: event) else { return nil }
switch event.known_kind {
case .text:
best_attempt_content.title = String(format: NSLocalizedString("%@ posted a note", comment: "Title label for push notification where a user posted a note"), display_name)
break
case .dm:
best_attempt_content.title = String(format: NSLocalizedString("New message from %@", comment: "Title label for push notifications where a direct message was sent to the user"), display_name)
break
case .like:
guard let reaction_emoji = to_reaction_emoji(ev: event) else {
best_attempt_content.title = String(format: NSLocalizedString("%@ reacted to your note", comment: "Reaction heading in local/push notification"), display_name)
best_attempt_content.body = ""
break
}
best_attempt_content.title = String(format: NSLocalizedString("%@ reacted with %@", comment: "Reacted by heading in local notification"), display_name, reaction_emoji)
best_attempt_content.body = ""
break
case .zap:
best_attempt_content.title = String(format: NSLocalizedString("%@ zapped you ⚡️", comment: "Title label for a push notification where someone zapped the user"), display_name)
break
default:
return nil
}
return best_attempt_content
}
func format_message(event: NdbNote) -> UNMutableNotificationContent? {
let content = UNMutableNotificationContent()
if let event_json_data = try? JSONEncoder().encode(event), // Must be encoded, as the notification completion handler requires this object to conform to `NSSecureCoding`