UILocalNotification

UILocalNotification *notification=[[UILocalNotification alloc] init];app

 2         if (notification!=nil) {ide

 3             NSDate *now = [NSDate date];spa

 4             //从如今开始,10秒之后通知it

 5             notification.fireDate=[now dateByAddingTimeInterval:10];io

 6             //使用本地时区class

 7             notification.timeZone=[NSTimeZone defaultTimeZone];test

 8             notification.alertBody=@"顶部提示内容,通知时间到啦";后台

 9             //通知提示音 使用默认的object

10             notification.soundName= UILocalNotificationDefaultSoundName;date

11             notification.alertAction=NSLocalizedString(@"你锁屏啦,通知时间到啦", nil);

12             //这个通知到时间时,你的应用程序右上角显示的数字。

13             notification.applicationIconBadgeNumber = 1;

14             //add key  给这个通知增长key 便于半路取消。nfkey这个key是我本身随便起的。

15             // 假如你的通知不会在还没到时间的时候手动取消 那下面的两行代码你能够不用写了。

16             NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber            numberWithInt:notificationtag],@"nfkey",nil];

17             [notification setUserInfo:dict];

18             //启动这个通知

19             [[UIApplication sharedApplication]   scheduleLocalNotification:notification];

20             //这句真的特别特别重要。若是不加这一句,通知到时间了,发现顶部通知栏提示的地方有了,而后你经过通知栏进去,而后你发现通知栏里边还有这个提示

21             //除非你手动清除,这固然不是咱们但愿的。加上这一句就行了。网上不少代码都没有,就比较郁闷了。

22             [notification release];

23         }

日程提醒功能,本地通知的功能,记录相关知识以下:

一、本地通知的定义和使用:

本地通知是UILocalNotification的实例,主要有三类属性:

scheduled time,时间周期,用来指定iOS系统发送通知的日期和时间;

notification type,通知类型,包括警告信息、动做按钮的标题、应用图标上的badge(数字标记)和播放的声音;

自定义数据,本地通知能够包含一个dictionary类型的本地数据。

对本地通知的数量限制,iOS最多容许最近本地通知数量是64个,超过限制的本地通知将被iOS忽略。

 代码以下 复制代码

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    if (localNotification == nil) {

        return;

    }

    //设置本地通知的触发时间(若是要当即触发,无需设置),这里设置为20妙后

    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:20];

    //设置本地通知的时区

    localNotification.timeZone = [NSTimeZone defaultTimeZone];

    //设置通知的内容

    localNotification.alertBody = affair.title;

    //设置通知动做按钮的标题

    localNotification.alertAction = @"查看”;

    //设置提醒的声音,能够本身添加声音文件,这里设置为默认提示声

    localNotification.soundName = UILocalNotificationDefaultSoundName;

    //设置通知的相关信息,这个很重要,能够添加一些标记性内容,方便之后区分和获取通知的信息

    NSDictionary *infoDic = [NSDictionary dictionaryWithObjectsAndKeys:LOCAL_NOTIFY_SCHEDULE_ID,@"id",[NSNumber numberWithInteger:time],@"time",[NSNumber numberWithInt:affair.aid],@"affair.aid", nil];

    localNotification.userInfo = infoDic;

    //在规定的日期触发通知

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    

    //当即触发一个通知

//    [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

    [localNotification release];

二、取消本地通知:

 代码以下 复制代码

//取消某一个通知

    NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];

    //获取当前全部的本地通知

    if (!notificaitons || notificaitons.count <= 0) {

        return;

    }

    for (UILocalNotification *notify in notificaitons) {

        if ([[notify.userInfo objectForKey:@"id"] isEqualToString:LOCAL_NOTIFY_SCHEDULE_ID]) {

            //取消一个特定的通知

            [[UIApplication sharedApplication] cancelLocalNotification:notify];

            break;

        }

    }

    

    //取消全部的本地通知

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

三、本地通知的响应:

若是已经注册了本地通知,当客户端响应通知时:

    a、应用程序在后台的时候,本地通知会给设备送达一个和远程通知同样的提醒,提醒的样式由用户在手机设置中设置

    b、应用程序正在运行中,则设备不会收到提醒,可是会走应用程序delegate中的方法:

 代码以下 复制代码

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {  

     

,若是你想实现程序在后台时候的那种提醒效果,能够在上面这个方法中添加相关代码,示例代码:

 代码以下 复制代码

if ([[notification.userInfo objectForKey:@"id"] isEqualToString:@"affair.schedule"]) {  

       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:notification.alertBody delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:notification.alertAction, nil nil];  

       [alert show];  

   } 

须要注意的是,在状况a中,若是用户点击提醒进入应用程序,也会执行收到本地通知的回调方法,这种状况下若是你添加了上面那段代码,则会出现连续出现两次提示,为了解决这个问题,修改代码以下:

 代码以下 复制代码

if ([[notification.userInfo objectForKey:@"id"] isEqualToString:@"affair.schedule"]) {  

       //判断应用程序当前的运行状态,若是是激活状态,则进行提醒,不然不提醒  

       if (application.applicationState == UIApplicationStateActive) {  

           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:notification.alertBody delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:notification.alertAction, nil nil];  

           [alert show];  

       }  

   } 

相关文章
相关标签/搜索