ios 闹钟的总结-------使用的是本地通知。

1. 首先在你指定的界面写这个方法
//注册本地通知的方法
-(void)resginloaclNotifation
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *nowdate = [NSDate date];
    NSLog(@"%@",nowdate);
    NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
    NSDateComponents *dateComponents = [calender components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nowdate];
    NSLog(@"%@",dateComponents);
    [dateComponents setHour:9];
    [dateComponents setMinute:00];
    NSLog(@"^^^^^^^^^^^^^^^````````%@",checksignAndDailywork.isdailywork);
    
    UILocalNotification *notification = [[UILocalNotification alloc]init];
    if (notification != nil) {
        notification.fireDate=[calender dateFromComponents:dateComponents]; ;
        notification.timeZone=[NSTimeZone defaultTimeZone];
        notification.alertBody=@"你今天还没写日报&签到噢,再不写就要罚款了:";
        notification.soundName = UILocalNotificationDefaultSoundName;
        notification.repeatInterval = NSCalendarUnitDay;
        NSLog(@"&**********%@",notification.fireDate);
        notification.alertAction = @"马上去做";
        NSMutableDictionary *infoDict = [[NSMutableDictionary alloc]init];
        [infoDict setValue:checksignAndDailywork.issign forKey:@"issign"];
        [infoDict setValue:checksignAndDailywork.isdailywork forKey:@"isdailywork"];
        notification.userInfo = infoDict;
          // 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 = NSDayCalendarUnit;  
  }  
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        
    }
    
}
2. 在appdelegate中实现部分代码
//收到通知后执行这个方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    if (application.applicationState == UIApplicationStateActive) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"日报&签到提醒" message:notification.alertBody delegate:self cancelButtonTitle:@"等会再写" otherButtonTitles:@"马上去做",nil];
        
        [alert show];
    }
    else
    {//添加通知中心进行跳转
        [[NSNotificationCenter defaultCenter] postNotificationName:@"signAndDailyPresentView" object:nil userInfo:notification.userInfo];
    }
    self.notificationDic = notification.userInfo;
    
    NSLog(@"^^^^^%@",notification);
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"1111");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"signAndDailyPresentView" object:nil userInfo:self.notificationDic];
}
3. //根据本地消息进行推送
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(signAndDailyPresentView:) name:@"signAndDailyPresentView" object:nil];
4.页面跳转
- (void)signAndDailyPresentView:(UILocalNotification *)notification
{
    [[UIApplication sharedApplication] cancelLocalNotification:notification];
    if ([[notification.userInfo objectForKey:@"issign"] isEqualToString:@"0"]) {

        self.topView.hidden = YES;
        //日历界面
        UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController * changepassVC = [storyboard instantiateViewControllerWithIdentifier:@"mySignViewController"];
        [self.navigationController pushViewController:changepassVC animated:YES];
    }
    else
    {
        self.topView.hidden = YES;
        if ([userInfo.dailywork isEqualToString:@"1"]) {
            
            self.endLabel.hidden = NO;
            
            [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(endLabelHidden) userInfo:nil repeats:YES];
            self.topView.hidden = NO;
            return;
        }
        UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        
        NSString *judgeRole = userInfo.role;
        if (([judgeRole rangeOfString:@"总经理"].location != NSNotFound) && ![judgeRole isEqualToString:@""]) {
            jingliribaoViewController * jingliviewcontroller = [storyboard instantiateViewControllerWithIdentifier:@"jingliribaoViewController"];
            [self.navigationController pushViewController:jingliviewcontroller animated:YES];
        }
        else{
            whiteDailyViewController * viewcontroller = [storyboard instantiateViewControllerWithIdentifier:@"whiteDailyViewController"];
            [self.navigationController pushViewController:viewcontroller animated:YES];
            
        }

        
    }
}


你可能感兴趣的:(ios 闹钟的总结-------使用的是本地通知。)