Flush and background task

The data which is collected for a session will be pushed to the server when the app goes to the background.

If your app supports BackgroundTask then you can enable BackgroundTask for Apptics by calling the following:

  • Apptics.enableBackgroundTask(true) 
  • By setting AppticsConfig.default.enableBackgroundTask=true and add these lines to the completion handler of handleEventsForBackgroundURLSession in your AppDelegate.
Copied- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler{
   if ([identifier hasPrefix:@"com.apptics.engagement.backgroundsession"]){
        [[Analytics getInstance] setBgEngagementRequestSuccessBlock:completionHandler];
    }
    
    if ([identifier hasPrefix:@"com.apptics.nonfatal.backgroundsession"]){
        [[Analytics getInstance] setBgNonFatalRequestSuccessBlock:completionHandler];
    } 
}
Copiedfunc application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {                  
    if (identifier.hasPrefix("com.apptics.engagement.backgroundsession")){             				 
      Analytics.getInstance().bgEngagementRequestSuccessBlock = completionHandler         
    }         
    if (identifier.hasPrefix("com.apptics.nonfatal.backgroundsession")){             				 
      Analytics.getInstance().bgNonFatalRequestSuccessBlock = completionHandler        
    }    
 }
  • Add "com.apptics.engagement.bgtaskidentifier" and "com.apptics.nonfatal.bgtaskidentifier" to 'BGTaskSchedulerPermittedIdentifiers' in your main info.plist.
  • The BGAppRefreshTaskRequest will be registered and scheduled just before the BackgroundTask expires and executed when the system launches your app in the background.
  • Since we have used BGAppRefreshTaskRequest, make sure you have selected ”Background fetch“ in the background modes section of your project capabilities.

Note: Available only on iOS 13.0+, MAC Catalyst 13.0+, and tvOS 13.0+.