iOS之Local Notification(本土通知)
来源:互联网 发布:linux alien 编辑:程序博客网 时间:2024/05/16 23:47
本文转自:http://www.myexception.cn/operating-system/1602805.html
iOS之Local Notification(本地通知)·
Local Notification的作用Local Notification(本地通知) :是根据本机状态做出的通知行为,因此,凡是仅需依赖本机状态即可判断需要发出通知的行为都可以或者说应该使用Local Notification来处理。比方说:iBeacon中进入了某个Beacon region,或者说自定义的一些定时提醒等。
构建Local Notification
在iOS中,构建LocalNotification非常简单,只需要掌握好NSLocalNotification这个类就够用了,基本步骤如下:
1. 创建并初始化 UILocalNotification对象
2. 配置该对象的属性:
* 触发时间(fireDate,timeZone,repeatInterval,repeatCalendar),如果你想根据时间来触发。
* 通知行为(alertAction,hasAction),定义查看通知的操作名。
* 触发通知时的启动画面(alertLaunchImage)
* 通知的正文内容(alertBody),
* 通知的背景声(soundName)
* 通知消息数的展示(applicationIconBadgeNumber),就是强迫症们最讨厌的App图标上左上角的那个小数字
* 其它(userInfo),可以给通知绑定一些处理通知时需要的额外信息。
3.展示通知,展示通知有两个方式:
* - (void)scheduleLocalNotification:(UILocalNotification *)notification:根据触发时间的配置展示通知消息,
* - (void)presentLocalNotificationNow:(UILocalNotification *)notification:立即发送通知到本机
栗子:
- (void)showNotificationWithAction:(NSString *)action andContent:(NSString *)content{ UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.alertBody = content; notification.alertAction = action; notification.soundName = UILocalNotificationDefaultSoundName; [[UIApplication sharedApplication] presentLocalNotificationNow:notification];}- (void)scheduleNotificationWithItem:(ToDoItem *)item interval:(int)minutesBefore { NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; NSDateComponents *dateComps = [[NSDateComponents alloc] init]; [dateComps setDay:item.day]; [dateComps setMonth:item.month]; [dateComps setYear:item.year]; [dateComps setHour:item.hour]; [dateComps setMinute:item.minute]; NSDate *itemDate = [calendar dateFromComponents:dateComps]; UILocalNotification *localNotif = [[UILocalNotification alloc] init]; if (localNotif == nil) return; localNotif.fireDate = [itemDate dateByAddingTimeIntervalInterval:-(minutesBefore*60)]; localNotif.timeZone = [NSTimeZone defaultTimeZone]; localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i minutes.", nil), item.eventName, minutesBefore]; localNotif.alertAction = NSLocalizedString(@"View Details", nil); localNotif.soundName = UILocalNotificationDefaultSoundName; localNotif.applicationIconBadgeNumber = 1; NSDictionary *infoDict = [NSDictionary dictionaryWithObject:item.eventName forKey:ToDoItemKey]; localNotif.userInfo = infoDict; [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];}
处理Local Notification
在处理本地通知时,我们需要考虑三种情况:
1. App没有启动,
这种情况下,当点击通知时,会启动App,而在App中,开发人员可以通过实现*AppDelegate中的方法:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions,然后从lauchOptions中获取App启动的原因,若是因为本地通知,则可以App启动时对App做对应的操作,比方说跳转到某个画面等等。栗子:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ NSLog(@"Start App...."); .... UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if ([self isWelcomeNotification:localNotification]) { NSLog(@"start with welcome notification"); [self.mainViewController showOfficeIntroduction]; } return YES;}
2. App运行在后台
3. App运行在前台
上面的2种情况的处理基本一致, 不同点只有当运行再后台的时候,会有弹窗提示用户另外一个App有通知,对于本地通知单的处理都是通过*AppDelegate的方法:- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification来处理的。 栗子:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ if ([self isWelcomeNotification:notification]) { [self.mainViewController showOfficeIntroduction]; }if ([self isCustomerDataNotification:notification]) { [self.mainViewController showCustomerDataIntroduction]; }}
0 0
- iOS之Local Notification(本土通知)
- iOS实现本地通知(local notification)的方法
- cordova插件之Local Notification(本地通知)
- iOS推送之本地推送(iOS Notification Of Local Notification)
- iOS基础知识:Objective-C 之 通知(Notification)
- ios local notification
- iOS Local Notification
- iOS 中常用通知(Notification)
- iOS 学会使用通知(notification)
- IOS Notification通知实例
- IOS Notification 通知中心
- IOS Notification 通知中心
- iOS 通知notification
- iOS Notification 通知中心
- iOS开发 -- 通知Notification
- iOS 通知notification
- iphone 如何添加本地通知(local notification)
- iphone 如何添加本地通知(local notification)
- Oracle用户、授权、角色管理
- c语言指针初探 一 指针与数组
- 【Maven】Maven的简单使用
- ios build setting笔记
- c#(Asp.net)多个Excel文件合并到一个新的excel中
- iOS之Local Notification(本土通知)
- [转]便于理解mysql内幕的各种逻辑图组
- hdu1166敌兵布阵(线段树点修改)
- The "Double-Checked Locking is Broken" Declaration
- 通过PEB的Ldr枚举进程内所有已加载的模块
- eclipse log4j配置
- [1241]Oil Deposits HDU
- 简要分析汽车燃油供给系统一般有什么样的作用
- poj 3304 (计算机图论_直线和线段相交)