面试集锦iOS知识专题

iOS开发本地通知(每天3点半发送消息推送)

2016-06-27  本文已影响2268人  wwwwwwww1

NSDate *now = [NSDate date];
//取得系统时间
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
components = [calendar components:unitFlags fromDate:now];
NSInteger hour = [components hour];
NSInteger min = [components minute];
NSInteger sec = [components second];
NSInteger week = [components weekday];
NSLog(@"现在是%ld:%ld:%ld,周%ld",hour,min,sec,week);

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//设置时区(跟随手机的时区)
localNotification.timeZone = [NSTimeZone defaultTimeZone];
if (localNotification) {
    localNotification.alertBody = @"消息推送消息推送";
    localNotification.alertAction = @"打开";
    //小图标数字
    localNotification.applicationIconBadgeNumber = 0;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSDate *date = [formatter dateFromString:@"15:30:00"];
    //通知发出的时间
    localNotification.fireDate = date;
}
//循环通知的周期
localNotification.repeatInterval = kCFCalendarUnitDay;

//设置userinfo方便撤销
NSDictionary *info = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
localNotification.userInfo = info;
//启动任务
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
 */
/*
for (UILocalNotification *obj in [UIApplication sharedApplication].scheduledLocalNotifications) {
    if ([obj.userInfo.allKeys containsObject:kLocalNotificationKey]) {
        [[UIApplication sharedApplication] cancelLocalNotification:obj];
    }
}




// 设置一个按照固定时间的本地推送
NSDate *now = [NSDate date];
//取得系统时间
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
components = [calendar components:unitFlags fromDate:now];
NSInteger hour = [components hour];
NSInteger min = [components minute];
NSInteger sec = [components second];
NSInteger week = [components weekday];
WDLog(@"现在是%ld:%ld:%ld,周%ld",hour,min,sec,week);

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//设置时区(跟随手机的时区)
localNotification.timeZone = [NSTimeZone defaultTimeZone];
if (localNotification) {
    localNotification.alertBody = @"消息推送消息推送";
    localNotification.alertAction = @"打开";
    //小图标数字
    localNotification.applicationIconBadgeNumber = 0;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSDate *date = [formatter dateFromString:@"15:30:00"];
    //通知发出的时间
    localNotification.fireDate = date;
}
//循环通知的周期
localNotification.repeatInterval = kCFCalendarUnitDay;

//设置userinfo方便撤销
NSDictionary *info = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
localNotification.userInfo = info;
//启动任务
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
 */
/*
for (UILocalNotification *obj in [UIApplication sharedApplication].scheduledLocalNotifications) {
    if ([obj.userInfo.allKeys containsObject:kLocalNotificationKey]) {
        [[UIApplication sharedApplication] cancelLocalNotification:obj];
    }
}
//直接取消全部本地通知
//[[UIApplication sharedApplication] cancelAllLocalNotifications];
上一篇下一篇

猜你喜欢

热点阅读