xamarin.ios 本地通知推送
由于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 以下版本方法
xamarin.ios 本地通知推送的更多相关文章
- IOS 本地通知推送消息
在现在的移动设备中,好多应用性的APP都用到了推送服务,但是有好多推送的内容,比如有的只是单纯的进行推送一个闹钟类型的,起了提醒作 用,有的则是推送的实质性的内容,这就分为推送的内容来区别用什么推送, ...
- 在Unity3D中实现安卓平台的本地通知推送
[前言] 对于手游来说,什么时候需要推送呢?玩过一些带体力限制的游戏就会发现,我的体力在恢复满后,手机会收到一个通知告诉我体力已完全恢复了.这类通知通常是由本地的客户端发起的,没有经过服务端. 在安卓 ...
- iOS - Push 通知推送
1.UserNotifications 通知是 App 用来和用户交流的一种方式,特别是当 App 并没有在前台运行的时候.通知,正如它的名称所强调的,被用作向用户'通知'一个事件,或者仅仅向用户提示 ...
- iOS 本地消息推送机制
转发自:https://www.jianshu.com/p/e347f999ed95 //已经废除的 http://blog.csdn.net/three_zhang/article/deta ...
- IOS - 本地消息推送
第一步:创建本地推送 // 创建一个本地推送 UILocalNotification *notification = [[[UILocalNotification alloc] init] autor ...
- iOS 通知推送APNS
结合网上各个资料,再简单整理的一份. 一.APNS推送说明 1.你的IOS应用需要去注册APNS消息推送功能. 2.当苹果APNS推送服收到来自你应用的注册消息就会返回一串device token给你 ...
- iOS上简单推送通知(Push Notification)的实现
iOS上简单推送通知(Push Notification)的实现 根据这篇很好的教程(http://www.raywenderlich.com/3443/apple-push-notification ...
- iOS 10 消息推送(UserNotifications)秘籍总结(二)
背景 上一篇博客iOS 10 消息推送(UserNotifications)秘籍总结(一)发布后被 简书编辑推荐至首页,这着实让我受宠若惊啊.可是好事不长,后面发生了让我伤心欲绝的事,我的女朋友不要我 ...
- iOS开发 iOS10推送必看
iOS10更新之后,推送也是做了一些小小的修改,下面我就给大家仔细说说.希望看完我的这篇文章,对大家有所帮助. 一.简单入门篇---看完就可以简单适配完了 相对简单的推送证书以及环境的问题,我就不在这 ...
随机推荐
- Struts之上传
上传的jsp写法: <tr> <td width="50%" align="left">软件上传: <input type=&qu ...
- Delphi字符串处理函数
1.Copy 功能说明:该函数用于从字符串中复制指定范围中的字符.该函数有3个参数.第一个参数是数据源(即被复制的字符串),第二个参数是从字符串某一处开始复制,第三个参数是要复制字符串的长度(即个数) ...
- Bugku杂项-convert
一进去就发现一堆二进制数,然后考虑怎么才能把这个和隐写扯上关系.首先,二进制我们肉眼就是看不懂再说什么的,这里就想到了转换,再联想上hex将原始数据转化为16进制.我们可以先把2进制转化为16进制,然 ...
- uncompyle2反编译python的.py文件
前几天学用github,一不小心把a.py文件给删除了,由于1天没有提交,也无法找回.突然发现同a.py文件生成的编译文件a.pyc还在,逐去搜索一番反编译的方法. 查询得知python比较好的工具u ...
- 单元测试如何保证了易用的API
一般而言TDD的好处是以输出为导向及早发现问题,以及方便重构(单元测试保证).我理解,还有一个比较重要的意义是: 客观上强制了程序员写出更加友好的接口 方便测试和联调. 问题 这里我以c++举例,需求 ...
- TOJ5272: 逆矩阵
5272: 逆矩阵 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 11 ...
- 在线人数统计session管理
下午比较闲(其实今天都很闲),想了一下在线人数统计方面的实现,上网找了下这方面的知识,最初我的想法是,管理session,如果session销毁了就减少,如果登陆用户了就新增一个,但是如果是用户非法退 ...
- Jquery版放大镜效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 整合SpringMVC与Mybatis
第一步.导包 第二步.配置springmvc springmvc.xml <?xml version="1.0" encoding="UTF-8"?> ...
- POJ 2976 Dropping tests(01分数规划入门)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11367 Accepted: 3962 D ...