I'm creating a geolocation app, and I wanted to start the startup automatically of a service that sent me coordinates every time.
Looking at the internet I read that technically it is not possible on ios, but there are some methods to overcome the problem.
I wanted to adopt the silent notification.
I implemented the method and when the app is in the backgorund everything works, but when I turn on the device and send a silent notification, the event: didReceiveRemoteNotification: does not trigger. or even when I close the app from the app switcher and then send a notification is not triggered.
I wanted to know if there is something wrong if you can do it.
ps: I'm using: didReceiveRemoteNotification: and no application: didReceiveRemoteNotification: fetchCompletionHandler: (can this be the problem?)
My Code in AppDelegate:
public override async void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult>
completionHandler)
{
await Manager.SendPosition(completionHandler);
}
in my FinishedLaunching:
if (Convert.ToInt16(UIDevice.CurrentDevice.SystemVersion.Split('.')[0].ToString()) < 8)
{
UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge |
UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
}
else
{
UIUserNotificationType notificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
var settings = UIUserNotificationSettings.GetSettingsForTypes(notificationTypes, new NSSet(new string[] { }));
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
UIApplication.SharedApplication.RegisterForRemoteNotifications();
}
and in my info.plist:
<key>UIBackgroundModes</key>
<array>
<string>location</string>
<string>remote-notification</string>
</array>
UPDATE:
public async Task<bool> SendPosition(Action<UIBackgroundFetchResult> completionHandler = null)
{
StartLocationUpdates();
var setting = ConnectDb()?.Table<Settings>().FirstOrDefault() ?? new Settings();
_currentLong = 14.52538;
_currentLat = 36.82842;
if (_currentLong != 0 && _currentLat != 0)// && setting.TimeNotification != 0)
{
var objectSend = new
{
unique_key = setting.UniqueKey,
lat = _currentLat,
lng = _currentLong,
idPlayerOneSignal = setting.IdPlayerOneSignal,
token = ""
};
var client = new HttpClient { BaseAddress = new Uri("http://amywebservice.com/") };
var data = JsonConvert.SerializeObject(objectSend);
var content = new StringContent(data, Encoding.UTF8, "application/json");
var response = await client.PostAsync("api/setPosition", content);
if (response.IsSuccessStatusCode)
{
var successResult = response.Content.ReadAsStringAsync().Result;
Debug.WriteLine("k", successResult);
if (completionHandler != null)
{
completionHandler(UIBackgroundFetchResult.NoData);
}
return true;
}
else
{
var failureResulr = response.Content.ReadAsStringAsync().Result;
Debug.WriteLine("f", failureResulr);
if (completionHandler != null)
{
completionHandler(UIBackgroundFetchResult.NoData);
}
return false;
}
}
return false;
}
but not work, if I send a silent notification, I only run the first fly and only if the app is in the backgorund.
If I turn my phone on and notify notification will not work.