本地推送 到9点自动刷新界面
2016-10-09 16:18
232 查看
- (void)viewWillAppear:(BOOL)animated
{
[self registUILocalNotification];
}
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification) name:@"getNotification" object:nil];
}
- (void)getNotification{
[_tableView.header beginRefreshing];
}
//第一步:创建本地推送
- (void)registUILocalNotification {
// 创建一个本地推送
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSDate *currentDate = [NSDate date];//获取当前时间,日期
//把当前时间转化为时间戳
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"HH:mm:ss"];
//时间戳
NSTimeInterval nowtime = [currentDate timeIntervalSince1970];
NSTimeInterval period = 0.0;
[dateFormatter setDateFormat:@"YYYY-MM-dd "];
NSString *dateString = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:currentDate]];
NSString *AMStr = [NSString stringWithFormat:@"%@%@",dateString,@"09:00:00"];
if (nowtime <= [self turnDayTime:AMStr]){
period = [self turnDayTime:AMStr] - nowtime;
}
if (notification != nil) {//判断系统是否支持本地通知
// 设置推送时间
notification.fireDate = [currentDate dateByAddingTimeInterval:period];
// 设置时区
notification.timeZone = [NSTimeZone defaultTimeZone];
// 设置重复间隔
notification.repeatInterval = kCFCalendarUnitDay;
// 推送声音
notification.soundName = UILocalNotificationDefaultSoundName;
// ios8后,需要添加这个注册,才能得到授权
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 通知重复提示的单位,可以是天、周、月
notification.repeatInterval = NSCalendarUnitDay;
} else {
// 通知重复提示的单位,可以是天、周、月
notification.repeatInterval = NSCalendarUnitDay;
}
//添加推送到UIApplication
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
在APPDelegate里写代理方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
NSLog(@"======9点刷新界面=====");
[[NSNotificationCenter defaultCenter] postNotificationName:@"getNotification" object:nil];
}
{
[self registUILocalNotification];
}
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification) name:@"getNotification" object:nil];
}
- (void)getNotification{
[_tableView.header beginRefreshing];
}
//第一步:创建本地推送
- (void)registUILocalNotification {
// 创建一个本地推送
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSDate *currentDate = [NSDate date];//获取当前时间,日期
//把当前时间转化为时间戳
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"HH:mm:ss"];
//时间戳
NSTimeInterval nowtime = [currentDate timeIntervalSince1970];
NSTimeInterval period = 0.0;
[dateFormatter setDateFormat:@"YYYY-MM-dd "];
NSString *dateString = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:currentDate]];
NSString *AMStr = [NSString stringWithFormat:@"%@%@",dateString,@"09:00:00"];
if (nowtime <= [self turnDayTime:AMStr]){
period = [self turnDayTime:AMStr] - nowtime;
}
if (notification != nil) {//判断系统是否支持本地通知
// 设置推送时间
notification.fireDate = [currentDate dateByAddingTimeInterval:period];
// 设置时区
notification.timeZone = [NSTimeZone defaultTimeZone];
// 设置重复间隔
notification.repeatInterval = kCFCalendarUnitDay;
// 推送声音
notification.soundName = UILocalNotificationDefaultSoundName;
// ios8后,需要添加这个注册,才能得到授权
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 通知重复提示的单位,可以是天、周、月
notification.repeatInterval = NSCalendarUnitDay;
} else {
// 通知重复提示的单位,可以是天、周、月
notification.repeatInterval = NSCalendarUnitDay;
}
//添加推送到UIApplication
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
在APPDelegate里写代理方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
NSLog(@"======9点刷新界面=====");
[[NSNotificationCenter defaultCenter] postNotificationName:@"getNotification" object:nil];
}
相关文章推荐
- Json数据异步绑定到界面的Table并且自动刷新
- Json数据异步绑定到界面的Table并且自动刷新原理及代码
- SwipeRefreshLayout 首次进入界面自动下拉刷新
- easyUI刷新界面时列自动变宽直到超过屏幕
- 在界面上添加完监听,监听服务起不来, 报错:本地计算机上的OracleOraDb11g_homeTNSListener服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止。
- (小技巧)刷新页面一定次数后自动 转到登录界面
- 使用webpack构建本地服务器,实现自动刷新,并直接在手机上访问
- sublime3安装liveload,实现前端自动F5刷新html界面
- SwipeRefreshLayout进入界面自动刷新效果
- Fragment界面莫名其妙自动刷新
- js + ajax实现自动刷新界面或局部刷新
- 写css 的时候可以自动刷新界面的js
- [置顶] ExtJs4.2 登陆界面(点击验证码自动刷新,label实现click事件)
- SwipeRefreshLayout进入界面后自动显示刷新效果
- Json数据异步绑定到界面的Table并且自动刷新
- Json数据异步绑定到界面的Table并且自动刷新原理及代码
- DataBindings 与 INotifyPropertyChanged 实现自动刷新 WinForm 界面
- bug系列-------home界面自动刷新
- Android 自定义可拖拽View界面渲染刷新后不会自动回到起始位置
- gulp创建本地服务器自动刷新浏览器