LocalNotification本地通知
来源:互联网 发布:fm2016开档曼联数据 编辑:程序博客网 时间:2024/06/06 19:14
本地通知是由本地应用触发的,它是基于时间行为的一种通知形式,例如闹钟定时、待办事项提醒,又或者一个应用在一段时候后不使用通常会提示用户使用此应用等都是本地通知。创建一个本地通知通常分为以下几个步骤:
- 创建UILocalNotification。
- 设置处理通知的时间fireDate。
- 配置通知的内容:通知主体、通知声音、图标数字等。
- 配置通知传递的自定义数据参数userInfo(这一步可选)。
- 调用通知,可以使用scheduleLocalNotification:按计划调度一个通知,也可以使用presentLocalNotificationNow立即调用通知。
下面就以一个程序更新后用户长期没有使用的提醒为例对本地通知做一个简单的了解。在这个过程中并没有牵扯太多的界面操作,所有的逻辑都在AppDelegate中:进入应用后如果没有注册通知,需要首先注册通知请求用户允许通知;一旦调用完注册方法,无论用户是否选择允许通知此刻都会调用应用程序的- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings代理方法,在这个方法中根据用户的选择:如果是允许通知则会按照前面的步骤创建通知并在一定时间后执行。
AppDelegate.m
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// Override point for customization after application launch.
//如果已经获得发送通知的授权则创建本地通知,否则请求授权(注意:如果不请求授权在设置中是没有对应的通知设置项的,也就是说如果从来没有发送过请求,即使通过设置也打不开消息允许设置)
if ([[UIApplicationsharedApplication]currentUserNotificationSettings].types!= UIUserNotificationTypeNone) {
[selfaddLocalNotification];
} else {
[[UIApplicationsharedApplication]registerUserNotificationSettings:[UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlertcategories:nil]];
// Override point for customization after application launch.
//如果已经获得发送通知的授权则创建本地通知,否则请求授权(注意:如果不请求授权在设置中是没有对应的通知设置项的,也就是说如果从来没有发送过请求,即使通过设置也打不开消息允许设置)
if ([[UIApplicationsharedApplication]currentUserNotificationSettings].types!= UIUserNotificationTypeNone) {
[selfaddLocalNotification];
} else {
[[UIApplicationsharedApplication]registerUserNotificationSettings:[UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlertcategories:nil]];
}
}
#pragma调用过用户注册通知方法之后执行
-(void)application:(UIApplication*)application didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings
{
if (notificationSettings.types!= UIUserNotificationTypeNone) {
[selfaddLocalNotification];
}
}
#pragma 进入前台后设置消息信息
- (void)applicationWillEnterForeground:(UIApplication*)application
{
[[UIApplicationsharedApplication]setApplicationIconBadgeNumber:0];//进入前台取消应用消息图标
}
#pragma添加本地通知
- (void)addLocalNotification
{
//定义本地通知对象
UILocalNotification *notification = [[UILocalNotificationalloc]init];
//设置调用时间
notification.fireDate=[NSDatedateWithTimeIntervalSinceNow:10000.0];//通知触发时间, 10后
notification.repeatInterval= 2;//通知重复次数
notification.repeatCalendar= [NSCalendarcurrentCalendar];//当前日历,使用前最好设置时区等信息以便能够自动同步时间
//设置通知属性
notification.alertBody= @"最近更新了很多有趣的文章,是否前往?";//通知体
notification.applicationIconBadgeNumber= 1;//应用程序图标右上角显示的消息数
notification.alertAction= @"打开应用";//待机界面的滑动动作提示
notification.alertLaunchImage= @"Default";//通过点击通知打开应用时的启动图片,这里使用程序启动图片
// notification.soundName = @"msg.caf";//真机
//设置用户信息
// notification.userInfo=@{@"id":@1,@"user":@"Kenshin Cui"};//绑定到通知上的其他附加信息
//调用通知
[[UIApplicationsharedApplication]scheduleLocalNotification:notification];
}
#pragma移除本地通知,再不需要此通知时
- (void)removeNotification
{
[[UIApplicationsharedApplication]cancelAllLocalNotifications];
{
if (notificationSettings.types!= UIUserNotificationTypeNone) {
[selfaddLocalNotification];
}
}
#pragma 进入前台后设置消息信息
- (void)applicationWillEnterForeground:(UIApplication*)application
{
[[UIApplicationsharedApplication]setApplicationIconBadgeNumber:0];//进入前台取消应用消息图标
}
#pragma添加本地通知
- (void)addLocalNotification
{
//定义本地通知对象
UILocalNotification *notification = [[UILocalNotificationalloc]init];
//设置调用时间
notification.fireDate=[NSDatedateWithTimeIntervalSinceNow:10000.0];//通知触发时间, 10后
notification.repeatInterval= 2;//通知重复次数
notification.repeatCalendar= [NSCalendarcurrentCalendar];//当前日历,使用前最好设置时区等信息以便能够自动同步时间
//设置通知属性
notification.alertBody= @"最近更新了很多有趣的文章,是否前往?";//通知体
notification.applicationIconBadgeNumber= 1;//应用程序图标右上角显示的消息数
notification.alertAction= @"打开应用";//待机界面的滑动动作提示
notification.alertLaunchImage= @"Default";//通过点击通知打开应用时的启动图片,这里使用程序启动图片
// notification.soundName = @"msg.caf";//真机
//设置用户信息
// notification.userInfo=@{@"id":@1,@"user":@"Kenshin Cui"};//绑定到通知上的其他附加信息
//调用通知
[[UIApplicationsharedApplication]scheduleLocalNotification:notification];
}
#pragma移除本地通知,再不需要此通知时
- (void)removeNotification
{
[[UIApplicationsharedApplication]cancelAllLocalNotifications];
}
传送门:
通知与消息机制:
http://www.cnblogs.com/kenshincui/p/4168532.html
0 0
- LocalNotification本地通知
- 本地通知LocalNotification简单实用及示例
- Unity3D研究院之IOS本地消息通知LocalNotification的使用
- iOS 本地通知(LocalNotification,低于iOS 10的版本)
- Unity3D研究院之IOS本地消息通知LocalNotification的使用(六十七)
- Unity IOS本地消息通知LocalNotification的使用—推送功能
- 本地通知
- 本地通知。
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- AndroidStudio里面怎么取消与SVN的关联
- Visual Studio使用
- How to Use a Shader to Dynamically Swap a Sprite's Colors
- android 9.png图片原理
- 随机概率算法
- LocalNotification本地通知
- 如何更改tomcat7及以上版本内存设置
- 构建一个倒排索引 A first take at building an inverted index
- 邮件开发:邮件内容组织结构、复杂邮件的编写API
- 在selenium测试中使用XPATH功能函数starts-with、contains、descendant、ancestor、text()定位网页元素
- 使用host模式搭建dockyard环境
- UITableView中刷新单行cell与单个section段的方法
- 一大第15位代表-尼克尔斯基
- Mac中常用的终端命令