iosswiftswiftuiwidgetliveactivity

How to continuously update iOS LiveActivity without push notifications?


I'm trying to expand my app with a feature that can be found in various music streaming apps: Display a LiveActivity on the lockscreen that shows a currently running event with remaining time. After this event has expired, the next one should be displayed, and so on. I would like to achieve this without using push notifications (too complex for my purposes).

What I've tried so far:

All tutorials I found only implement updates from the active app, e.g. via buttons.

Question: Is this even possible without push notification?


Solution

  • TLDR: Unfortunately without push notifications, you cannot reliably update custom UI components of a live activity with arbitrary data at any update frequency you'd like.

    Here's a more elaborate list of the different mechanisms you can use to update live activities with their pros and cons.

    System UI components that support auto-updating a live activity

    There are some built-in APIs which update specific parts of a live notification, such as ProgressView(timerInterval:countsDown:label:currentValueLabel:), which updates a progress view in your live activity every second or Text(timerInterval:pauseTime:countsDown:showsHours:), which updates a Text displaying a count down every second. These APIs keep updating your live activity even if the app has been killed by either the system or the user.

    However, these APIs are quite limited in what they can do and aren't customisable at all, so if you want to update some custom UI or change the update frequency, these APIs aren't suitable for your goals.

    Background tasks

    Background tasks give you much more freedom as they can be used to update any parts of your live activity with any data at your desired update frequency. However, the system doesn't guarantee that background tasks are delivered at exact points in time (due to power usage considerations) and hence if you want frequent updates, you cannot really rely on background tasks. Background tasks might also be cancelled if the system or the user kills your app.

    Push notifications

    Push notifications are the most reliable way of updating a live activity (assuming a stable network connection of course) as their frequency can be decided by the server and they work even if the host app has been killed by the system or the user. Live activities have their own push tokens, independent from the host app and hence can be updated independently.

    Push notifications can deliver any arbitrary data and update any custom UI of a live activity.

    The downsides of push notifications are the need for a backend which sends them out and the user needing an active internet connection to receive them.