I have an Xamarin IOS Application for which I want to implement a background fetch. When I trigger it on the simulator it works as expected. But on the device it is never executed.
In the AppDelegate "FinishedLaunching" I set the interval to the minimum:
UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);
and I've implemented the perform fetch method:
public override async void PerformFetch(UIApplication application,
Action<UIBackgroundFetchResult> completionHandler)
{
UIBackgroundFetchResult fetchResult;
try
{
Debug.Write("Start background fetch.");
bool canResolve = Mvx.CanResolve<IBusinessFacade>();
if (!canResolve) return;
var businessFacade = Mvx.Resolve<IBusinessFacade>();
//Nach Zeitstempel neu laden
var reloadChangesResult = await businessFacade.ReloadByTimestamp();
//Aktualisieren der Kontakte
ReloadContactsFromAddressBook();
var resultReload = await businessFacade.ReloadContacts();
//Ich Kontakt neu laden und bei Änderung Service notifizieren
var reloadMeResult = await businessFacade.ReloadMeContact();
if (reloadMeResult.Success)
{
if (reloadMeResult.Result.AnyValueChanged || reloadMeResult.Result.HashChanged)
{
var updateMeResult = await businessFacade.UpdateMeContactToService();
}
}
//Notifikationen laden
var reloadRequestsResult = await businessFacade.ReloadRequests();
//Pusk aktualisieren
var pushResult = await businessFacade.RegisterPush();
// check if there where new data downloaded.
if (reloadChangesResult.Result || reloadRequestsResult.Result)
{
fetchResult = UIBackgroundFetchResult.NewData;
}
else
{
fetchResult = UIBackgroundFetchResult.NoData;
}
}
catch (Exception ex)
{
Debug.Write(ex);
Analytics.TrackEvent("Exception: Background Fetch", new Dictionary<string, string>
{
{"Exception", ex.ToString()}
});
fetchResult = UIBackgroundFetchResult.Failed;
}
completionHandler(fetchResult);
}
private async void ReloadContactsFromAddressBook()
{
var addressBook = new AddressBookService();
var dbFactory = new DbFactory();
var contactDataService = new ContactDataService(new ContactRepository(dbFactory), new SettingsAdapter(new Settings()));
var phonebookContacts = addressBook.GetAllContacts();
HashSet<string> contacts = new HashSet<string>((await contactDataService.GetAllContacts()).Select(r => r.IdAddressbook));
var listNewContacts = phonebookContacts.Where(c => !contacts.Contains(c.AddressBookId));
foreach (var contact in listNewContacts)
{
ContactEntity ce = new ContactEntity
{
IdAddressbook = contact.AddressBookId,
DisplayName = contact.DisplayName,
IdService = 0,
DisplayImage = contact.Image,
DisplayBirthDate = contact.Events.FirstOrDefault(r => r.EventType == Models.Enums.EventType.Birthday)?.EventDate ?? DateTime.MinValue,
DisplayPhoneNumber = string.Empty
};
await contactDataService.SaveContact(ce);
}
}
Background modes are enabled in my info.plist:
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
</array>
I target IOS 10 and above. Xamarin IOS Version: 11.6.1.3
What could be wrong here?