iOS初体验---通知

本文详细介绍了本地通知(UILocalNotification)的定义、功能描述及实现步骤,并解决了自定义铃声的问题,同时提供了时间间隔设置的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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。






参照了这篇博文,博主写的非常详细

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值