Add experimental push notification support

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>
This commit is contained in:
Daniel D’Aquino
2023-11-14 07:21:39 +00:00
committed by William Casarin
parent 878b1caa95
commit ad75d8546c
13 changed files with 562 additions and 5 deletions

View File

@@ -19,6 +19,9 @@ struct LossyLocalNotification {
}
static func from_user_info(user_info: [AnyHashable: Any]) -> LossyLocalNotification? {
if let encoded_nostr_event_push_data = user_info["nostr_event_info"] as? String {
return self.from(encoded_nostr_event_push_data: encoded_nostr_event_push_data)
}
guard let id = user_info["id"] as? String,
let target_id = MentionRef.from_bech32(str: id) else {
return nil
@@ -28,6 +31,21 @@ struct LossyLocalNotification {
return LossyLocalNotification(type: type, mention: target_id)
}
static func from(encoded_nostr_event_push_data: String) -> LossyLocalNotification? {
guard let json_data = encoded_nostr_event_push_data.data(using: .utf8),
let nostr_event_push_data = try? JSONDecoder().decode(NostrEventInfoFromPushNotification.self, from: json_data) else {
return nil
}
return self.from(nostr_event_push_data: nostr_event_push_data)
}
static func from(nostr_event_push_data: NostrEventInfoFromPushNotification) -> LossyLocalNotification? {
guard let type = LocalNotificationType.from(nostr_kind: nostr_event_push_data.kind) else { return nil }
guard let note_id: NoteId = NoteId.init(hex: nostr_event_push_data.id) else { return nil }
let target: MentionRef = .note(note_id)
return LossyLocalNotification(type: type, mention: target)
}
}
struct LocalNotification {
@@ -48,4 +66,21 @@ enum LocalNotificationType: String {
case repost
case zap
case profile_zap
static func from(nostr_kind: NostrKind) -> Self? {
switch nostr_kind {
case .text:
return .mention
case .dm:
return .dm
case .like:
return .like
case .longform:
return .mention
case .zap:
return .zap
default:
return nil
}
}
}