Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 18, 2025

Summary

Local push connections sometimes fail to recover automatically, requiring users to manually toggle the setting. This adds automatic reconnection when the app returns to foreground.

Changes:

  • LocalPushManager: Added forceReconnect() method that cancels and recreates subscriptions, bypassing the webhookID change check
  • NotificationManagerLocalPushInterface: Added reconnectAll() protocol method with platform-specific implementations:
    • macOS: Directly calls forceReconnect() on each enabled server's manager
    • iOS: Triggers manager reload to restart the PushProvider extension
  • LifecycleManager: Calls reconnectAll() asynchronously on foreground transition using utility queue to avoid blocking UI
  • Added unit test testForceReconnect() verifying subscription cancellation and recreation
// Force reconnection without webhookID change
public func forceReconnect() {
    subscription?.cancel()
    subscription = nil
    updateSubscription(force: true)
}

Screenshots

N/A - Internal functionality change with no UI impact

Link to pull request in Documentation repository

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

Any other notes

Reconnection is queued on background thread to prevent UI blocking during foreground transitions with multiple servers.

Original prompt

Inside Companion App Settings > Server name > internal URL, we have a "local push" toggle. Sometimes local push disconnects and only reconnects if user disable this toggle, save then reenable and save again. Lets jmprove this and force reconnect when app enters foregeound and the toggle is on


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Improve reconnect behavior for local push toggle Auto-reconnect local push on app foreground Dec 18, 2025
Copilot AI requested a review from bgoncal December 18, 2025 01:27
@bgoncal bgoncal marked this pull request as ready for review December 18, 2025 01:41
Copilot AI review requested due to automatic review settings December 18, 2025 01:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds automatic reconnection for local push connections when the app returns to foreground, addressing an issue where connections sometimes fail to recover automatically and require manual toggling of the local push setting.

Key changes:

  • Added forceReconnect() method to LocalPushManager that bypasses webhookID change detection and forces subscription recreation
  • Implemented reconnectAll() protocol method across all NotificationManagerLocalPushInterface implementations with platform-specific behavior
  • Integrated automatic reconnection into the app lifecycle by calling reconnectAll() asynchronously when entering foreground

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
Sources/Shared/Notifications/LocalPush/LocalPushManager.swift Added forceReconnect() method and modified updateSubscription() to accept a force parameter that bypasses webhookID change checks
Sources/App/Notifications/NotificationManagerLocalPushInterface.swift Added reconnectAll() protocol method to support automatic reconnection
Sources/App/Notifications/NotificationManagerLocalPushInterfaceDirect.swift Implemented reconnectAll() for macOS by directly calling forceReconnect() on each enabled server's manager
Sources/App/Notifications/NotificationManagerLocalPushInterfaceExtension.swift Implemented reconnectAll() for iOS by triggering manager reload to restart the PushProvider extension
Sources/App/Notifications/NotificationManagerLocalPushInterfaceUnsupported.swift Added no-op reconnectAll() implementation for unsupported platforms
Sources/App/LifecycleManager.swift Integrated automatic reconnection by calling reconnectAll() asynchronously on utility queue during foreground transitions
Tests/Shared/LocalPushManager.test.swift Added comprehensive unit test verifying forced reconnection behavior without webhookID changes

Comment on lines +67 to +72
func reconnectAll() {
Current.Log.info("Triggering reconnection for all local push managers")
// For iOS, we reload managers which will ensure they're properly configured
// and trigger the PushProvider extension to reconnect if needed
reloadManagersAfterSave()
}
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reloadManagersAfterSave method performs asynchronous NEAppPushManager operations. If reconnectAll is called multiple times rapidly (e.g., if the app enters foreground repeatedly in quick succession), multiple concurrent calls to NEAppPushManager.loadAllFromPreferences could occur, potentially leading to race conditions when updating the managers dictionary and tokens array. Consider debouncing this call or using a serial queue to ensure operations complete before new ones begin.

Copilot uses AI. Check for mistakes.
@bgoncal bgoncal enabled auto-merge (squash) December 18, 2025 02:45
@bgoncal bgoncal closed this Dec 18, 2025
auto-merge was automatically disabled December 18, 2025 15:17

Pull request was closed

@bgoncal bgoncal deleted the copilot/improve-local-push-reconnect branch December 18, 2025 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants