Xamarin表单中的本地通知的Foundation.ModelNotImplementedException

时间:2017-03-10 11:01:45

标签: xamarin xamarin.ios xamarin.forms

我有PCL项目,我需要显示简单的通知(当任务完成时显示简单的字符串)。在iOS项目中,当我尝试在ReceivedLocalNotification中显示通知时使用:

Window.RootViewController.PresentViewController(okayAlertController, true, null);
抛出

异常:Foundation.ModelNotImplementedException:抛出类型为“Foundation.ModelNotImplementedException”的异常。这是一个代码:

public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        ZXing.Net.Mobile.Forms.iOS.Platform.Init();
        global::Xamarin.Forms.Forms.Init();
        LoadApplication(new App());

        if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
        {
            var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes(
                UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null
            );

            app.RegisterUserNotificationSettings(notificationSettings);

            // check for a notification
            if (options != null)
            {
                // check for a local notification
                if (options.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey))
                {
                    var localNotification = options[UIApplication.LaunchOptionsLocalNotificationKey] as UILocalNotification;
                    if (localNotification != null)
                    {
                        UIAlertController okayAlertController = UIAlertController.Create(localNotification.AlertAction, localNotification.AlertBody, UIAlertControllerStyle.Alert);
                        okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

                        Window.RootViewController.PresentViewController(okayAlertController, true, null);

                        UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
                    }
                }
            }

        }

        base.FinishedLaunching(app, options);
    }

    public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
    {
        // show an alert
        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;
    }
}

和NotificationService_iOS.cs:

public class NotificationService_iOS : INotificationService
{
    public void Notify(string title, string text)
    {
        var notification = new UILocalNotification();
        notification.FireDate = NSDate.FromTimeIntervalSinceNow(0); // Fire now
        notification.AlertAction = title;
        notification.AlertBody = text;
        notification.ApplicationIconBadgeNumber = 1;
        notification.SoundName = UILocalNotification.DefaultSoundName;
        UIApplication.SharedApplication.ScheduleLocalNotification(notification);
    }
}

1 个答案:

答案 0 :(得分:3)

窗口对象不可用,您需要使用

var window = UIApplication.SharedApplication.KeyWindow;

在此处查看答案problem with Window object