第一步:创建本地推送
// 创建一个本地推送
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
//设置10秒之后
NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10];
if (notification != nil) {
    // 设置推送时间
    notification.fireDate = pushDate;
    // 设置时区
    notification.timeZone = [NSTimeZone defaultTimeZone];
    // 设置重复间隔
    notification.repeatInterval = kCFCalendarUnitDay;
    // 推送声音
    notification.soundName = UILocalNotificationDefaultSoundName;
    // 推送内容
    notification.alertBody = @"推送内容";
    //显示在icon上的红色圈中的数子
    notification.applicationIconBadgeNumber = 1;
    //设置userinfo 方便在之后需要撤销的时候使用
    NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"];
    notification.userInfo = info;
    //添加推送到UIApplication       
    UIApplication *app = [UIApplication sharedApplication];
    [app scheduleLocalNotification:notification]; 
   
}

第二步:接收本地推送
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
    // 图标上的数字减1
    application.applicationIconBadgeNumber -= 1;
}

第三步:解除本地推送
// 获得 UIApplication
UIApplication *app = [UIApplication sharedApplication];
//获取本地推送数组
NSArray *localArray = [app scheduledLocalNotifications];
//声明本地通知对象
UILocalNotification *localNotification;
if (localArray) {
    for (UILocalNotification *noti in localArray) {
        NSDictionary *dict = noti.userInfo;
        if (dict) {
            NSString *inKey = [dict objectForKey:@"key"];
            if ([inKey isEqualToString:@"对应的key值"]) {
                if (localNotification){
                    [localNotification release];
                    localNotification = nil;
                }
                localNotification = [noti retain];
                break;
            }
        }
    }
   
    //判断是否找到已经存在的相同key的推送
    if (!localNotification) {
        //不存在初始化
        localNotification = [[UILocalNotification alloc] init];
    }
   
    if (localNotification) {
        //不推送 取消推送
        [app cancelLocalNotification:localNotification];
        [localNotification release];
        return;
    }
}

最新文章

  1. C#按位操作,直接操作INT数据类型的某一位
  2. Linux修改SSH端口,并禁止Root远程登陆
  3. ADO.NET学习系列(一)
  4. ios7.1 在线安装 失败的解决办法
  5. tuple只有一个元素的时候,必须要加逗号
  6. HTML+CSS+JS简介
  7. win10 mysql详尽安装教程
  8. 移动端的silder,未封装,基于zepto的touch模块,有参照修改过touch的bug
  9. pypi镜像源加速第三方库在线安装
  10. c++ CreateProcess调用dos命令
  11. [CocoaPods]使用Gemfile
  12. P2799国王的魔镜
  13. 使用 IntraWeb (15) - 基本控件之 TIWEdit、TIWMemo、TIWText
  14. 有序数组每个数平方后,不同数字的个数?O(n)
  15. spring--mvc用戶注册用户名验重
  16. PHP7 学习笔记(二)PHP5.9 升级到PHP7 遇到的一些坑的记录(php-fpm 图解)
  17. js解析与序列化json数据(一)json.stringify()的基本用法
  18. vue 时间戳转 YYYY-MM-DD h:m:s
  19. loj #6261 一个人的高三楼 FFT + 组合数递推
  20. bzoj 1043 下落的圆盘 —— 求圆心角、圆周长

热门文章

  1. Linux系统内核制作和内核模块的基础
  2. C#入门教程(一)–.Net平台技术介绍、C#语言及开发工具介绍-打造C#学习教程
  3. 简单遗传算法求解n皇后问题
  4. 关于Class.forName("oracle.jdbc.driver.OracleDriver");报ClassNotFoundException 的异常
  5. Java Concurrency - 浅析 CountDownLatch 的用法
  6. Commons JXPath - Extension Functions
  7. Spring(3.2.3) - Beans(6): 作用域
  8. 找出文件正在被哪个windows进程使用的方法
  9. sql常识-FULL JOIN
  10. iOS - 基于蓝牙数据交换的环境监测(温度、湿度、光照、粉尘、噪声)