#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];
if (notification != nil) {
notification.fireDate = [now dateByAddingTimeInterval:interval];
notification.timeZone = [NSTimeZone defaultTimeZone];
if ([model.repeat isEqualToString:@"True"]) {
notification.repeatInterval = kCFCalendarUnitDay;
} else {
notification.repeatInterval = 0;
}
notification.soundName = UILocalNotificationDefaultSoundName;
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);
notification.userInfo = [NSDictionary dictionaryWithObject:model.remindGUID forKey:kClockNotificationKey];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
- (void)deleteLocalNotification:(TimeRemindModel *)model
{
NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
for(UILocalNotification *myUILocalNotification in localNotifications) {
NSString *remindGUID = [myUILocalNotification.userInfo objectForKey:kClockNotificationKey];
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];
}
}