本地推送通知UILocalNotification

http://www.brighttj.com/ios/ios-uiocalnotification.html

本地推送通知(UILocalNotification)其实也是通知的一种,系统能在具体的时间触发它,而不用App来触发。本地通知触发后,会显示在通知中心中,并根据配置,显示横幅与声音。

本地通知在项目中的运用很有特点,大多都和时间相关。如:备忘录、闹钟(自定义闹钟)、生日提醒等等。

文本将讲解如何简单使用本地通知,并在不需要的时候,进行通知移除。

本文Demo下载地址:

https://github.com/saitjr/UILocalNotificationDemo.git

环境信息:

Mac OS X 10.10.3

Xcode 6.3

iOS 8.3

正文:

一、创建工程

创建一个SingleViewApplication,并将一个UISwitch控件拖到StoryBoard中,添加居中约束。(添加完以后,注意点击Add 2 Constraints

放入UISwitch并添加约束

放入UISwitch并添加约束

二、为UISwitch关联事件

关联UISwitch事件

关联UISwitch事件

设置UISwitch时间

设置UISwitch时间

三、给ViewController添加注册/移除本地通知的两个方法


- (void)scheduleLocalNotification {
    
    
}

- (void)removeLocalNotification {
    
    
}

四、在UISwitch的响应事件中,对两个方法进行调用


- (IBAction)switchTapped:(id)sender {
    
    UISwitch *switcher = (UISwitch *)sender;
    
    if (switcher.on) {
        
        [self scheduleLocalNotification];
        return;
    }
    [self removeLocalNotification];
}

五、注册本地通知


- (void)scheduleLocalNotification {
    
    // 初始化本地通知
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    
    // 通知触发时间
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
    // 触发后,弹出警告框中显示的内容
    localNotification.alertBody = @"弹出警告框中显示的内容";
    // 触发时的声音(这里选择的系统默认声音)
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    // 触发频率(repeatInterval是一个枚举值,可以选择每分、每小时、每天、每年等)
    localNotification.repeatInterval = NSCalendarUnitDay;
    // 需要在App icon上显示的未读通知数(设置为1时,多个通知未读,系统会自动加1,如果不需要显示未读数,这里可以设置0)
    localNotification.applicationIconBadgeNumber = 1;
    // 设置通知的id,可用于通知移除,也可以传递其他值,当通知触发时可以获取
    localNotification.userInfo = @{@"id" : @"notificationId1"};
    
    // 注册本地通知
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

六、接收本地通知

当本地通知触发后,需要在AppDelegate中进行接收并做相应处理。在AppDelegate中,实现以下方法:


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:notification.alertBody delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
    [alert show];
}

七、移除本地通知

移除本地通知的方法有两种,一种是全部移除:


[[UIApplication sharedApplication] cancelAllLocalNotifications];

一种是按照id来移除,这个id就是在注册通知时,写的id:


- (void)removeLocalNotification {
    
    // 取出全部本地通知
    NSArray *notifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    // 设置要移除的通知id
    NSString *notificationId = @"notificationId1";
    
    // 遍历进行移除
    for (UILocalNotification *localNotification in notifications) {
        
        // 将每个通知的id取出来进行对比
        if ([[localNotification.userInfo objectForKey:@"id"] isEqualToString:notificationId]) {
            
            // 移除某一个通知
            [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
        }
    }
}

八、注意事项

1. 如果需要做一个闹钟或备忘录之类的应用,则将时间作为fireDate,触发之后可以进行移除;

2. 一个App中,本地通知数量最多64个,不用的通知要及时移除;

3. 如果通知的时间是后台返回的,那每次需要将通知全部移除,再添加新的。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值