由于ios10版本以后UILocalNotification被标为弃用了,所以要添加新的本地通知推送功能,下面提供一些代码参考。
一、先在AppDelegate.cs上注册本地通知推送功能。
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
global::ZXing.Net.Mobile.Forms.iOS.Platform.Init();
Renderers.KeyboardOverlapRenderer.Init(); if (UIDevice.CurrentDevice.CheckSystemVersion(, ))
{
//Notification framework.
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (approved, err) =>
{
// Handle approval
}); //Get current notification settings.
UNUserNotificationCenter.Current.GetNotificationSettings((settings) =>
{
var alertsAllowed = (settings.AlertSetting == UNNotificationSetting.Enabled);
});
UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
}
}
同时用到一个处理函数,代码如下:
public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
#region Constructors
public UserNotificationCenterDelegate()
{
}
#endregion #region Override Methods
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
// Do something with the notification
Console.WriteLine("Active Notification: {0}", notification); // Tell system to display the notification anyway or use
// `None` to say we have handled the display locally.
completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound);
}
#endregion
}
通知处理函数
二、定义调用本地通知推送的函数,如果是xamarn.From可以写成一个Dependencies方法来调用。
public void ShowNotification(string strNotificationTitle,
string strNotificationSubtitle,
string strNotificationDescription,
string strNotificationIdItem,
string strDateOrInterval,
int intervalType,
string extraParameters)
{
//intervalType: 1 - set to date | 2 - set to interval //Object creation.
var notificationContent = new UNMutableNotificationContent(); //Set parameters.
//notificationContent.Title = "Mesince";
notificationContent.Subtitle = strNotificationSubtitle;
notificationContent.Body = strNotificationDescription;
//notificationContent.Badge = 1;
notificationContent.Badge = Int32.Parse(strNotificationIdItem);
notificationContent.Sound = UNNotificationSound.Default; //Set date.
//DateTime notificationContentDate = Convert.ToDateTime(strDateOrInterval);
DateTime notificationContentDate = DateTime.Now.AddMilliseconds(); NSDateComponents notificationContentNSCDate = new NSDateComponents();
notificationContentNSCDate.Year = notificationContentDate.Year;
notificationContentNSCDate.Month = notificationContentDate.Month;
notificationContentNSCDate.Day = notificationContentDate.Day;
notificationContentNSCDate.Hour = notificationContentDate.Hour;
notificationContentNSCDate.Minute = notificationContentDate.Minute;
notificationContentNSCDate.Second = notificationContentDate.Second;
notificationContentNSCDate.Nanosecond = (notificationContentDate.Millisecond * ); //Set trigger and request.
var notificationRequestID = Guid.NewGuid().ToString();
UNNotificationRequest notificationRequest = null;
//在某月某日某时触发
if (intervalType == )
{
var notificationCalenderTrigger = UNCalendarNotificationTrigger.CreateTrigger(notificationContentNSCDate, false); notificationRequest = UNNotificationRequest.FromIdentifier(notificationRequestID, notificationContent, notificationCalenderTrigger);
}
else
{
//一定时间后触发
var notificationIntervalTrigger = UNTimeIntervalNotificationTrigger.CreateTrigger(Int32.Parse(strDateOrInterval), false); notificationRequest = UNNotificationRequest.FromIdentifier(notificationRequestID, notificationContent, notificationIntervalTrigger);
} //Add the notification request.
UNUserNotificationCenter.Current.AddNotificationRequest(notificationRequest, (err) =>
{
if (err != null)
{
System.Diagnostics.Debug.WriteLine("Error : " + err);
}
});
}
定义调用方法
方法中定义了两种方式,只实现了一种。
三、上面是Ios 10以上版本的方法,旧版本的方法在此也放出来:
if (UIDevice.CurrentDevice.CheckSystemVersion(, ))
{
ShowNotification(Title, Title, Content, "", "2017-28-02 08:30:00", , "");
}
else
{
var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Sound, null);
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings); UILocalNotification notification = new UILocalNotification();
notification.TimeZone = NSTimeZone.DefaultTimeZone;
notification.AlertLaunchImage = "ico.png";
notification.FireDate = NSDate.FromTimeIntervalSinceNow();
notification.AlertAction = AppResource.提示;//获取得访问消息中心权限对话框标题
notification.AlertTitle = Title;
notification.AlertBody = Content;
if (CurrentApp.HasSound)
{
notification.SoundName = UILocalNotification.DefaultSoundName;
}
if (CurrentApp.HasVibrate)
{
}
//判断是否开启新邮件提醒
if (CurrentApp.NewMialRemind)
{
//UIApplication.SharedApplication.ScheduleLocalNotification(notification);
//立即触发一个通知
UIApplication.SharedApplication.PresentLocalNotificationNow(notification);
}
}
IOS 10 以下版本方法