您的位置:首页 > 产品设计 > UI/UE

UILocalNotification

2016-01-30 17:45 405 查看
UILocalNotification本地通知,通知有两种形式,一种是网络通知一种是本地通知,本文章介绍的是本地通知常用属性的使用及我所知的注意事项

@property (strong,nonatomic) UILocalNotification *localNotification;

//初始化本地通知对象

self.localNotification = [[UILocalNotification alloc] init];

if(self.localNotification){

//获取当前时间

NSDate *now = [[NSDate alloc] init];

//设置时区(设置后提示的时间根据时区的改变而改变,若为nil将在某个时间段后发送通知,不会根据确切的时间进行通知)

self.localNotification.timeZone = [NSTimeZone defaultTimeZone];

//指定通知的提醒时间,单位是秒

self.localNotification.fireDate = [now dateByAddingTimeInterval:10];

//提示消息(桌面提示的消息,只有在锁屏或者是应用没有显示出来的时候才会显示),如果没有设置将会通知不会有提醒

self.localNotification.alertBody = @"弹出提示内容主体";

//事件提示

self.lovalNotification.alertAction = NSLocalizedString(@"起床了",nil);

//控制事件提示是否显示,默认值是YES

self.localNotification.hasAction = NO;

//设置通知提示音,这里使用的是默认的消息提示音,可以定义提示音,通知最长的提示时间是30秒,这个值无法改变,但是可以通过提示音的长短来改变提示的时长,自定以设置声音名称加上声音类型的后缀即可

self.localNotification.soundName = UILocalNotificationDefaultSoundName;

//通知的次数,就是显示在ICon左上角的圆形红色背景白色数字

self.localNotification.applicationIconBadgeNumber = 0;

//主要作用有标识本地通知、页面之间的传值携带者,数值比较多的情况下可以将数据封装在这里面进行传递

NSMuatbleDictionary *userInfo = [NSMutableDictionary dictionary];

[userInfo setObject:@"localNotificationValue" forKey:@"localNotificationKey"];

self.localNotification.userInfo = userInfo;

//重复的时间间隔,设置通知最小重复间隔是分钟,但是在枚举数据类型中有秒,但是设置了也是无效的

self.localNotification.repeatInterval = NSCalendarUnitEra;

}

//上半部分对本地通知UILocalNotification的常用设置已经基本介绍完了,本地通知在iOS下NotificationManager统一管理,上面已经将本地通知封装好了现在将封装好的本地通知对象加入到系统Notification管理机制队列中,系统会在指定的时间激发将本地Notification。

//添加本地通知有两种,一种是计划通知(最多64个通知,新的会迭代老的通知),一种是马上通知

//计划通知

[[UIApplication sharedApplication] scheduleLocalNotification:self.localNotification];

//马上通知

//[[UIApplication sharedApplication] presentLocalNotification:self.localNotification];

//本地通知不会因为软件的卸载就被取消了,当程序再次安装新老通知同样会存在,取消通知只能通过手动的方式进行取消

//一次性取消所有的通知,无论是计划的还是马上的都会被取消

[[UIApplication sharedApplication] cancelAllLocalNotification];

//通知触发后在AppDelegate中的代理函数,可以在这个函数里处理通知触发后的逻辑业务

//本地通知代理方法(在程序处于运行的状态下才会执行该方法)

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

{

NSLog(@"应用程序接受本地通知");

//取消某个特定的本地通知

for (UILocalNotification *noti
in [[UIApplication
sharedApplication] scheduledLocalNotifications]) {

NSString *notiID = noti.userInfo[@"localNotificationKey"];

NSString *receiveNotiID = notification.userInfo[@"localNotificationKey"];

if ([notiID
isEqualToString:receiveNotiID]) {

[[UIApplication
sharedApplication] cancelLocalNotification:noti];

return;

}

}

UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:@"你好测试本地通知"
message:@"本地通知哦"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];

[alert show];

}

本地通知就介绍到这里,后面还有通知中心的介绍
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: