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更新之后,推送也是做了一些小小的修改,下面我就给大家仔细说说.希望看完我的这篇文章,对大家有所帮助. 一.简单入门篇---看完就可以简单适配完了 相对简单的推送证书以及环境的问题,我就不在这 ...
随机推荐
- 想进BAT?这些面试题助你一臂之力
1 软性热身题 这种题目,考的就是你的软性能力,比如表达能力,理解能力,协调能力,一个词概括就是套路.这类题目会在面试开始热身的时候,问一道两题,不会多,但是如果你能回答的有条不紊,清晰达意,那么就会 ...
- Jmeter-深入理解cookie,session,token
1.很久很久以前,Web 基本上就是文档的浏览而已, 既然是浏览,作为服务器, 不需要记录谁在某一段时间里都浏览了什么文档,每次请求都是一个新的HTTP协议, 就是请求加响应, 尤其是我不用记住是谁 ...
- var、let、const与JavaScript变量/常量的定义
早期的JavaScript中,声明变量只能使用var关键字定义变量,并没有定义常量的功能.通过var关键字定义的变量,其作用域只能函数级或是全局作用域,并没有块级作用域.ES6(ECMAScript ...
- Leetcode 488.祖玛游戏
祖玛游戏 回忆一下祖玛游戏.现在桌上有一串球,颜色有红色(R),黄色(Y),蓝色(B),绿色(G),还有白色(W). 现在你手里也有几个球. 每一次,你可以从手里的球选一个,然后把这个球插入到一串球中 ...
- vlc无法播放.flv视频文件
解决方法:https://videoconverter.wondershare.com/vlc/flv-not-displaying-video-vlc-media-player.html. 在pre ...
- Weixin API -- 微信js接口
今天在开发项目的时候,由于需要在微信中实现分享功能(分享成功后实现页面跳转并记录).问度娘,找了很久,终于找到一个不错的方法.收藏起来以后备用,也希望对大家有所帮助! 在github的地址:https ...
- BZOJ 3876 支线剧情(有上下界的无源汇最小费用可行流)
3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec Memory Limit: 256 MB Submit: 1783 Solved: 1079 [Submit][St ...
- ECNU 3263 丽娃河的狼人传说(差分约束)
丽娃河的狼人传说 Time limit per test: 1.0 seconds Memory limit: 256 megabytes 丽娃河是华师大著名的风景线.但由于学校财政紧缺,丽娃河边的路 ...
- 座位安排(seat)
座位安排(seat) 题目描述 费了一番口舌,wfj_2048终于成功地说服了n∗mn∗m个妹子来陪自己看电影. 为了这次声势浩大,wfj_2048包下了一座有n∗mn∗m个座位的电影院. (wfj_ ...
- EasyUI序列化提交学习总结
jquery easyui将form表单元素的值序列化成对象 form表单 <form id="ff"> <input type="text" ...