iOS学习笔记11—本地通知UILocalNotification

 iOS学习笔记11—本地通知UILocalNotification 


本地通知,用于基于时间行为的通知。 操作系统负责提供在适当的时候分发本地通知给应用程序(注意通知不是由应用来分发),应用程序无需处于运行状态。与远程消息推送类似,本地通知也能够显示警告,发出声音,改变应用图标上的小数字。

本地通知主要用于基于定时器的行为、简单的日历、待办事项列表等应用场景。在允许的时间内在后台运行的应用也可以处理本地通知,如定期或即时通知用户传入的邮件,聊天,或更新等。

对本地通知的数量限制,iOS最多允许最近本地通知数量是64,超过限制的话,将只保留最近的64个,其余本地通知将被丢弃。

本地通知可以包括自定义的数据存储在一个字典。由于的UILocalNotification采用NSCopying协议,可以复制现有的本地通知,并对其进行修改。

 

一个简单的使用例子:

- (void)addNotification

{

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

    notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:2];

    notification.timeZone=[NSTimeZone defaultTimeZone];

    notification.alertBody=@"Hello, world!";

    notification.applicationIconBadgeNumber=1;

    notification.repeatInterval=kCFCalendarUnitMinute;

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}


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

{

    if (notification) {

        NSLog(@"didReceiveLocalNotification");

        UIAlertView *alert =  [[UIAlertView alloc] initWithTitle:nil message:@"received notification1" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [alert show];

        [alert release];

    }

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    [self addNotification];

    

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

    self.window.rootViewController = self.viewController;

    [self.window makeKeyAndVisible];

    

    return YES;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值