I would like to show notification to user, but title is always null. Problem is notification.AlertAction is null before showing notification.
public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
{
// notification.AlertAction is null!!! while notification.AlertBody is correct
UIAlertController okayAlertController = UIAlertController.Create(notification.AlertAction, notification.AlertBody, UIAlertControllerStyle.Alert);
okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
Window.RootViewController.PresentViewController(okayAlertController, true, null);
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
}
and I am setting both notification.AlertAction and notification.AlertBody:
public class NotificationService_iOS : INotificationService
{
public void Notify(string title, string text)
{
var notification = new UILocalNotification();
notification.FireDate = NSDate.FromTimeIntervalSinceNow(0);
notification.AlertAction = title;
notification.AlertBody = text;
notification.ApplicationIconBadgeNumber = 1;
notification.SoundName = UILocalNotification.DefaultSoundName;
UIApplication.SharedApplication.ScheduleLocalNotification(notification);
}
}
I was following this tutorial: https://developer.xamarin.com/guides/ios/application_fundamentals/notifications/local_notifications_in_ios_walkthrough/
There seems to be an bug in Xamarin for this part. I tried your code and indeed the AlertAction is always null not matter what you set but this is only happening when running the code on a device with iOS 10+. Run the same code in a simulator with iOS 9.3 and everything ran just fine.
Have been looking in the Xamarin.iOS code but couldn't fine anything weird so the next step will be opening an issue.
One thing: for your specific example you can use the AlertTitle
public void Notify(string title, string text)
{
var notification = new UILocalNotification();
notification.FireDate = NSDate.FromTimeIntervalSinceNow(0);
notification.AlertTitle = title;
notification.AlertBody = text;
// Use this if you want to set the Action Text from here.
notification.AlertAction = "Got it!!";
notification.ApplicationIconBadgeNumber = 1;
notification.SoundName = UILocalNotification.DefaultSoundName;
UIApplication.SharedApplication.ScheduleLocalNotification(notification);
}
and
UIAlertController okayAlertController = UIAlertController.Create(notification.AlertTitle, notification.AlertBody, UIAlertControllerStyle.Alert);
Which is in fact the property to set the title you want for the notification. The AlertAction is for the Action text in case you want to set one from the notification.
okayAlertController.AddAction(UIAlertAction.Create(notification.AlertAction, UIAlertActionStyle.Default, null));
But as you are currently using just "Ok" you shouldn't have any problem until the real issue is resolved.
Hope this help!