home-assistant / iOS

:iphone: Home Assistant for Apple platforms
https://companion.home-assistant.io
Other
1.53k stars 296 forks source link

update_widgets command only works with the app the foreground #2845

Closed literally-anything closed 1 month ago

literally-anything commented 1 month ago

iOS device model, version and app version Model Name: iPhone 13 Software Version: iOS 18 beta 3 App version: 2024.711

Home Assistant Core Version 2024.7.1

Describe the bug The update_widgets notification command only works if the mobile app is in the foreground.

To Reproduce

  1. Add a gauge or details widget on the Lock Screen and configure it to use the state of some entity in a template.
  2. Ensure the app is in the background (on the Home Screen or another app) and pull down the Notification Center.
  3. Send the update_widgets command from another device or automation. The widgets will not update

Expected behavior The widgets should update with the app in the background. This works completely if you open the app in the foreground and pull down Notification Center from there, just not in the background.

Additional context From my pull request that was merged a few days ago: https://github.com/home-assistant/iOS/pull/2830

My best guess as to why this doesn’t work is that I used the main dispatch queue which only seems to run in the ui thread. https://github.com/home-assistant/iOS/blob/2c838a1c70338607998f47e0444aa4637f2fd078/Sources/Shared/Notifications/NotificationCommands/NotificationsCommandManager.swift#L121-L125 I would try testing this, but in my builds I can’t get my own firebase app connected properly.

bgoncal commented 1 month ago

That's curious, it worked for me in background while I was testing, the only thing is that you can't call it infinitely, iOS restricts the number of times you can update and how often too

literally-anything commented 1 month ago

Yeah, I'll test it again when the limit resets.

literally-anything commented 1 month ago

Now the notification just appears as an alert in Notification Center. It’s strange because it only seems to happen sometimes.

bgoncal commented 1 month ago

I checked with Zac and the command also needs to be flagged by firebase push in here: https://github.com/home-assistant/mobile-apps-fcm-push

bgoncal commented 1 month ago

I merged a PR there, now it should be good, at least I don't see the notification anymore consistently.

But the same restrictions from Apple apply https://developer.apple.com/documentation/widgetkit/keeping-a-widget-up-to-date

You can keep an eye on how often it updates with a template like this:

{{ states['sensor.date_time'].state }}

literally-anything commented 1 month ago

The alerts stopped appearing now, but it still won't seem to update without the app in the foreground. I'll try again when the limit resets tomorrow.

bgoncal commented 1 month ago

It's weird, sometimes it works reliably for me and sometimes it stops working for a while, it must be some Apple safe guard to prevent excessive usage of refresh widgets timelines

bgoncal commented 1 month ago

After a few days testing apparently iOS decides when the background update will happen, I also read on Apple forums more people discussing about this and even though the update doesnt happen right away, the OS is notified that "this widget requested updates as soon as allowed".

I'll be closing this issue for now since I don't think it's under our control to force the update to happen always, please let me know in case you believe we should reopen.