Add notification mode setting
This allows the user to switch between local and push notifications Signed-off-by: Daniel D’Aquino <daniel@daquino.me> Reviewed-by: William Casarin <jb55@jb55.com>
This commit is contained in:
@@ -53,7 +53,7 @@ class NotificationService: UNNotificationServiceExtension {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
guard should_display_notification(state: state, event: nostr_event) else {
|
guard should_display_notification(state: state, event: nostr_event, mode: .push) else {
|
||||||
// We should not display notification for this event. Suppress notification.
|
// We should not display notification for this event. Suppress notification.
|
||||||
// contentHandler(UNNotificationContent())
|
// contentHandler(UNNotificationContent())
|
||||||
// TODO: We cannot really suppress until we have the notification supression entitlement. Show the raw notification
|
// TODO: We cannot really suppress until we have the notification supression entitlement. Show the raw notification
|
||||||
|
|||||||
@@ -733,7 +733,7 @@ class HomeModel: ContactsDelegate {
|
|||||||
func got_new_dm(notifs: NewEventsBits, ev: NostrEvent) {
|
func got_new_dm(notifs: NewEventsBits, ev: NostrEvent) {
|
||||||
notification_status.new_events = notifs
|
notification_status.new_events = notifs
|
||||||
|
|
||||||
guard should_display_notification(state: damus_state, event: ev),
|
guard should_display_notification(state: damus_state, event: ev, mode: .local),
|
||||||
let notification_object = generate_local_notification_object(from: ev, state: damus_state)
|
let notification_object = generate_local_notification_object(from: ev, state: damus_state)
|
||||||
else {
|
else {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import UIKit
|
|||||||
let EVENT_MAX_AGE_FOR_NOTIFICATION: TimeInterval = 12 * 60 * 60
|
let EVENT_MAX_AGE_FOR_NOTIFICATION: TimeInterval = 12 * 60 * 60
|
||||||
|
|
||||||
func process_local_notification(state: HeadlessDamusState, event ev: NostrEvent) {
|
func process_local_notification(state: HeadlessDamusState, event ev: NostrEvent) {
|
||||||
guard should_display_notification(state: state, event: ev) else {
|
guard should_display_notification(state: state, event: ev, mode: .local) else {
|
||||||
// We should not display notification. Exit.
|
// We should not display notification. Exit.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -25,7 +25,12 @@ func process_local_notification(state: HeadlessDamusState, event ev: NostrEvent)
|
|||||||
create_local_notification(profiles: state.profiles, notify: local_notification)
|
create_local_notification(profiles: state.profiles, notify: local_notification)
|
||||||
}
|
}
|
||||||
|
|
||||||
func should_display_notification(state: HeadlessDamusState, event ev: NostrEvent) -> Bool {
|
func should_display_notification(state: HeadlessDamusState, event ev: NostrEvent, mode: UserSettingsStore.NotificationsMode) -> Bool {
|
||||||
|
// Do not show notification if it's coming from a mode different from the one selected by our user
|
||||||
|
guard state.settings.notifications_mode == mode else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
if ev.known_kind == nil {
|
if ev.known_kind == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,6 +155,9 @@ class UserSettingsStore: ObservableObject {
|
|||||||
@Setting(key: "like_notification", default_value: true)
|
@Setting(key: "like_notification", default_value: true)
|
||||||
var like_notification: Bool
|
var like_notification: Bool
|
||||||
|
|
||||||
|
@StringSetting(key: "notifications_mode", default_value: .local)
|
||||||
|
var notifications_mode: NotificationsMode
|
||||||
|
|
||||||
@Setting(key: "notification_only_from_following", default_value: false)
|
@Setting(key: "notification_only_from_following", default_value: false)
|
||||||
var notification_only_from_following: Bool
|
var notification_only_from_following: Bool
|
||||||
|
|
||||||
@@ -326,6 +329,36 @@ class UserSettingsStore: ObservableObject {
|
|||||||
@Setting(key: "latest_contact_event_id", default_value: nil)
|
@Setting(key: "latest_contact_event_id", default_value: nil)
|
||||||
var latest_contact_event_id_hex: String?
|
var latest_contact_event_id_hex: String?
|
||||||
|
|
||||||
|
|
||||||
|
// MARK: Helper types
|
||||||
|
|
||||||
|
enum NotificationsMode: String, CaseIterable, Identifiable, StringCodable, Equatable {
|
||||||
|
var id: String { self.rawValue }
|
||||||
|
|
||||||
|
func to_string() -> String {
|
||||||
|
return rawValue
|
||||||
|
}
|
||||||
|
|
||||||
|
init?(from string: String) {
|
||||||
|
guard let notifications_mode = NotificationsMode(rawValue: string) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
self = notifications_mode
|
||||||
|
}
|
||||||
|
|
||||||
|
func text_description() -> String {
|
||||||
|
switch self {
|
||||||
|
case .local:
|
||||||
|
NSLocalizedString("Local", comment: "Option for notification mode setting: Local notification mode")
|
||||||
|
case .push:
|
||||||
|
NSLocalizedString("Push", comment: "Option for notification mode setting: Push notification mode")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case local
|
||||||
|
case push
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func pk_setting_key(_ pubkey: Pubkey, key: String) -> String {
|
func pk_setting_key(_ pubkey: Pubkey, key: String) -> String {
|
||||||
|
|||||||
@@ -26,6 +26,17 @@ struct NotificationSettingsView: View {
|
|||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
Form {
|
Form {
|
||||||
|
if settings.enable_experimental_push_notifications {
|
||||||
|
Picker(NSLocalizedString("Notifications mode", comment: "Prompt selection of the notification mode (Feature to switch between local notifications (generated from user's own phone) or push notifications (generated by Damus server)."),
|
||||||
|
selection: Binding($settings.notifications_mode)
|
||||||
|
) {
|
||||||
|
ForEach(UserSettingsStore.NotificationsMode.allCases, id: \.self) { notification_mode in
|
||||||
|
Text(notification_mode.text_description())
|
||||||
|
.tag(notification_mode.rawValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Section(header: Text("Local Notifications", comment: "Section header for damus local notifications user configuration")) {
|
Section(header: Text("Local Notifications", comment: "Section header for damus local notifications user configuration")) {
|
||||||
Toggle(NSLocalizedString("Zaps", comment: "Setting to enable Zap Local Notification"), isOn: $settings.zap_notification)
|
Toggle(NSLocalizedString("Zaps", comment: "Setting to enable Zap Local Notification"), isOn: $settings.zap_notification)
|
||||||
.toggleStyle(.switch)
|
.toggleStyle(.switch)
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
|
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
|
||||||
// Return if this feature is disabled
|
// Return if this feature is disabled
|
||||||
guard let settings = self.settings else { return }
|
guard let settings = self.settings else { return }
|
||||||
if !settings.enable_experimental_push_notifications {
|
if !settings.enable_experimental_push_notifications || settings.notifications_mode == .local {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user