iOS本地推送与取消本地通知—UILocalNotification的使用

1.首先我们初始化一个 UISwith

    self.swith = [[UISwitch alloc] initWithFrame:CGRectMake(80, 80, 160, 30)];
    [_swith addTarget:self action:@selector(doLocalNotifition) forControlEvents:UIControlEventValueChanged];
    [_swith setOn:NO];
    [self.view addSubview:_swith];

2.实现UISwith的方法

- (void)doLocalNotifition
{
    if (_swith.isOn==YES) {
        //初始化一个 UILocalNotification
        UILocalNotification * notification = [[UILocalNotification alloc] init];
        NSDate * pushDate = [NSDate dateWithTimeIntervalSinceNow:10.0];
        if (notification!=nil) {
            
            //设置 推送时间
            notification.fireDate= pushDate;
            //设置 时区
            notification.timeZone = [NSTimeZone defaultTimeZone];
            //设置 重复间隔
            notification.repeatInterval = kCFCalendarUnitDay;
            //设置 推送 时间
            notification.soundName = UILocalNotificationDefaultSoundName;
            //设置 推送提示语
            notification.alertBody = @"提示框内容5";
            //设置 icon 上 红色数字
            notification.applicationIconBadgeNumber = 1;
            //取消 推送 用的 字典  便于识别
            NSDictionary * inforDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
            notification.userInfo =inforDic;
            //添加推送到 Application
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        }
        NSLog(@"开启本地通知");
    }else if(_swith.isOn==NO){
        
        //拿到 存有 所有 推送的数组
        NSArray * array = [[UIApplication sharedApplication] scheduledLocalNotifications];
        //便利这个数组 根据 key 拿到我们想要的 UILocalNotification
        for (UILocalNotification * loc in array) {
            if ([[loc.userInfo objectForKey:@"key"] isEqualToString:@"name"]) {
                //取消 本地推送
                [[UIApplication sharedApplication] cancelLocalNotification:loc];
            }
        }

        NSLog(@"关闭本地通知");
    }
}

代码地址:http://download.csdn.net/detail/u012405234/6432113




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值