mirror of
git://jb55.com/damus
synced 2024-09-20 20:16:58 +00:00
ad75d8546c
I added support for the experimental push notifications feature. There are many improvements to be made, so this feature is currently opt-in only. If the user does not opt-in, their device tokens will not be sent out and thus they will receive no push notifications. We should perform more testing on real-life staging environments before fully releasing this feature. Testing ------- Testing was done gradually during development. Device: iOS simulators iOS: 17 Damus version: A few different but recent prototypes Rough coverage: 1. Checked that no device tokens are sent out when setting is off 2. Checked that I can successfully receive device tokens when feature is ON and set to localhost. 3. Checked sending test push notifications of types "note" (kind: 1), reaction (kind: 7) and DMs (kind 4) works and shows a generic but reasonable push notification message 4. Checked that clicking on the notifications above take the user to the correct screen Closes: https://github.com/damus-io/damus/issues/67 Changelog-Added: Add experimental push notification support Signed-off-by: Daniel D’Aquino <daniel@daquino.me> Signed-off-by: William Casarin <jb55@jb55.com>
49 lines
2.6 KiB
Swift
49 lines
2.6 KiB
Swift
//
|
||
// NotificationFormatter.swift
|
||
// DamusNotificationService
|
||
//
|
||
// Created by Daniel D’Aquino on 2023-11-13.
|
||
//
|
||
|
||
import Foundation
|
||
import UserNotifications
|
||
|
||
struct NotificationFormatter {
|
||
static var shared = NotificationFormatter()
|
||
|
||
// TODO: These is a very generic notification formatter. Once we integrate NostrDB into the extension, we should reuse various functions present in `HomeModel.swift`
|
||
func formatMessage(event: NostrEventInfoFromPushNotification) -> UNNotificationContent? {
|
||
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`
|
||
let event_json_string = String(data: event_json_data, encoding: .utf8) {
|
||
content.userInfo = [
|
||
"nostr_event_info": event_json_string
|
||
]
|
||
}
|
||
switch event.kind {
|
||
case .text:
|
||
content.title = NSLocalizedString("Someone posted a note", comment: "Title label for push notification where someone posted a note")
|
||
content.body = event.content
|
||
break
|
||
case .dm:
|
||
content.title = NSLocalizedString("New message", comment: "Title label for push notifications where a direct message was sent to the user")
|
||
content.body = NSLocalizedString("(Contents are encrypted)", comment: "Label on push notification indicating that the contents of the message are encrypted")
|
||
break
|
||
case .like:
|
||
guard let reactionEmoji = event.reactionEmoji() else {
|
||
content.title = NSLocalizedString("Someone reacted to your note", comment: "Generic title label for push notifications where someone reacted to the user's post")
|
||
break
|
||
}
|
||
content.title = NSLocalizedString("New note reaction", comment: "Title label for push notifications where someone reacted to the user's post with a specific emoji")
|
||
content.body = String(format: NSLocalizedString("Someone reacted to your note with %@", comment: "Body label for push notifications where someone reacted to the user's post with a specific emoji"), reactionEmoji)
|
||
break
|
||
case .zap:
|
||
content.title = NSLocalizedString("Someone zapped you ⚡️", comment: "Title label for a push notification where someone zapped the user")
|
||
break
|
||
default:
|
||
return nil
|
||
}
|
||
return content
|
||
}
|
||
}
|