Changing the live activity without push notification

I am trying to implement "Live activity" to my app. I am following the Apple docs.

Link: https://developer.apple.com/documentation/activitykit/displaying-live-data-with-live-activities

Example code:


struct LockScreenLiveActivityView: View {
    let context: ActivityViewContext<PizzaDeliveryAttributes>
    
    var body: some View {
        VStack {
            Spacer()
            Text("\(context.state.driverName) is on their way with your pizza!")
            Spacer()
            HStack {
                Spacer()
                Label {
                    Text("\(context.attributes.numberOfPizzas) Pizzas")
                } icon: {
                    Image(systemName: "bag")
                        .foregroundColor(.indigo)
                }
                .font(.title2)
                Spacer()
                Label {
                    Text(timerInterval: context.state.deliveryTimer, countsDown: true)
                        .multilineTextAlignment(.center)
                        .frame(width: 50)
                        .monospacedDigit()
                } icon: {
                    Image(systemName: "timer")
                        .foregroundColor(.indigo)
                }
                .font(.title2)
                Spacer()
            }
            Spacer()
        }
        .activitySystemActionForegroundColor(.indigo)
        .activityBackgroundTint(.cyan)
    }
}

Actually, the code is pretty straightforward. We can use the timerInterval for count-down animation. But when the timer ends, I want to update the Live Activity view. If the user re-opens the app, I can update it, but what happens if the user doesn't open the app? Is there a way to update the live activity without using push notifications?

Post not yet marked as solved Up vote post of tolgaytoklar Down vote post of tolgaytoklar
3.9k views

Replies

I have the same issue. I would also like to update the timer text label as soon as the timer counts down to 0.

  • Using background tasks is not reliable enough for this use-case, as the timing of background tasks is up to the system and might only be called several minutes after the countdown has already expired.
  • Using server-side push notifications for this simple and very common use-case also seems to be a ton of work that could be avoided.

Something like Text(timerInterval: context.state.deliveryTimer, countsDown: true, timerEnded: "EXPIRED") would be quite nice.

This presumably must be possible in some form, as the Timer app demonstrates this natively on iOS 16.1. The timer has a great countdown on the Lock Screen and Dynamic Island, and updates itself once the countdown hits zero. It seems unlikely that this is occurring through background tasks, as those tasks are not guaranteed to run at the specified time, and this behavior continues to work with Airplane Mode on, so it is not networking/remote push based.

My thoughts thus far are implementing an internal timer (via Combine, SwiftUI, or UIKit) that checks if a certain condition is met (IE, it updates once a second and checks if a date is met or threshold passed) and initiates the Live Activity update, or that the Apple Timer app may be using some sort of internal/private entitlement that third-party developers do not have access to.

Most of the use case examples of Live Activities do seem more based in activities that update from the network (IE sports scores updates, rideshare updates, food delivery updates) that wouldn't necessarily have updates perpetuated by the system and the exact timing would not be as relevant. Hopeful that someone from Apple can comment more as there seems to be a missing way to handle updating a Live Activity when a condition is met or a guaranteed time.

Hello,

exact same problem here. Has anyone found a solution? Thank you.

Hi, any updates?

I’d like to know this also. For regular widgets we can update them by just scheduling another timeline entry when we have to. There has to be something we’re missing right?

Having the same issue here for my app.

The unfortunate thing is that the timer starts counting up (mine works like a countdown), which makes it incredibly confusing for the user. As a workaround, I have to put the time the countdown ends.

Using an if conditional (check if the date < current date) doesn't work as well. Seems like the UI is only updated once.

Seems like there's no reliable way to do it now other than through a server-side push notification...

No, you're not missing anything. This came up several times in the Slack-based Tech Talk chats too... Your choices are push notifications or background tasks. If you use background tasks you have very very little control over when they run. I eventually gave up and setup a very basic push system to do what I wanted and that works great. Basic push is pretty simple to do - I'd strongly recommend going down that road even if it's something where you really shouldn't need it.

  • Hi. By basic push system you mean server-based push notification? Thanks

Add a Comment

I have a different but related question. I’ve seen some apps able to animate continuously in live activities like a character dancing non stop. How is that done? I don’t think the app is sending live activities updates every second. It wouldn’t even be possible I don’t think when backgrounded.

Add a Comment