本地推送 到9点自动刷新界面

来源:互联网 发布:手机百度云网络异常 编辑:程序博客网 时间:2024/04/30 10:00

- (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];

}

0 0
原创粉丝点击