A UILocalNotification object specifies a notification that an app can schedule for presentation
at a specific date and time. The operating system is responsible for delivering local notifications
at their scheduled times; the app does not have to be running for this to happen. Although local
notifications are similar to remote notifications in that they are used for displaying alerts,
playing sounds, and badging app icons, they are composed and delivered locally and do not require
connection with remote servers.
A UILocalNotification object specifies a notification that an app can schedule for presentation
at a specific date and time. The operating system is responsible for delivering local notifications
at their scheduled times; the app does not have to be running for this to happen. Although local
notifications are similar to remote notifications in that they are used for displaying alerts,
playing sounds, and badging app icons, they are composed and delivered locally and do not require
connection with remote servers.
上述的api描述了本地通知(UILocalNotification)的定义,本地通知是有本地App触发,基于时间来进行通知。
功能描述:
界面做个time picker 选择时间后,点击测试后,到了选定的时间后,界面会推送出消息
1、首先设计界面,拖出两个控件,Date Picker 和 Button。
2、在AppDelgate配置相关通知授权信息
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//如果已经获得发送通知的授权则创建本地通知,否则请求授权
if (![[UIApplication sharedApplication]currentUserNotificationSettings].types!=UIUserNotificationTypeNone) {
[[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
return YES;
}
#pragma mark 进入前台后设置消息信息
-(void)applicationWillEnterForeground:(UIApplication *)application{
[[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];//进入前台取消应用消息图标
}
3、在ViewController中配置相关时间和编写事件以及通知信息
(1)设置时间控件
- (IBAction)clickShow:(id)sender {
//获取用户设置的日期和时间
NSDate *selected = [self.timePicker date];
[self addLocalNotification:selected];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
//设置日期格式为字符串
[dateFormatter setDateFormat:@"HH mm"];
//格式化
NSString *dateString = [dateFormatter stringFromDate:selected];
NSString *message = [NSString stringWithFormat:
@"您选择的日期和时间是:%@", dateString];
// 创建一个UIAlertView对象(警告框),并通过该警告框显示用户选择的日期、时间
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"日期和时间"
message:message
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
// 显示UIAlertView
[alert show];
}
(2)设置通知的相关消息
-(void)addLocalNotification:(NSDate *)fireDate{
//定义本地通知对象
UILocalNotification *notification=[[UILocalNotification alloc]init];
//设置调用时间
notification.fireDate = fireDate;
notification.repeatInterval=NSCalendarUnitHour;//通知重复次数
notification.repeatCalendar=[NSCalendar currentCalendar];//当前日历,使用前最好设置时区等信息以便能够自动同步时间
//设置通知属
notification.alertBody=@"这个是定时器出发的。。。"; //通知主体
notification.applicationIconBadgeNumber=1;//应用程序图标右上角显示的消息数
notification.alertAction=@"打开"; //待机界面的滑动动作提示
notification.alertLaunchImage=@"Default";//通过点击通知打开应用时的启动图片,这里使用程序启动图片
notification.soundName=UILocalNotificationDefaultSoundName;//收到通知时播放的声音,默认消息声音,如果是自定义则需要注意格式
//调用通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
------------------------分割-------------------
今天用真机测试了下,后来发现有个问题,就是自定义铃声那一块的。首先是是需要将资源加入到项目中,然后一定过要检查Targets/Build Phaswa/CopyResourceBundle中是否有相关的资源放进去
然后定时器中的音乐名称要对应
notification.soundName=@"kakao.caf";//通知声音(需要真机才能听到声音) //调用通知
------------------------继续分割-------------------
关于时间间隔设置
notification.repeatInterval=NSCalendarUnitHour
设置时间间隔的,比如 NSCalendarUnitHour 是 每个小时的固定时间(设置的firedate为准) 经行通知提醒。
这个repeatInterval 值的设定是枚举 有NSCalendarUnitSecond,NSCalendarUnitMinute....等等默认值为0 是不重复,这些都是可以看api查到的,
由于是枚举值,本地通知的时间间隔是不支持自定义的。而且本地通知时间间隔是不支持小于等于一分钟的,所以时间间隔的下限是NSCalendarUnitMinute。
如果你想每隔10分钟发送一次本地通知。是不能实现了 除非 你设置6个不同的通知,时间间隔为10,20,30,40,50,60 并且notification.repeatInterval=NSCalendarUnitHour。