1
0
mirror of git://jb55.com/damus synced 2024-09-16 02:03:45 +00:00
damus/DamusNotificationService/NostrEventInfoFromPushNotification.swift
Daniel D’Aquino ad75d8546c 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>
2023-11-20 10:32:33 -08:00

50 lines
1.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// NostrEventInfoFromPushNotification.swift
// DamusNotificationService
//
// Created by Daniel DAquino on 2023-11-13.
//
import Foundation
/// The representation of a JSON-encoded Nostr Event used by the push notification server
/// Needs to match with https://gitlab.com/soapbox-pub/strfry-policies/-/raw/433459d8084d1f2d6500fdf916f22caa3b4d7be5/src/types.ts
struct NostrEventInfoFromPushNotification: Codable {
let id: String // Hex-encoded
let sig: String // Hex-encoded
let kind: NostrKind
let tags: [[String]]
let pubkey: String // Hex-encoded
let content: String
let created_at: Int
static func from(dictionary: [AnyHashable: Any]) -> NostrEventInfoFromPushNotification? {
guard let id = dictionary["id"] as? String,
let sig = dictionary["sig"] as? String,
let kind_int = dictionary["kind"] as? UInt32,
let kind = NostrKind(rawValue: kind_int),
let tags = dictionary["tags"] as? [[String]],
let pubkey = dictionary["pubkey"] as? String,
let content = dictionary["content"] as? String,
let created_at = dictionary["created_at"] as? Int else {
return nil
}
return NostrEventInfoFromPushNotification(id: id, sig: sig, kind: kind, tags: tags, pubkey: pubkey, content: content, created_at: created_at)
}
func reactionEmoji() -> String? {
guard self.kind == NostrKind.like else {
return nil
}
switch self.content {
case "", "+":
return "❤️"
case "-":
return "👎"
default:
return self.content
}
}
}