2017-03-10 4 views
2

Ich möchte dem Benutzer eine Benachrichtigung anzeigen, aber Titel ist immer null. Problem ist notification.AlertAction ist null, bevor die Benachrichtigung angezeigt wird.Lokale Benachrichtigung AlertAction immer Null in Xamarin iOS

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

und ich bin die Einrichtung sowohl notification.AlertAction und 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); 
    } 
} 

ich dieses Tutorial folgenden wurde: https://developer.xamarin.com/guides/ios/application_fundamentals/notifications/local_notifications_in_ios_walkthrough/

Antwort

2

Es scheint für diesen Teil ein Fehler in Xamarin zu sein. Ich habe Ihren Code ausprobiert und in der Tat ist die AlertAction immer null, egal was Sie einstellen, aber das passiert nur, wenn Sie den Code auf einem Gerät mit iOS 10+ ausführen. Führen Sie den gleichen Code in einem Simulator mit iOS 9.3 und alles lief gut.

Ich habe im Xamarin.iOS-Code nachgeschaut, konnte aber nichts Seltsames tun, so dass der nächste Schritt ein Problem verursacht.

Eine Sache: für Ihr spezielles Beispiel können Sie die AlertTitle verwenden

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

und

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

die in der Tat ist die Eigenschaft, die Titel, den Sie für die Benachrichtigung festlegen möchten. Die AlertAction ist für den Aktionstext, wenn Sie einen aus der Benachrichtigung festlegen möchten.

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

Aber wie Sie derzeit nur "Ok" verwenden, sollten Sie kein Problem haben, bis das eigentliche Problem gelöst ist.

Hoffe diese Hilfe!

Verwandte Themen