由于ios10版本以后UILocalNotification被标为弃用了,所以要添加新的本地通知推送功能,下面提供一些代码参考。
一、先在AppDelegate.cs上注册本地通知推送功能。
1 public override bool FinishedLaunching(UIApplication app, NSDictionary options) 2 { 3 global::Xamarin.Forms.Forms.Init(); 4 global::ZXing.Net.Mobile.Forms.iOS.Platform.Init(); 5 Renderers.KeyboardOverlapRenderer.Init(); 6 7 if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) 8 { 9 //Notification framework. 10 UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (approved, err) => 11 { 12 // Handle approval 13 }); 14 15 //Get current notification settings. 16 UNUserNotificationCenter.Current.GetNotificationSettings((settings) => 17 { 18 var alertsAllowed = (settings.AlertSetting == UNNotificationSetting.Enabled); 19 }); 20 UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate(); 21 } 22 }
同时用到一个处理函数,代码如下:
1 public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate 2 { 3 #region Constructors 4 public UserNotificationCenterDelegate() 5 { 6 } 7 #endregion 8 9 #region Override Methods 10 public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) 11 { 12 // Do something with the notification 13 Console.WriteLine("Active Notification: {0}", notification); 14 15 // Tell system to display the notification anyway or use 16 // `None` to say we have handled the display locally. 17 completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound); 18 } 19 #endregion 20 }