local alarm clock

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

Guess you like

Origin blog.csdn.net/baidu_33298752/article/details/51657439