当前位置:编程学习 > wap >>

IOS实现本地通知

iOS实现本地通知
本地通知,local notification,用于基于时间行为的通知,比如有关日历或者todo列表的小应用。另外,应用如果在后台执行,iOS允许它在受限的时间内运行,它也会发现本地通知有用。比如,一个应用,在后台运行,向应用的服务器端获取消息,当消息到达时,比如下载更新版本的提示消息,通过本地通知机制通知用户。

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

scheduled time,时间周期,用来指定iOS系统发送通知的日期和时间;
notification type,通知类型,包括警告信息、动作按钮的标题、应用图标上的badge(数字标记)和播放的声音;
自定义数据,本地通知可以包含一个dictionary类型的本地数据。
对本地通知的数量限制,iOS最多允许最近本地通知数量是64个,超过限制的本地通知将被iOS忽略。

如果就写个简单的定时提醒,是很简单的,比如这样:

 \
 

 

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

    if (localNotif == nil)

        return;

    //localNotif.fireDate = itemDate;

    //NSDate * now = [NSDate new];

    NSDate * date1 = [NSDate dateWithTimeInterval:8 sinceDate:date];

    localNotif.fireDate = date1;

    NSLog(@"now = %@",date1);

    localNotif.timeZone = [NSTimeZone defaultTimeZone];


// Notification details

    localNotif.alertBody = @"吃饭时间到了,通知提醒 。。。。。。。。";

// Set the action button

    localNotif.alertAction = @"View";


    localNotif.soundName = UILocalNotificationDefaultSoundName;

    localNotif.applicationIconBadgeNumber = 1;


// Specify custom data for the notification

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];

    localNotif.userInfo = infoDict;


// Schedule the notification

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

    [localNotif release];

   
//    NSArray * array = [[UIApplication sharedApplication] scheduledLocalNotifications];

//    [[UIApplication sharedApplication] cancelAllLocalNotifications];

//    [[UIApplication sharedApplication] cancelLocalNotification:localNotif];

   

 

 

补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,