0

When implementing lock screen live activity widget for debugging purposes I had a button which runs startLiveActivity() function, app runs normaly widget appears totally functioning.

However I need this activity widget to appear whenever the app receives a remote push notification when the app is killed or in background, but it is appearing only when app is in foreground which doesn't really help the case here.

class LiveActivityHelper: ObservableObject {
    
    static var shared = LiveActivityHelper()
    
    @Published var activity: Activity<Attributes>? = nil

    func startLiveActivity() {
        
        let state = Attributes.ContentState()
        
        activity = try? Activity<Attributes>.request(attributes: Attributes(), contentState: state, pushType: nil)

    }

I tried running the startLiveActivity() function from appDelegate's didReceiveRemoteNotification:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse) async {
        
        LiveActivityHelper.shared.startLiveActivity()
}

And from a notification extension I have:

class NotificationService: UNNotificationServiceExtension, UNUserNotificationCenterDelegate {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {

    ...
    LiveActivityHelper.shared.startLiveActivity()

All these approaches lead to the same result, the widget appearing only when the app is opened.

Apple's documentation included updating the widget from a remote notification but not how to start it using that approach.

  • Highly doubt Apple would allow this, background is very limited. Limited to only Background Modes and almost always restricted to the user triggering start. – lorem ipsum Jul 27 '23 at 14:26
  • how is updating a live activity widget in background is allowed and not creation? – raouf Mehdid Jul 27 '23 at 15:11
  • You'll have to ask Apple, but probably because by launching the app the user has made a conscious action which results in starting the activity. If pushes can start the activity themselves, without user action, you could end up, hypothetically, with 100 different apps all trying to fight to start an activity at the same time via a push. How does the UI display 100 activities? Activities the user didn't even start?The user is always in control is behind the reasoning in every restriction Apple place on app developers. – Gruntcakes Jul 27 '23 at 15:15
  • Because the user has done something in your app to get it started. If an iOS user kills and app they don’t expect for that app to put something on their island, Lock Screen, etc. every app would be trying to put their live activity in the user’s eye. Every app is fighting for the user’s attention. Apple doesn’t allow apps to do stuff when they have been killed and very little can be done in the background. You need a very good reason and the user is well aware of this reason. – lorem ipsum Jul 27 '23 at 15:19
  • @loremipsum if so how can it be done? – raouf Mehdid Jul 27 '23 at 18:06
  • Like I said in my first comment “Background Modes” is a start. – lorem ipsum Jul 27 '23 at 18:13
  • @loremipsum doesn't look to be doing much work on the issue. – raouf Mehdid Jul 27 '23 at 22:15
  • Work on the issue? SO isn't my job, I made a couple of comments but I am certainly not working on anything for you. – lorem ipsum Jul 27 '23 at 22:18

1 Answers1

0

A Live Activity can only be started while the app is in foreground, because its purpose is to track a user-initiated feature, based on documentation.

Send your push notification, have your user open your app with it, then start the activity. Your desired flow is explicitly stated to be unsupported.

iSpain17
  • 2,502
  • 3
  • 17
  • 26