zoukankan      html  css  js  c++  java
  • iOS本地通知:UILocalNotification

    最近在做一个电商的APP,话说今年电商很火啊。 用到了本地通知,特此整理一下

    添加一个本地通知到系统中,代码如下:

    // 初始化本地通知对象
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    if (notification) {
        // 设置通知的提醒时间
        NSDate *currentDate   = [NSDate date];
        notification.timeZone = [NSTimeZone defaultTimeZone]; // 使用本地时区
        notification.fireDate = [currentDate dateByAddingTimeInterval:5.0];
         
        // 设置重复间隔
        notification.repeatInterval = kCFCalendarUnitDay;
         
        // 设置提醒的文字内容
        notification.alertBody   = @"Wake up, man";
        notification.alertAction = NSLocalizedString(@"该吃药了", nil);
         
        // 通知提示音 使用默认的
        notification.soundName= UILocalNotificationDefaultSoundName;
         
        // 设置应用程序右上角的提醒个数
        notification.applicationIconBadgeNumber++;
         
        // 设定通知的userInfo,用来标识该通知
        NSMutableDictionary *aUserInfo = [[NSMutableDictionary alloc] init];
        aUserInfo[kLocalNotificationID] = @"LocalNotificationID";
        notification.userInfo = aUserInfo;
         
        // 将通知添加到系统中
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
    

    在APPDelegate.m中 加入接受本地通知的代码    :

    在收到通知后,调用程序委托中的下列方法处理://当然方法中可以是跳转到指定页面等方式,这里是一个弹出框

    -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
        NSLog(@"Application did receive local notifications");
         
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"welcome" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }

    最近看大牛的博客,说是本地通知在APP卸载后,还会存在系统中,

    可以看一下,打印一下所有的,验证一下

    1
    2
    NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    NSLog(@"%@", localNotifications);
    

    那么怎么删除本地通知呢,大牛也给出了方法

    取消方法分为两种。

    第一种比较暴力,直接取消所有的本地通知:

    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    

     第二种方法是针对某个特定通知的:

    - (void)cancelLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);
    

     这时就需要通知有一个标识,这样我们才能定位是哪一个通知。可以在notification的userInfo(一个字典)中指定。

    例如:

    -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
        NSLog(@"Application did receive local notifications");
         
        // 取消某个特定的本地通知
        for (UILocalNotification *noti in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
            NSString *notiID = noti.userInfo[kLocalNotificationID];
            NSString *receiveNotiID = notification.userInfo[kLocalNotificationID];
            if ([notiID isEqualToString:receiveNotiID]) {
                [[UIApplication sharedApplication] cancelLocalNotification:notification];
                return;
            }
        }
         
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"welcome" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    
  • 相关阅读:
    SQL Server 2008 R2下载地址
    [转]sqlserver2014两台不同服务器上数据库同步
    笔记本(ThinkPad)怎样关闭触摸板
    10 Free Image Hosting Sites for Your Photos
    Photobucket不能用了怎么办?推荐10个在线图片储存服务!
    证书错误 导航已阻止 无法跳转 最终解决
    用websploit获取管理员后台地址
    10 Free Image Hosting Sites for Your Photos
    Css3动画属性总汇
    23个适合logo设计的常用英文字体
  • 原文地址:https://www.cnblogs.com/KingQiangzi/p/4561711.html
Copyright © 2011-2022 走看看