Local Notification的作用
2016-03-24 14:36
225 查看
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:立即发送通知到本机
栗子:
Object-c代码
- (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做对应的操作,比方说跳转到某个画面等等。栗子:
Object-c代码
- (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来处理的。 栗子:
Object-c代码
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([self isWelcomeNotification:notification]) {
[self.mainViewController showOfficeIntroduction];
}if ([self isCustomerDataNotification:notification]) {
[self.mainViewController showCustomerDataIntroduction];
}
}
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:立即发送通知到本机
栗子:
Object-c代码
- (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做对应的操作,比方说跳转到某个画面等等。栗子:
Object-c代码
- (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来处理的。 栗子:
Object-c代码
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([self isWelcomeNotification:notification]) {
[self.mainViewController showOfficeIntroduction];
}if ([self isCustomerDataNotification:notification]) {
[self.mainViewController showCustomerDataIntroduction];
}
}
相关文章推荐
- CPU的内部架构和工作原理
- 日期格式转换
- 空 格不能多
- RecyclerView,ListView去除边缘动画效果
- sed入门实例
- 利用三维旋转矩阵在空间中旋转平面
- Mysql5.7 安装
- ASP.NET Core 1.0中的管道-中间件模式
- Python爬取房屋租售信息
- 使用Java字节流拷贝文件
- python脚本后台运行
- P2P通讯的C++实现-UDP Hole Punching方法
- PAT 乙级 1025. 反转链表
- mysql5.7.11密码修改
- XP修复任务栏程序最小化丢失
- Media Playback媒体播放
- Codeforces 140E New Year Garland (dp + 容斥)
- 【php-redis】pub/sub 公开和订阅
- sqlit的基本操作
- linux下安装java jdk后版本不对一例