iOS Live activity banner not appearing on devices without Dynamic Island

  Kiến thức lập trình

As per the Apple documentation:

On devices that don’t support the Dynamic Island, the system uses the
Lock Screen presentation of your Live Activity as a banner that
briefly overlays the Home Screen or another app. This overlay only
happens when you alert people about an update to your Live Activity
with an alert configuration.

So I went through their demo video and passed alert configuration as following:

struct ParkWidgetAttributes: ActivityAttributes {
    
    var startDate: Date
    var endDate: Date

    // 'ContentState' represents the dynamic content of the Live Activity.
    public struct ContentState: Codable, Hashable {
        // Dynamic stateful properties about your activity go here!
        var parkingTimer: ClosedRange<Date>
    }
}

func startLiveActivity() {

    let now = Date.now
    let future = Calendar.current.date(byAdding: .minute, value: minutes, to: now)!
    let dateRange = now...future

    // (1) Fixed Attributes
    let activityAttributes = ParkWidgetAttributes(startDate: now, endDate: future)

    // (2) Content State
    let initialContentState = ParkWidgetAttributes.ContentState(parkingTimer: dateRange)

    let activityContent = ActivityContent(
        state: initialContentState,
        staleDate: future
    )

    do {
        let activity = try Activity.request(attributes: activityAttributes,
                                 content: activityContent)
        print("Requested Parking Live Activity.")
        
        // Pass an AlertConfiguration to the update(_:alertConfiguration:) function

        Task {
            let alertConfig = AlertConfiguration(
                title: "Alert title",
                body: "Alert body",
                sound: .default
            )
            await activity.update(activityContent, alertConfiguration: alertConfig)
        }

    } catch (let error) {
        print("Error requesting Parking Live Activity (error.localizedDescription).")
    }
}

Using above implementation, the live activity appears on lock screen for both the devices (with and without Dynamic Island). But for unlocked screen, it only appears on device with Dynamic Island.

LEAVE A COMMENT