【问题标题】:Local notification AlertAction always null in Xamarin iOSXamarin iOS 中的本地通知 AlertAction 始终为空
【发布时间】:2017-07-31 19:19:08
【问题描述】:

我想向用户显示通知,但标题始终为空。问题是通知。在显示通知之前,AlertAction 为空。

    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;
    }

我同时设置了 notification.AlertAction 和 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);
    }
}

我正在关注本教程:https://developer.xamarin.com/guides/ios/application_fundamentals/notifications/local_notifications_in_ios_walkthrough/

【问题讨论】:

    标签: xamarin xamarin.ios xamarin.forms uilocalnotification


    【解决方案1】:

    这部分的 Xamarin 中似乎存在一个错误。我尝试了您的代码,实际上无论您设置什么,AlertAction 始终为空,但这仅在使用 iOS 10+ 的设备上运行代码时才会发生。在装有 iOS 9.3 的模拟器中运行相同的代码,一切运行良好。

    一直在查看 Xamarin.iOS 代码,但无法解决任何奇怪的问题,因此下一步将打开一个问题。

    一件事:对于您的具体示例,您可以使用 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);
    }
    

    UIAlertController okayAlertController = UIAlertController.Create(notification.AlertTitle, notification.AlertBody, UIAlertControllerStyle.Alert);
    

    这实际上是设置通知标题的属性。 AlertAction 用于操作文本,以防您想从通知中设置一个。

    okayAlertController.AddAction(UIAlertAction.Create(notification.AlertAction, UIAlertActionStyle.Default, null));
    

    但由于您目前只使用“Ok”,在真正的问题得到解决之前,您应该不会有任何问题。

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-10
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 2019-02-13
      • 2017-12-28
      相关资源
      最近更新 更多