BeginBackgdTask in DidReceiveRemoteNotification terminates the app before the expt suspension time

Hi all,
I am using silent push notification in my app and I have below code in DidReceiveRemoteNotification

    public override void  DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {   
        nint taskID = UIApplication.SharedApplication.BeginBackgroundTask(() => {});
    new Task(() => {

            BackgroundService backgroundService = new BackgroundService();
            backgroundService.Sync(false);
            UIApplication.SharedApplication.EndBackgroundTask(taskID);
            completionHandler(UIBackgroundFetchResult.NewData);

    }).Start();
    }

After receiving silent push, I am syncing with server to save latest data. My problem is iOS suspends the task in random seconds like 30, 40...and not beyond 1 minute. But I need at least 5 minutes to keep the app alive for sync to complete. The above task BeginBackgroundTask supposed to keep the app alive until the task completes right? or am I missing something?

From Xamarin documentation:
You can use this pattern anywhere in your application where you don't want a task being interrupted should the user put the app into the background

But iOS terminates the background task after silent push, before the expected suspension time. I can't use NSUrlSession also, since my sync is not like passing url to download data. This may be simple and has been asked many times also, but please point me out where am I going wrong?

Answers

  • DannyCDannyC US ✭✭✭✭

    @SubhaMuralidharan iOS gives you very limited usually 30 seconds to complete the task. The same 30 seconds would apply if you do a PerformFetch. If you need longer than that then NSURLSession is the only option. Not sure why you cannot use it.

Sign In or Register to comment.