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

        
    }
}
相關文章
相關標籤/搜索