How to end liveActivity via aps push

86 views Asked by At

I have integrated liveActivity in to my project, it does show up on the lock screen, I can update it via the script below with "event": "update", but I can't close it via "event": "end".

I added this function "endActivity(activity: Activity, dismissTimeInterval: Double?)" in anticipation that it will work, but it didn't.

Here how it looks like:

func endActivity(activity: Activity<LiveActivityWidgetAttributes>, dismissTimeInterval: Double?) async {
        let finalContent = LiveActivityWidgetAttributes.ContentState(startTimestamp: 0, arrivalTime: 0)
        let dismissalPolicy: ActivityUIDismissalPolicy
        if let dismissTimeInterval = dismissTimeInterval {
            if dismissTimeInterval <= 0 {
                dismissalPolicy = .immediate
            } else {
                dismissalPolicy = .after(.now + dismissTimeInterval)
            }
        } else {
            dismissalPolicy = .default
        }
        await activity.end(ActivityContent(state: finalContent, staleDate: nil), dismissalPolicy: dismissalPolicy)
    }

When I send the "update" via apn - it works and does update the contents of the activity. When I do send the "end" via apn - no errors in terminal and nothing happens it the app:

  • We are completely uploaded and fine < HTTP/2 200

Here is the data I send to end the liveActivity:

--data \
'{"aps": {
   "timestamp": '$(date +%s)',
   "event": "end",
   "dismissal-date": 1663177260 //timestamp in the past to close liveActivity immediately
}}' \

What is missing or completely wrong? Frequent updates are enabled.

1

There are 1 answers

0
jakub1984 On

You also need to include content-state, otherwise the push notification will not end the live activity. Here is how you should update your payload:

--data \
'{"aps": {
   "timestamp": '$(date +%s)',
   "event": "end",
   "content-state": {},
   "dismissal-date": 1663177260 
}}' \