Hello
I am trying to work with Xamarin.Form. To get a overview I want to write an application that shows on all plattforms (UWP, ANDROID, iOS) a local notification.
For UWP it worked perfectly!
I am using dependency injection.
For iOS I have this Code here. And this do not work. The Notification is never shown!
There is no crash and monitoring the code with a breakpoints do not Show any problems.
public void SetGeofence(List<GeoLocation> GeofencesInformationList)
{
var locationManger = new CLLocationManager();
locationManger.RequestAlwaysAuthorization();
locationManger.RequestWhenInUseAuthorization();
if (CLLocationManager.LocationServicesEnabled)
{
if (CLLocationManager.Status != CLAuthorizationStatus.Denied)
{
if (CLLocationManager.IsMonitoringAvailable(typeof(CLCircularRegion)))
{
locationManger.DidStartMonitoringForRegion += (o, e) => {
Console.WriteLine("Now monitoring region {0}", e.Region.ToString());
showNotification("Test", "Test!");
};
locationManger.RegionLeft += (o, e) => {
Console.WriteLine("Just left " + e.Region.ToString());
};
locationManger.RegionEntered += LocationManger_RegionEntered;
foreach (var item in GeofencesInformationList)
{
locationManger.LocationsUpdated += LocationManger_LocationsUpdated;
CLCircularRegion region = new CLCircularRegion(new CLLocationCoordinate2D(item.Latitude, item.Longitude), item.radius, item.ID);
if (!locationManger.MonitoredRegions.Contains(region))
{
locationManger.StartMonitoring(region);
}
}
}
}
}
}
private void LocationManger_LocationsUpdated(object sender, CLLocationsUpdatedEventArgs e)
{
showNotification("View Alert", "Location Updated!");
}
private void LocationManger_RegionEntered(object sender, CLRegionEventArgs e)
{
Console.WriteLine("Just entered " + e.Region.ToString());
showNotification("View Alert", "Just entered");
}
private void showNotification(string Header, string Content)
{
UILocalNotification notification = new UILocalNotification();
notification.AlertTitle = "Alert Title"; // required for Apple Watch notifications
notification.FireDate = NSDate.FromTimeIntervalSinceNow(15);
notification.AlertAction = Header;
notification.AlertBody = Content;
UIApplication.SharedApplication.ScheduleLocalNotification(notification);
}
In App delegate i tried to add this Code here https://developer.xamarin.com/guides/ios/platform_features/user-notifications/deprecated/local_notifications_in_ios_walkthrough/
and also tried this
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
// Ask the user for permission to get notifications on iOS 10.0+
UNUserNotificationCenter.Current.RequestAuthorization(
UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
(approved, error) => { });
}
else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
// Ask the user for permission to get notifications on iOS 8.0+
var settings = UIUserNotificationSettings.GetSettingsForTypes(
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
new NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
}
in info.plist I added this keys.
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Test</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Test1</string>
It would be awsome if someone can help me. I really want to write code in Xamarin.Forms.
I dont want to use the Plugins because i want to learn it first step by step.
Thanks
Agredo