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

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

144 
作者 D_w 
2016.06.27 15:54* 字数 488 阅读 671评论 0

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

转载于:https://www.cnblogs.com/ios4app/p/6956784.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值