swift – `BGAppRefreshTask` Handler Not Being Known as Regardless of Correct Setup in iOS App

0
38
swift – `BGAppRefreshTask` Handler Not Being Known as Regardless of Correct Setup in iOS App


I’m engaged on implementing a background activity in my iOS app utilizing BGAppRefreshTask. I’ve adopted all of the steps to set it up, however the background activity didn’t work for the background and terminated utility states. Right here’s what I’ve completed up to now:

Setup Particulars:

  1. Signing & Capabilities Configuration: Enabled Background fetch and Background Processing choices.

  2. Data.plist Configuration:
    I’ve added the BGTaskSchedulerPermittedIdentifiers key with my background activity identifier (e.g., “com.instance.myapp.refresh”).

  3. Registering the Background Job:
    In utility(_:didFinishLaunchingWithOptions:), I name the registerBackgroundTask() methodology to register the duty:

    func registerBackgroundTask() {
        BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.instance.myapp.refresh", utilizing: nil) { activity in
            guard let activity = activity as? BGAppRefreshTask else {
                return
            }
            Job {
                await self.scheduleNotificationIfNeeded()
                activity.setTaskCompleted(success: true)
            }
            activity.expirationHandler = {
                print("BGAppRefreshTask is expired")
            }
        }
    }
    
  4. Scheduling the Job: I’m scheduling the duty like this:

    let request = BGAppRefreshTaskRequest(identifier: "com.instance.myapp.refresh")
    request.earliestBeginDate = Date(timeIntervalSinceNow: 61) // 61 seconds later for testing
    
    do {
        strive BGTaskScheduler.shared.submit(request)
    } catch {
        print("Didn't submit BGAppRefreshTaskRequest: (error)")
    }
    
  5. Testing Circumstances: I’ve examined all these situations with a bodily gadget and never with a simulator. I realized that it is not working in Simulator. Right here is the situations that I gave a strive:

  • I’ve tried each the app is in background & terminated states and await the
    activity to set off, There isn’t any triggered notification.
  • I’ve verified that:
    • The duty identifier within the code matches the one in Data.plist.
    • The registerBackgroundTask perform is being known as in utility(_:didFinishLaunchingWithOptions:).
    • The duty is scheduled with an earliestBeginDate of 61 seconds for testing.
  1. Debugging Makes an attempt:
  • Added print statements to see if the handler is known as (it is not) within the background app state.
  • Used Xcode’s Simulate Background Fetch debug choice to set off the background activity, however nonetheless no luck.
  • Verified that the gadget isn’t in Low Energy Mode and Background app refresh toggle is ON within the app settings.

Query:
What could possibly be inflicting the BGAppRefreshTask doesn’t work regardless of having all the things seemingly arrange accurately? Is there anything I ought to be checking or any potential points with iOS’s background activity dealing with that could possibly be inflicting this?

Any steerage or troubleshooting suggestions can be enormously appreciated!

LEAVE A REPLY

Please enter your comment!
Please enter your name here