美文网首页IOS 知识积累程序员iOS
本地通知UILocalNotification实现闹钟功能

本地通知UILocalNotification实现闹钟功能

作者: 浩杰ee | 来源:发表于2016-04-18 11:33 被阅读3021次

一、摘要

Notification是智能手机应用编程中非常常用的一种传递信息的机制,在iOS下分为本地和远程两种推送。本地的Notification由iOS下NotificationManager统一管理,只需要将封装好的本地Notification对象加入到系统Notification管理机制队列中,系统会在指定的时间激发本地Notification,应用只需设计好处理Notification的方法就完成了整个Notification流程了。

最近项目中需要的功能是,需要在每天特定的时间通知用户(可以看成简单的闹钟功能),以下内容解释基于闹钟的功能。

二、几个基本类及其解释

NSCalendar 日历

NSCalendar对世界上现存的常用的历法进行了封装,既提供了不同历法的时间信息,又支持日历的计算。
*1 初始化一个日历:

NSCalendar *calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];; //        NSGregorianCalendar 表示阳历

NSDateComponents

NSDateComponents将时间表示成适合人类阅读和使用的方式,通过NSDateComponents可以快速而简单地获取某个时间点对应的“年”,“月”,“日”,“时”,“分”,“秒”,“周”等信息。当然一旦涉及了年月日时分秒就要和某个历法绑定,因此NSDateComponents必须和NSCalendar一起使用,默认为公历。
初始化一个NSDateComponents:

  • 1 // 先定义一个遵循某个历法的日历对象

    NSCalendar *greCalendar = [[NSCalendar alloc]  initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [greCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit fromDate:[NSDate date]];
    

UILocalNotification

本地通知是由本地应用触发的,它是基于时间行为的一种通知形式,例如闹钟定时、待办事项提醒,又或者一个应用在一段时候后不使用通常会提示用户使用此应用等都是本地通知。创建一个本地通知通常分为以下几个步骤:

  • 1创建UILocalNotification。
  • 2设置处理通知的时间fireDate。
  • 3配置通知的内容:通知主体、通知声音、图标数字等。
  • 4配置通知传递的自定义数据参数userInfo(这一步可选)。
  • 5调用通知,可以使用scheduleLocalNotification:按计划调度一个通知,也可以使用presentLocalNotificationNow立即调用通知。

三、实现闹钟功能

*1
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

//如果已经获得发送通知的授权则创建本地通知,否则请求授权(注意:如果不请求授权在设置中是没有对应的通知设置项的,也就是说如果从来没有发送过请求,即使通过设置也打不开消息允许设置)
if ([[UIApplication sharedApplication]currentUserNotificationSettings].types!=UIUserNotificationTypeNone) {
    [self addLocalNotification];
}else{
    [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound  categories:nil]];
}

return YES;
}

*2 调用过用户注册通知方法之后执行(也就是调用完 registerUserNotificationSettings:方法之后执行)

-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{

if (notificationSettings.types!=UIUserNotificationTypeNone) {
    [self addLocalNotification];
}
}
  • 3
    - (void)addLocalNotification{
    NSCalendar *calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitEra |
    NSCalendarUnitYear |
    NSCalendarUnitMonth |
    NSCalendarUnitDay |
    NSCalendarUnitHour |
    NSCalendarUnitMinute |
    NSCalendarUnitSecond |
    NSCalendarUnitWeekOfYear |
    NSCalendarUnitWeekday |
    NSCalendarUnitWeekdayOrdinal |
    NSCalendarUnitQuarter;
    comps = [calendar components:unitFlags fromDate:[NSDate date]];
    for (int i = 0; i <100; i++) {
    NSDate newFireDate = [[calendar dateFromComponents:comps] dateByAddingTimeInterval:30i];
    UILocalNotification *newNotification = [[UILocalNotification alloc] init];
    if (newNotification) {
    newNotification.fireDate = newFireDate;
    newNotification.alertBody = @"哈哈";
    newNotification.soundName = @"呵呵";
    newNotification.applicationIconBadgeNumber=1;//应用程序图标右上角显示的消息数
    newNotification.alertAction = @"查看闹钟";
    newNotification.userInfo =@{@"id":@1,@"user":@"Kenshin Cui"};//绑定到通知上的其他附加信息;
    [[UIApplication sharedApplication] scheduleLocalNotification:newNotification];
    }
    }
    }

*4 进入前台后设置消息信息

-(void)applicationWillEnterForeground:(UIApplication *)application{
[[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];//进入前台取消应用消息图标
  }

*5 移除本地通知,在不需要此通知时记得移除

-(void)removeNotification{
 [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

四、小结

第一次写博客,希望以后能够坚持下去,fighting!

相关文章

网友评论

  • PGOne爱吃饺子:你好 请问一下啊,比如说设置每周一上午七点闹钟提醒,请问后台怎么执行这个方法,我一开始的时候设置闹钟的时候是开了一个定时器,让定时器不停走,然后时间做比较,时间到了就提醒。请问你的这个原理是怎么实现的,谢谢
    浩杰ee:@PGOne爱吃饺子 是的,加入本地通知,就是自己的iphone 系统会发送本地通知的
    PGOne爱吃饺子:是的 定时器肯定是不行的,我想问一下你的这个加入本地通知的方式是怎么实现的,是不是程序加入了本地通知后,程序会在那个时间段发送通知。
    浩杰ee:@PGOne爱吃饺子 你的方法不可行吧,app杀死以后,你的定时器就不走了。正确的方法是,加入系统的schedule,这样不管app有没有杀死,到了你设定的时间点,都会响的
  • 丶狂舞:能不能发我一份demo,827768028@qq.com,谢谢了
  • Sunshine_an:能不能发我一份demo,1437050181@qq.com,谢谢了
    丶狂舞:@浩杰ee 顺便发我一份吧,827768028@qq.com,谢谢了
    浩杰ee:我找一下
  • feng_dev:如何实现 在触发 本地通知的时候 播放自定义 MP3 啊?
    随意_M:@伤感的小孩 你好,这个问题有答案了吗?同问
    feng_dev:@浩杰ee 是不是 要想播放声音 必须 在后台啊,就是 点击了 home 之后,才能有播放的声音,caf 格式的,如果想在 app 前台 播放,怎么办
    浩杰ee:@伤感的小孩 有一个参数可以设置成你想要播放的音乐的
  • 异乡有悟:如果我设置了一个起床闹钟,然后想让它每周四 周五 推送给我,这样NSCalendar可以做吗
    浩杰ee:@新_1740 可以的
    新_1740:设置类似每周四,每周五推送的实现了吗
    浩杰ee:@异乡有悟 可以的
  • __________mo:赞一个~
  • 毕竟我是个大仙儿:第一次能写成这样 真的很棒 加油!

本文标题:本地通知UILocalNotification实现闹钟功能

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