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

来源:互联网 发布:java汽车租赁项目 编辑:程序博客网 时间:2024/05/29 10:42

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