本地通知UILocalNotification

作者: 不会游泳De鱼 | 来源:发表于2016-06-16 20:35 被阅读1149次

    在应用中有的时候需要做一个类似于闹钟提醒的功能,在每周的特定的几天中,在固定时间进行提醒。比如每周一到周五下午六点,需要吃药的时间就弹窗提醒:快去吃药!不要放弃治疗!
    本地固定时间提醒,需要用到 UILocalNotification
    如果只是添加一次,并且设置提醒的周期为NSCalendarUnitWeekOfYear ,那么就是每周只提醒一次。所以如果要达到每周固定星期几提醒则需要添加提醒天的次数,比如周一到周五提醒那么就需要添加五次,每个提醒的间隔都为一个星期。这样就可以做到每周固定星期提醒了。

    for(id day in _weekArr) {// _weekArr 为周一到周日需要提醒的星期 
        UILocalNotification* beginLocalNotification = [[UILocalNotification alloc] init]; 
        NSDictionary* beginInfo = [NSDictionary dictionaryWithObject:kLocalNoti forKey:@"beginNoti"];
        beginLocalNotification.userInfo = beginInfo;
        beginLocalNotification.fireDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"2015-06-%d %@:00",15+[day integerValue]-1,_beginTime]];// 在过去的某个星期内添加需要的本地提醒。并且设置提醒间隔为NSCalendarUnitWeekOfYear,如果添加在未来,那么提醒不会发生。 
        beginLocalNotification.alertBody = @"该吃药啦!";
        beginLocalNotification.soundName = @"ping.caf"; 
        beginLocalNotification.timeZone = [NSTimeZone defaultTimeZone]; 
        beginLocalNotification.alertAction = @"不要忘记吃药哦!"; 
        beginLocalNotification.repeatInterval = NSCalendarUnitWeekOfYear;// 每周循环提醒
        //按照规定的时候触发
        [[UIApplication sharedApplication] scheduleLocalNotification:beginLocalNotification];
        //立即触发一个通知
        //[[UIApplication sharedApplication] presentLocalNotificationNow: beginLocalNotification]; 
    }
    

    取消提醒比较简单,如果想取消所有的本地提醒:

    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    

    取消特定的提醒:

    NSArray *narry=[[UIApplication sharedApplication] scheduledLocalNotifications]; 
    NSUInteger acount=[narry count]; 
    if (acount<1) { 
        return false; 
    } 
    for (int i=0; i<acount; i++) { 
        UILocalNotification *myUILocalNotification = [narry objectAtIndex:i]; 
        NSDictionary *userInfo = myUILocalNotification.userInfo; 
        NSString *obj = [userInfo objectForKey:@"beginNoti"]; 
        if ([obj isEqualToString @"kLocalNoti "]) { 
            [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification]; 
            return true; 
        } 
    }
    

    点击提醒
    App在前台或者在后台时,即运行时接收到本地推送消息。上面的2种情况的处理基本一致, 不同点只有当运行再后台的时候,会有弹窗提示用户另外一个App有通知,对于本地通知单的处理都是通过*AppDelegate的方法:- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification来处理的。

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"接收到本地提醒 in app" message:notification.alertBody delegate:nil  cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        //这里,你就可以通过notification的useinfo,干一些你想做的事情了
        application.applicationIconBadgeNumber -= 1;
    }
    

    App没有启动,点击通知。这种情况下,当点击通知时,会启动App,而在App中,开发人员可以通过实现*AppDelegate中的方法:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions,然后从lauchOptions中获取App启动的原因,若是因为本地通知,则可以App启动时对App做对应的操作。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    {  
        NSLog(@"Start App....");  
        ....  
        UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];  
        if ([self isWelcomeNotification:localNotification]) {  
            NSLog(@"start with welcome notification");  
            [self.mainViewController showOfficeIntroduction];  
        }  
        return YES;  
    }  
    

    repeatInterval的循环枚举

    
    typedef NS_OPTIONS(NSUInteger, NSCalendarUnit) {
            NSCalendarUnitEra                = kCFCalendarUnitEra,//一个年代、一个世纪循环一次。
            NSCalendarUnitYear               = kCFCalendarUnitYear,//每年循环一次
            NSCalendarUnitMonth              = kCFCalendarUnitMonth,//每月循环一次
            NSCalendarUnitDay                = kCFCalendarUnitDay,//每天循环一次
            NSCalendarUnitHour               = kCFCalendarUnitHour,//每小时循环一次
            NSCalendarUnitMinute             = kCFCalendarUnitMinute,//每分钟循环一次
            NSCalendarUnitSecond             = kCFCalendarUnitSecond,//每秒循环一次
            NSCalendarUnitWeekday            = kCFCalendarUnitWeekday,//平常日、工作日循环,但是周末也会。(*而不是每周循环,具体和NSCalendarUnitDay有什么区别,暂时没有测试出来,如果哪位知道请告知,感谢)
            NSCalendarUnitWeekdayOrdinal     = kCFCalendarUnitWeekdayOrdinal,
            NSCalendarUnitQuarter            NS_ENUM_AVAILABLE(10_6, 4_0) = kCFCalendarUnitQuarter,//一个季度循环一次
            NSCalendarUnitWeekOfMonth        NS_ENUM_AVAILABLE(10_7, 5_0) = kCFCalendarUnitWeekOfMonth,//每个月的第几周
            NSCalendarUnitWeekOfYear         NS_ENUM_AVAILABLE(10_7, 5_0) = kCFCalendarUnitWeekOfYear,//每年的第几周
            NSCalendarUnitYearForWeekOfYear  NS_ENUM_AVAILABLE(10_7, 5_0) = kCFCalendarUnitYearForWeekOfYear,
            NSCalendarUnitNanosecond         NS_ENUM_AVAILABLE(10_7, 5_0) = (1 << 15),
            NSCalendarUnitCalendar           NS_ENUM_AVAILABLE(10_7, 4_0) = (1 << 20),
            NSCalendarUnitTimeZone           NS_ENUM_AVAILABLE(10_7, 4_0) = (1 << 21),
    }
    

    相关文章

      网友评论

        本文标题:本地通知UILocalNotification

        本文链接:https://www.haomeiwen.com/subject/sijcdttx.html