zoukankan      html  css  js  c++  java
  • xamarin.ios 本地通知推送

      由于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     }
    通知处理函数

    二、定义调用本地通知推送的函数,如果是xamarn.From可以写成一个Dependencies方法来调用。

     1 public void ShowNotification(string strNotificationTitle,
     2                                 string strNotificationSubtitle,
     3                                 string strNotificationDescription,
     4                                 string strNotificationIdItem,
     5                                 string strDateOrInterval,
     6                                 int intervalType,
     7                                 string extraParameters)
     8         {
     9             //intervalType: 1 - set to date | 2 - set to interval
    10 
    11 
    12             //Object creation.
    13             var notificationContent = new UNMutableNotificationContent();
    14 
    15 
    16             //Set parameters.
    17             //notificationContent.Title = "Mesince";
    18             notificationContent.Subtitle = strNotificationSubtitle;
    19             notificationContent.Body = strNotificationDescription;
    20             //notificationContent.Badge = 1;
    21             notificationContent.Badge = Int32.Parse(strNotificationIdItem);
    22             notificationContent.Sound = UNNotificationSound.Default;
    23 
    24 
    25             //Set date.
    26             //DateTime notificationContentDate = Convert.ToDateTime(strDateOrInterval);
    27             DateTime notificationContentDate = DateTime.Now.AddMilliseconds(10);
    28 
    29             NSDateComponents notificationContentNSCDate = new NSDateComponents();
    30             notificationContentNSCDate.Year = notificationContentDate.Year;
    31             notificationContentNSCDate.Month = notificationContentDate.Month;
    32             notificationContentNSCDate.Day = notificationContentDate.Day;
    33             notificationContentNSCDate.Hour = notificationContentDate.Hour;
    34             notificationContentNSCDate.Minute = notificationContentDate.Minute;
    35             notificationContentNSCDate.Second = notificationContentDate.Second;
    36             notificationContentNSCDate.Nanosecond = (notificationContentDate.Millisecond * 1000000);
    37 
    38 
    39             //Set trigger and request.
    40             var notificationRequestID = Guid.NewGuid().ToString();
    41             UNNotificationRequest notificationRequest = null;
    42             //在某月某日某时触发
    43             if (intervalType == 1)
    44             {
    45                 var notificationCalenderTrigger = UNCalendarNotificationTrigger.CreateTrigger(notificationContentNSCDate, false);
    46 
    47                 notificationRequest = UNNotificationRequest.FromIdentifier(notificationRequestID, notificationContent, notificationCalenderTrigger);
    48             }
    49             else
    50             {
    51                 //一定时间后触发
    52                 var notificationIntervalTrigger = UNTimeIntervalNotificationTrigger.CreateTrigger(Int32.Parse(strDateOrInterval), false);
    53 
    54                 notificationRequest = UNNotificationRequest.FromIdentifier(notificationRequestID, notificationContent, notificationIntervalTrigger);
    55             }
    56 
    57 
    58             //Add the notification request.
    59             UNUserNotificationCenter.Current.AddNotificationRequest(notificationRequest, (err) =>
    60             {
    61                 if (err != null)
    62                 {
    63                     System.Diagnostics.Debug.WriteLine("Error : " + err);
    64                 }
    65             });
    66         }
    定义调用方法

    方法中定义了两种方式,只实现了一种。

    三、上面是Ios 10以上版本的方法,旧版本的方法在此也放出来:

     1 if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
     2                 {
     3                     ShowNotification(Title, Title, Content, "1", "2017-28-02 08:30:00", 1, "");
     4                 }
     5                 else
     6                 {
     7                     var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Sound, null);
     8                     UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
     9 
    10                     UILocalNotification notification = new UILocalNotification();
    11                     notification.TimeZone = NSTimeZone.DefaultTimeZone;
    12                     notification.AlertLaunchImage = "ico.png";
    13                     notification.FireDate = NSDate.FromTimeIntervalSinceNow(0);
    14                     notification.AlertAction = AppResource.提示;//获取得访问消息中心权限对话框标题
    15                     notification.AlertTitle = Title;
    16                     notification.AlertBody = Content;
    17                     if (CurrentApp.HasSound)
    18                     {
    19                         notification.SoundName = UILocalNotification.DefaultSoundName;
    20                     }
    21                     if (CurrentApp.HasVibrate)
    22                     {
    23                     }
    24                     //判断是否开启新邮件提醒
    25                     if (CurrentApp.NewMialRemind)
    26                     {
    27                         //UIApplication.SharedApplication.ScheduleLocalNotification(notification);
    28                         //立即触发一个通知
    29                         UIApplication.SharedApplication.PresentLocalNotificationNow(notification);
    30                     }
    31                 }
    IOS 10 以下版本方法
  • 相关阅读:
    Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.2——使用Android Testing Support Library进行测试
    Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.1——单元测试
    Android开发:《Gradle Recipes for Android》阅读笔记(翻译)4.5——使用Android Libraries
    Android开发:《Gradle Recipes for Android》阅读笔记(翻译)4.4——自定义代码集合
    Android开发:《Gradle Recipes for Android》阅读笔记(翻译)4.3——排除任务
    10.13总结
    10.7号解题报告
    no zuo no die
    发誓!
    NOIP2016天天爱跑步
  • 原文地址:https://www.cnblogs.com/zuimengaitianya/p/7773616.html
Copyright © 2011-2022 走看看