Display ActivityKit Live Activity View using local push notifications when app is in the background

526 Views Asked by At

I am trying to create an app with live activity showing when user enters and exit a region. I have location manager setup and region setup in my app. App sends local push notification when user enters and exits a region. I want to show live activity view with push notification when user enters region. I tried using 'didEnterRegion' delegate. But I am getting the error Error requesting Live Activity: The operation couldn’t be completed. Target is not foreground.. Is there any workaround for this?

3

There are 3 best solutions below

0
On

Using actionable notification type can help you by guiding user for tap and hold it to add:

func userNotificationCenter(_ center: UNUserNotificationCenter,
       didReceive response: UNNotificationResponse,
       withCompletionHandler completionHandler: 
         @escaping () -> Void) {
       
   // Get the details from the original notification.
   let userInfo = response.notification.request.content.userInfo
        
   // Perform the task associated with the action.
   switch response.actionIdentifier {
   case "liveActivity":
      initiateLiveActivity(with: userInfo)
      break

   // Handle other actions…
   default:
      break
   }
    
   // Always call the completion handler when done.    
   completionHandler()
}

For remote push notifications, you need to prepare valid payload for this. More details about actionable notification from Apple documentation.

0
On

Live activity can not start in application's background mode. apple_documentation kindly read this

0
On

This is not possible. A Live Activity can only be started when the app is in foreground based on documentation.

Your best bet is to send the notification, have the user open your app, and start the Live Activity.