How do I create a periodic background job for iOS?

grimordegrimorde Member ✭✭
edited April 29 in Xamarin.Forms

I am creating an app for a customer used by their staff which requires the ability to send data back to a main repository via an API. If there is no internet connection the data is flagged, and a periodic background job will check for outstanding data requiring uploading if a connection is available.

I have this working for the Android version of the application using a dependency service (I'm following the Microsoft architecture example from here https://docs.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/).

When the app starts it calls my android implementation:-

public void SetupBackgroundSync()
        {
            Context context = Application.Context;
            var schedule = 30;

            using (var manager = (AlarmManager)context.GetSystemService(Context.AlarmService))
            {
                var syncIntent = new Intent(context, typeof(BackgroundSync));
                var pendingIntent = PendingIntent.GetService(context, 0, syncIntent, PendingIntentFlags.CancelCurrent);
                manager.SetRepeating(AlarmType.RtcWakeup, 1000 * 60 * schedule, 1000 * 60 * schedule, pendingIntent);
            }
        }

I can't seem to find a good example of how to do this for iOS. I've read the documentation on the background task options and it seems I would be looking at a Long Running Task - but it seems 'hacky' to have a timer within that to check for data I need to send back to an API every 30 minutes.

Does anybody have any examples/suggestions I could use?

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    There are server ways to implement periodic background task.

    1. Pushing Updates to Your App Silently

      You could deliver periodic silent notifications and wake up your app in the background on the user's device , everytime it will call the method application:didReceiveRemoteNotification:fetchCompletionHandler.

    2. Updating Your App with Background App Refresh

    • Call setMinimumBackgroundFetchInterval: to set periodic time .
    • Execute background task in the method application:performFetchWithCompletionHandler:
      [Note]The feature is available after iOS 7.
  • grimordegrimorde Member ✭✭

    Thank you @ColeX I will take a look at those options :)

Sign In or Register to comment.