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:立即发送通知到本机
栗子:
处理Local Notification
在处理本地通知时,我们需要考虑三种情况:
1. App没有启动,
这种情况下,当点击通知时,会启动App,而在App中,开发人员可以通过实现*AppDelegate中的方法:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions,然后从lauchOptions中获取App启动的原因,若是因为本地通知,则可以App启动时对App做对应的操作,比方说跳转到某个画面等等。栗子:
2. App运行在后台
3. App运行在前台
上面的2种情况的处理基本一致, 不同点只有当运行再后台的时候,会有弹窗提示用户另外一个App有通知,对于本地通知单的处理都是通过*AppDelegate的方法:- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)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];
- }
- }