iOS本地通知

来源:互联网 发布:linux模糊查询文件 编辑:程序博客网 时间:2024/05/09 17:09

RT:http://www.cocoachina.com/newbie/tutorial/2012/0104/3827.html



本地通知 

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

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

  • scheduled time,时间周期,用来指定iOS系统发送通知的日期和时间;
  • notification type,通知类型,包括警告信息、动作按钮的标题、应用图标上的badge(数字标记)和播放的声音;
  • 自定义数据,本地通知可以包含一个dictionary类型的本地数据。

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

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

示例写的很简单,启动应用后,就发出一个定时通知,10秒后启动。这时按Home键退出,一会儿就会提示上图的提示信息。如果应用不退出则无效。

代码如下:

UILocalNotification *notification=[[UILocalNotification alloc] init]; 
if (notification!=nil) { 
    NSLog(@">> support local notification"); 
    NSDate *now=[NSDate new]; 
    notification.fireDate=[now addTimeInterval:10]; 
    notification.timeZone=[NSTimeZone defaultTimeZone]; 
    notification.alertBody=@"该去吃晚饭了!"; 
    [[UIApplication sharedApplication]   scheduleLocalNotification:notification];

 

更详细的代码见官方文档:《Scheduling, Registering, and Handling Notifications》,可以设置比如声音,比如用户定义数据等。

设置更多本地通知的信息:

 

  • 设置icon上数字。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    // Override point for customization after application launch. 
    ///////////// 
  application.applicationIconBadgeNumber = 0; 
    // Add the view controller’s view to the window and display. 
    [self.window addSubview:viewController.view]; 
    [self.window makeKeyAndVisible];

    return YES; 
}

  • 添加通知时间,通知类型,取消通知

#pragma mark – 
#pragma mark onChageValue 
-(IBAction)onChangeValue:(id)sender 

    UISwitch *switch1=(UISwitch *)sender; 
    if (switch1.on) { 
        UILocalNotification *notification=[[UILocalNotification alloc] init]; 
        NSDate *now1=[NSDate date];  
        notification.timeZone=[NSTimeZone defaultTimeZone]; 
        notification.repeatInterval=NSDayCalendarUnit; 
        notification.applicationIconBadgeNumber = 1; 
        notification.alertAction = NSLocalizedString(@"显示", nil); 
        switch (switch1.tag) { 
            case 0: 
            { 
                notification.fireDate=[now1 dateByAddingTimeInterval:10]; 
                notification.alertBody=self.myLable1.text; 
            } 
                break; 
            case 1: 
            { 
                notification.fireDate=[now1 dateByAddingTimeInterval:20]; 
                notification.alertBody=self.myLable2.text; 
            } 
                break; 
            case 2: 
            { 
                notification.fireDate=[now1 dateByAddingTimeInterval:30]; 
                notification.alertBody=self.myLable3.text; 
            } 
                break; 
            default: 
                break; 
        } 
        [notification setSoundName:UILocalNotificationDefaultSoundName]; 
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: 
                              [NSString stringWithFormat:@"%d",switch1.tag], @"key1", nil]; 
        [notification setUserInfo:dict]; 
        [[UIApplication sharedApplication]   scheduleLocalNotification:notification]; 
    }else { 
        NSArray *myArray=[[UIApplication sharedApplication] scheduledLocalNotifications]; 
        for (int i=0; i<[myArray count]; i++) { 
            UILocalNotification    *myUILocalNotification=[myArray objectAtIndex:i]; 
            if ([[[myUILocalNotification userInfo] objectForKey:@"key1"] intValue]==switch1.tag) { 
                [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification]; 
            } 
        } 
    } 
}

原创粉丝点击