本地通知:
kvc(鍵值編碼)優(yōu)點:可以給類的任意實例變量賦值锦秒,即使實例變量是私有的
缺點:必須要知道實例變量名灌危,破壞封裝性
KVO (鍵值觀察) ?是一種能使對象獲取到其他對象屬性變化的通知,極大的簡化了代碼廷臼,實現(xiàn)KVO模式,被觀察的對象必須是使用KVC來修改它的實例變量,這樣才能被觀察者觀察到彼棍。
本地通知是由本地應(yīng)用觸發(fā)的,它是基于時間行為的一種通知形式膳算,例如鬧鐘定時座硕、待辦事項提醒,又或者一個應(yīng)用在一段時候后不使用通常會提示用戶使用此應(yīng)用等都是本地通知
由于在iOS8之后涕蜂,本地通知的寫法有所改變华匾,所以在此之前需要進行版本判斷,如下:
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//判斷版本是不是8.0以上的
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
//注冊通知
UIUserNotificationSettings *settings= [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge |UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] register UserNotificationSettings:settings];
}
return YES;
}
1蜘拉、創(chuàng)建UILocalNotification (創(chuàng)建本地通知,注冊)
2有鹿、設(shè)置處理通知的時間fireDate(觸發(fā)通知的時間)
3旭旭、配置通知的內(nèi)容:通知主體、葱跋、通知聲音持寄、圖標源梭、數(shù)字等
4、配置通知傳遞的自定義參數(shù)useInfo(可選)
5稍味、調(diào)用通知咸产,可以使用scheduleLocalNotification:按計劃調(diào)度一個通知,也可以用presentLocalNotificationNow立即調(diào)用通知仲闽;
//創(chuàng)建本地通知對象
UILocalNotification*localNotification= [[UILocalNotificationalloc]init];
//設(shè)定調(diào)度時間脑溢,即通知五秒后執(zhí)行
NSDate*nowDate = [NSDatedate];
[localNotificationsetFireDate:[nowDatedateByAddingTimeInterval:5.0]];
//循環(huán)次數(shù),kCFCalendarUnitWeekday一周一次
localNotification.repeatInterval=0;
//當前日歷赖欣,使用前最好設(shè)置時區(qū)等信息以便能夠自動同步時間
//notification.repeatCalendar=[NSCalendar currentCalendar];
//設(shè)定時區(qū)
[localNotificationsetTimeZone:[NSTimeZonedefaultTimeZone]];
//設(shè)置彈出消息
[localNotificationsetAlertBody:@"搶購時間到了"];
[localNotificationsetAlertAction:@"show now"];
//設(shè)置通知聲音
[localNotificationsetSoundName:UILocalNotificationDefaultSoundName];
//設(shè)置用戶信息
localNotification .userInfo=@{@"id":@1,@"user":@"KenshinCui"};//綁定到通知上的其他附加信息
//設(shè)置IconbadgeNumber圖標數(shù)字屑彻,設(shè)置count為全局變量用count來控制圖標數(shù)字的增加
count++;
[localNotificationsetApplicationIconBadgeNumber:count];
//應(yīng)用程序計劃執(zhí)行通知
[[UIApplicationsharedApplication]scheduleLocalNotification:localNotification];
[[[UIAlertViewalloc]initWithTitle:@"提示"message:@"設(shè)置提醒成功"delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil,nil]show]
二、程序運行時接收到本地推送信息
-(void)application:(UIApplication*)applicationdidReceiveLocalNotification:(UILocalNotification*)notification{
//這里顶吮,你就可以通過notification的useinfo社牲,干一些你想做的事情了
//這里是創(chuàng)建一個KViewController并在點擊通知時切換到該頁面
UIStoryboard*storyboard = [UIStoryboardstoryboardWithName:@"Main"bundle:nil];
KViewController*kViewController =[storyboardinstantiateViewControllerWithIdentifier:@"KViewController"];
UINavigationController*navigationController= (UINavigationController*)self.window.rootViewController;
[navigationControllerpushViewController:kViewControlleranimated:YES];
//圖標數(shù)字在每次點開通知后都會減一,知道圖標為0(即圖標消失)不再減
application.applicationIconBadgeNumber-=1;
}
三悴了、移除本地推送
#pragma mark移除本地通知
-(void)removeNotification{
[[UIApplicationsharedApplication]cancelAllLocalNotifications];
}