本地闹钟
2016-06-13 14:26
274 查看
#import <Foundation/Foundation.h> @interface LocalNotificationManager : NSObject + (instancetype)sharedInstance; /** * registerLocalNotification */ - (void)registLocalNotification; /** * createLocalNotification * * @param aRemind model of local event */ - (void)createLocalNotification:(TimeRemindModel *)model; /** * deleteLocalNotification * * @param aRemind model of local event */ - (void)deleteLocalNotification:(TimeRemindModel *)model; /** * deleteAllLocalNotification */ - (void)deleteAllLocalNotification; @end
#import "LocalNotificationManager.h" @implementation LocalNotificationManager + (instancetype)sharedInstance { static LocalNotificationManager *localNotificationManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ localNotificationManager = [[LocalNotificationManager alloc] init]; }); return localNotificationManager; } - (void)registLocalNotification { if (IOS_VERSION < 8.0) { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound]; } else { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; } } - (void)createLocalNotification:(TimeRemindModel *)model { NSString *hourstr = [model.remindTime substringToIndex:2]; NSString *minutestr = [model.remindTime substringFromIndex:3]; BNLog(@"create clockTime %@:%@",hourstr, minutestr); //取得与目标时间的间隔秒数 NSDate * now = [NSDate date]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *comps = [[NSDateComponents alloc] init]; NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; comps = [calendar components:unitFlags fromDate:now]; NSInteger hour = [comps hour]; NSInteger min = [comps minute]; NSInteger sec = [comps second]; NSInteger thour = [[model.remindTime substringToIndex:2] intValue]; NSInteger tmin = [[model.remindTime substringFromIndex:3] intValue]; NSInteger daynow = hour*3600 + min*60 + sec; NSInteger daytar = thour*3600 + tmin*60; NSInteger middle = daytar - daynow; NSInteger dayconst = 24*60*60; NSInteger interval = 0; if (middle <= 0) interval = dayconst + middle; else interval = middle; UILocalNotification *notification = [[UILocalNotification alloc] init]; //推送的最长显示时间是30s if (notification != nil) { //NSDateFormatter *formattrer = [[NSDateFormatter alloc] init]; //触发通知时间 notification.fireDate = [now dateByAddingTimeInterval:interval]; //设置默认时区 notification.timeZone = [NSTimeZone defaultTimeZone]; //通知重复提示的单位,可以是周(NSWeekdayCalendarUnit) 分钟(NSMinuteCalendarUnit) 秒(NSSecondCalendarUnit)月(NSMonthCalendarUnit)年(NSYearCalendarUnit) //提醒次数 if ([model.repeat isEqualToString:@"True"]) { notification.repeatInterval = kCFCalendarUnitDay; } else { notification.repeatInterval = 0; } //notification.repeatInterval = NSDayCalendarUnit; //通知触发时播放的声音 notification.soundName = UILocalNotificationDefaultSoundName; //notification.soundName = @"叮当.caf"; //应用的红色数字 //notification.applicationIconBadgeNumber = 1; //去掉下面2行就不会弹出提示框 //通知内容 if ([ZGTools isNullString:model.explain]) { if ([model.remindWay isEqualToString:@"fuyao"]) { notification.alertBody = @"服药时间"; } else { notification.alertBody = @"测量体温"; } } else { notification.alertBody = model.explain; } //提示框按钮 notification.alertAction = NSLocalizedString(@"View Details", nil); //是否显示额外的按钮,为no时alertAction消失 //notification.hasAction = NO; //添加额外的信息 notification.userInfo = [NSDictionary dictionaryWithObject:model.remindGUID forKey:kClockNotificationKey]; //执行通知注册 [[UIApplication sharedApplication] scheduleLocalNotification:notification]; } } - (void)deleteLocalNotification:(TimeRemindModel *)model { NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; //遍历找到对应nfkey和notificationtag的通知 for(UILocalNotification *myUILocalNotification in localNotifications) { NSString *remindGUID = [myUILocalNotification.userInfo objectForKey:kClockNotificationKey]; //BNLog(@"myUILocalNotification remindGUID = %@ model.remindGUID = %@",remindGUID, model.remindGUID); if ([remindGUID isEqualToString:model.remindGUID]) { //删除本地通知 BNLog(@"delete clockTime %@",model.remindTime); [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification]; break; } } } - (void)deleteAllLocalNotification { NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; for(UILocalNotification *myUILocalNotification in localNotifications) { [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification]; } }
相关文章推荐
- ios本地闹钟
- c++实践参考:教师兼干部类
- RecyclerView总结学习(一)
- 《C++精英内参之程序员高效指南》-17健身的意义
- 安卓全局异常捕获--自定义错误详情页
- SQL Server 发生错误:1807
- CSS3制作的一款按钮特效
- TP框架下钩子的实现
- children与parent
- windbg
- BZOJ2851 极限满月
- iOS应用程序国际化
- Web 前端开发的包管理工具 bower 快速入门
- C#中Winform动态调用Webservice的方法
- Android实现 通过手势随意缩放、移动ImageView图片
- Python 继承和多态
- 罗有仁创业计划书
- 自定义控件里的keydown方法无法捕获所有的按键消息的处理方法1
- 关于伪类,伪元素
- java内部类