iOS Local Notification学习笔记

做了很多有Push Notification和Locale Notification的app,来整理下开发的笔记。今天先说说Local Notification

在iOS4.0后Apple加入了Local Notification。这里是NSLocaleNotification的Class Reference http://developer.apple.com/library/ios/#documentation/iPhone/Reference/UILocalNotification_Class/Reference/Reference.html

如何注册一个Notification

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

// 设置notification的属性
localNotification.fireDate = [startTimePicker.picker.date dateByAddingTimeInterval:36000]; //出发时间
localNotification.alertBody = @"Time To Schedule Our Service"; // 消息内容
localNotification.repeatInterval = NSSecondCalendarUnit; // 重复的时间间隔
localNotification.soundName = UILocalNotificationDefaultSoundName; // 触发消息时播放的声音
localNotification.applicationIconBadgeNumber = 1; //应用程序Badge数目

//设置随Notification传递的参数
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"reminder", @"notificationId", @"phone", txtPhone.text, nil];
localNotification.userInfo = infoDict;
    
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; //注册
[localNotification release]; //释放

遍历已经注册的所有LocaleNotification

    NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    for (UILocalNotification *notification in notifications ) {
        if( [[notification.userInfo objectForKey:@"source"] isEqualToString:@"dailyReminder"] ) {
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
            break;
        }
    }

撤销LocaleNotification

[[UIApplication sharedApplication] cancelAllLocalNotifications]; // 撤销所有的Notification
[[UIApplication sharedApplication] cancelLocalNotification:notification]; // 撤销某个Notificiation,若要删除某个特定的Notification,则可以在UserInfo中加入标记,遍历所有的Notification来删除。



响应事件

当Notification被触发后,你的应用需要对此作出反应。应用此时可能会处于以下几个状态:

  • 在前台运行 - 当应用在前台运行时,则ApplicationDelegate的didReceiveLocalNotification会被调用。
    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
        NSLog(@"Notification Body: %@", notification.alertBody);
        NSLog(@"%@", notification.userInfo);
        application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;
    }

  • 在后台运行 - 用户可以看到类似Push Notification的提醒,若用户选择查看提醒详情,则应用通过 ApplicationDelegate的didFinishLaunchingWithOptions进入
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        UILocalNotification *localNotification =
        [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        if (localNotification) {
            NSLog(@"Notification Body: %@",localNotification.alertBody);
            NSLog(@"%@", localNotification.userInfo);
            application.applicationIconBadgeNumber = localNotification.applicationIconBadgeNumber-1;
        }
        // set up everything else
        return YES;
    
    }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值