UILocalNotification本地通知
来源:互联网 发布:记账软件 共享 编辑:程序博客网 时间:2024/05/21 19:48
通知和应用是分别处理的,应用程序调度了通知之后,即便被关掉,仍然能够在指定的调度时间被触发。
通常在使用本地通知时,是在应用程序退出到后台时来调度的。
应用程序退出到后台之后,会在内存中驻留10分钟的时间,过了10分钟之后,系统会自动清理应用程序,
被清理之后,再次运行时,才会调用application:didFinishLaunchingWithOptions:方法。
1. 通常本地通知是在退出应用程序时,被调度的,在后台往往会做一些后台调度的服务,
譬如
1) 一个联网下棋的软件:
当对手落子后,需要进行检测,检测完成后,通过本地通知的方式通知用户重新回到应用程序
2) 例如一个新闻客户端,在后台检测最新的数据,当发现数据后,通知用户,用户进入应用时,直接显示通知的内容
3)未接来电
4)QQ聊天
如果要实现类似的上述功能,可以借助userInfo数据字典
要获取到通知的消息,可以在application:didFinishLaunchingWithOptions:方法中实现
ILocalNotification常用属性
fireDate; 通知的触发时间
timeZone; 通知所在时区,如果指定时区,可以自行调整时间
alertBody; 通知的正文
soundName; 通知播放的声音,如果用UILocalNotificationDefaultSoundName,使用系统默认声音
applicationIconBadgeNumber; 应用程序图标
userInfo; 数据字典
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 截获本地通知,用户通过通知的横幅点击进入系统,字典中才能找到本地通知
UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
// 判断通知是否存在
if (notification) {
NSLog(@"%@", notification.userInfo);
[notification.userInfo writeToFile:@"/users/apple/Desktop/123.plist" atomically:YES];
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
[self loadNotification];
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - 进入前台,清除右上角图标
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[application setApplicationIconBadgeNumber:0];
}
#pragma mark - 进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self loadNotification];
}
#pragma mark - 创建本地通知
- (void)loadNotification
{
UILocalNotification *notification = [[UILocalNotification alloc]init];
// 5秒钟之后触发
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.alertBody = @"出大事了";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 100;
notification.userInfo = @{@"userName": @"马大哈", @"age": @(18)};
// 要调用本地通知,需要通过UIApplication来统一调度
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
}
@end
通常在使用本地通知时,是在应用程序退出到后台时来调度的。
应用程序退出到后台之后,会在内存中驻留10分钟的时间,过了10分钟之后,系统会自动清理应用程序,
被清理之后,再次运行时,才会调用application:didFinishLaunchingWithOptions:方法。
1. 通常本地通知是在退出应用程序时,被调度的,在后台往往会做一些后台调度的服务,
譬如
1) 一个联网下棋的软件:
当对手落子后,需要进行检测,检测完成后,通过本地通知的方式通知用户重新回到应用程序
2) 例如一个新闻客户端,在后台检测最新的数据,当发现数据后,通知用户,用户进入应用时,直接显示通知的内容
3)未接来电
4)QQ聊天
如果要实现类似的上述功能,可以借助userInfo数据字典
要获取到通知的消息,可以在application:didFinishLaunchingWithOptions:方法中实现
ILocalNotification常用属性
fireDate; 通知的触发时间
timeZone; 通知所在时区,如果指定时区,可以自行调整时间
alertBody; 通知的正文
soundName; 通知播放的声音,如果用UILocalNotificationDefaultSoundName,使用系统默认声音
applicationIconBadgeNumber; 应用程序图标
userInfo; 数据字典
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 截获本地通知,用户通过通知的横幅点击进入系统,字典中才能找到本地通知
UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
// 判断通知是否存在
if (notification) {
NSLog(@"%@", notification.userInfo);
[notification.userInfo writeToFile:@"/users/apple/Desktop/123.plist" atomically:YES];
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
[self loadNotification];
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - 进入前台,清除右上角图标
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[application setApplicationIconBadgeNumber:0];
}
#pragma mark - 进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self loadNotification];
}
#pragma mark - 创建本地通知
- (void)loadNotification
{
UILocalNotification *notification = [[UILocalNotification alloc]init];
// 5秒钟之后触发
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.alertBody = @"出大事了";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 100;
notification.userInfo = @{@"userName": @"马大哈", @"age": @(18)};
// 要调用本地通知,需要通过UIApplication来统一调度
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
}
@end
0 0
- 本地通知UILocalNotification
- UILocalNotification 本地通知
- iOS本地通知UILocalNotification
- IOS 本地通知 UILocalNotification
- ios本地通知UILocalNotification
- 本地通知UILocalNotification
- iOS UILocalNotification本地通知
- 本地通知UILocalNotification
- iOS本地通知:UILocalNotification
- UILocalNotification本地通知介绍
- 本地通知UILocalNotification
- UILocalNotification 本地通知
- 本地通知UILocalNotification
- iOS本地通知:UILocalNotification
- iOS UILocalNotification本地通知
- UILocalNotification一本地通知
- iOS本地通知:UILocalNotification
- 本地通知UILocalNotification
- 理解23种设计模式
- 权限控制器——Shiro安全框架
- 很好用的设置RecycleView的ItemDecoration的框架FlexibleDivider
- (Leetcode)判断一个图是否是可以拓扑排序的——使用Queue
- leetcode--jump_game&&jump_game_II
- UILocalNotification本地通知
- ubuntu sudo出现sudo:must be setuid root
- 分布式系统中的通讯范型与间接通信
- 实现广告条,采用ViewPager里嵌套fragment
- 流程控制
- java之物件导向程式设计(一)
- LINUX: 配置自己的网卡信息
- 【学习笔记】第十四章 类型信息
- HIVE之 将写好的sql 语句赋值到窗口, 出现“Display all 475 possibilities? (y or n)”问题