Files
iOS/Sources/App/Notifications/NotificationManagerLocalPushInterfaceDirect.swift
Ryan Warner 9779d0576a Fix Live Activity ContentState decoding and Direct push delegate (#4677)
## Summary

Follow-up fixes to two issues surfaced while testing #4671 end-to-end.

1. **`HALiveActivityAttributes.ContentState.countdownEnd` decoded via
Unix epoch.** ActivityKit decodes the `content-state` JSON arriving via
APNs with the default `JSONDecoder`, whose `Date` strategy is
`.deferredToDate` (seconds since the 2001 reference date). HA core sends
`countdown_end` as Unix epoch seconds, matching the documented
`data.when` / `data.when_relative` user contract and the in-app handler
that already does `Date(timeIntervalSince1970:)`. Without a manual
decode the APNs push path renders countdowns ~31 years in the future.
Adds explicit `init(from:)` and `encode(to:)` that map `countdownEnd`
via `timeIntervalSince1970`. All other fields use
`container.decodeIfPresent` so behavior is unchanged for them.

2. **`NotificationManagerLocalPushInterfaceDirect` assigns
`LocalPushManager.delegate`.** The Extension path assigns the delegate
at line 197; the Direct path (used on simulator and Mac Catalyst) never
did. On those platforms that meant silent commands such as
`clear_notification` (no alert title/body) were dropped: iOS doesn't
fire `willPresent` for content-less notifications, and the delegate is
the only fallback that routes into `commandManager`. One-line fix that
brings the Direct factory into line with the Extension's behavior.

Real-device verification (iPhone 13 Mini, iOS 26.5, paid Developer
account): chronometer countdown rendered correctly 60→0 with the Codable
fix in place. Without it the timer rendered as if `Date` were
seconds-since-2001.

## Screenshots

n/a — both fixes affect decode / message routing behavior, not UI
rendering.

## Link to pull request in Documentation repository

Documentation: home-assistant/companion.home-assistant#1303

## Any other notes

Part of the Live Activities effort tracked in home-assistant/epics#61.
Companion server PR: home-assistant/core#166072.
2026-06-01 11:36:33 +02:00

62 lines
2.0 KiB
Swift

import Foundation
import HAKit
import Shared
class NotificationManagerLocalPushInterfaceDirect: NotificationManagerLocalPushInterface {
func status(for server: Server) -> NotificationManagerLocalPushStatus {
.allowed(localPushManagers[server].state)
}
private var localPushManagers: PerServerContainer<LocalPushManager>!
weak var localPushDelegate: LocalPushManagerDelegate?
init(delegate: LocalPushManagerDelegate) {
self.localPushDelegate = delegate
self.localPushManagers = .init { [weak self] server in
let manager = LocalPushManager(server: server)
manager.delegate = self?.localPushDelegate
let token = NotificationCenter.default.addObserver(
forName: LocalPushManager.stateDidChange,
object: manager,
queue: .main,
using: { [weak self] _ in
self?.pushManagerStateDidChange(server: server)
}
)
return .init(manager) { _, _ in
NotificationCenter.default.removeObserver(token)
}
}
}
func addObserver(
for server: Server,
handler: @escaping (NotificationManagerLocalPushStatus) -> Void
) -> HACancellable {
let observer = Observer(identifier: UUID(), server: server, handler: handler)
observers.append(observer)
return HABlockCancellable { [weak self] in
self?.observers.removeAll(where: { $0.identifier == observer.identifier })
}
}
private struct Observer: Equatable {
let identifier: UUID
let server: Server
let handler: (NotificationManagerLocalPushStatus) -> Void
static func == (lhs: Observer, rhs: Observer) -> Bool {
lhs.identifier == rhs.identifier
}
}
private var observers = [Observer]()
private func pushManagerStateDidChange(server: Server) {
for observer in observers where observer.server == server {
observer.handler(status(for: server))
}
}
}