2017-03-10 12 views
2

通知をユーザーに表示したいのですが、タイトルは常にnullです。問題はnotification.AlertActionが通知を表示する前にnullです。ローカル通知Xamarin iOSでは常にAlertActionがnullです

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/

答えて

2

この部分はXamarinのバグがあるようです。私はあなたのコードを試しましたが、本当にAlertActionはあなたが設定した内容に関係なく常にnullですが、これは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は、通知から1つを設定する場合のアクションテキストです。

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

現在のところ、「OK」だけを使用しているため、実際の問題が解決されるまで問題はありません。

このヘルプが必要です。

関連する問題