iOS/Sources/Shared/Notifications/LocalNotificationDispatcher.swift
Bruno Pantaleão Gonçalves cac8f15627
Add debug notifications option (#3674)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->
This PR adds option in the debug section to receive debug notifications,
this is a debugging tool only and it is not visible to the user unless
it open the debugging tool hidden panel.

First use case: Receive notification when location fails to update ein
brackground.

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->
2025-06-25 17:05:43 +02:00

56 lines
1.7 KiB
Swift

import Foundation
import UserNotifications
public protocol LocalNotificationDispatcherProtocol {
func send(_ notification: LocalNotificationDispatcher.Notification)
}
/// Sends local notifications
public final class LocalNotificationDispatcher: LocalNotificationDispatcherProtocol {
public struct Notification {
public let id: NotificationIdentifier
public let title: String
public let body: String?
public let sound: UNNotificationSound?
public init(
id: NotificationIdentifier,
title: String,
body: String? = nil,
sound: UNNotificationSound? = nil
) {
self.id = id
self.title = title
self.body = body
self.sound = sound
}
}
public init() {}
public func send(_ notification: Notification) {
if notification.id == .debug, !Current.settingsStore.receiveDebugNotifications {
// Do not send debug notifications if the setting is disabled
return
}
let content = UNMutableNotificationContent()
content.title = notification.title
if let body = notification.body {
content.body = body
}
content.sound = notification.sound
let request = UNNotificationRequest(
identifier: notification.id.rawValue,
content: content,
trigger: nil
)
UNUserNotificationCenter.current().add(request) { error in
if let error {
Current.Log
.info("Error scheduling notification, id: \(notification.id) error: \(error.localizedDescription)")
}
}
}
}