通知中心(NotificationCenter)和通知(UILocalNotification)是雷鋒和雷峰塔的關(guān)系哦
通知分為:本地通知和遠(yuǎn)程通知
本地通知
就是你好久不用一個(gè)程序帖蔓,它給你拉一條橫幅寫“部落里沒有你臣妾好寂寞”;或者更典型的一個(gè)日歷軟件,到時(shí)間提醒你今天是喬幫主的忌日崖疤。(UILocalNotification)
即:由本地應(yīng)用程序發(fā)起的通知,一般是在應(yīng)用程序處于后臺(tái)或退出后,讓iOS系統(tǒng)在指定時(shí)間通知用戶的一種方式。
遠(yuǎn)程通知
就是你收到一條微信屎鳍,在鎖屏上顯示的或在頂部導(dǎo)航欄顯示的楷怒。(APNS)
本地通知使用步驟:
創(chuàng)建本地通知對(duì)象(UILocalNotification)
let notification = UILocalNotification()
設(shè)置處理通知的時(shí)間(fireDate屬性)
notification.fireDate = NSDate(timeIntervalSinceNow: 5)
配置通知的內(nèi)容:通知主體,通知聲音,圖標(biāo)數(shù)字
notification.repeatInterval = NSCalendarUnit.CalendarUnitMinute//CalendarUnit是指一分鐘/小時(shí)/周之后再發(fā)
notification.alertBody = "通知主體內(nèi)容!"
notification.applicationIconBadgeNumber = 1;//強(qiáng)迫癥最受不了的小紅點(diǎn)垛吗,圖標(biāo)數(shù)字1
notification.soundName = "success.caf"
調(diào)用通知:
按計(jì)劃調(diào)度: scheduleLocalNotification(一般情況都是按計(jì)劃調(diào)用)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
立即調(diào)用: presentLocalNotification(一般不用)
要注意從iOS8開始,應(yīng)用想發(fā)通知叠艳,必須經(jīng)過用戶允許奶陈,否則通知無法發(fā)送。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//判斷當(dāng)前設(shè)備是否是IOS8
if(UIDevice.currentDevice().systemVersion > 7.0){
//向用戶申請(qǐng)發(fā)通知的權(quán)限(參數(shù)可以允許:通知內(nèi)容附较,通知聲音吃粒,圖標(biāo)數(shù)字)
let settings = UIUserNotificationSettings(forTypes:UIUserNotificationType.Alert | UIUserNotificationType.Sound | UIUserNotificationType.Badge, categories:nil)
application.registerUserNotificationSettings(settings)
}
}
用戶點(diǎn)擊通知之后會(huì)進(jìn)入我們的App,所以還需要
1.取消紅點(diǎn)
func applicationWillEnterForeground(application: UIApplication) {
println("應(yīng)用程序從后臺(tái)進(jìn)入到了前臺(tái)!")
//取消應(yīng)用程序消息圖標(biāo)
application.applicationIconBadgeNumber = 0
}
//取消已有的通知
UIApplication.sharedApplication().cancelAllLocalNotifications()
1.控制臺(tái)NSNotificationCenter
[[NSNotificationCenterdefaultCenter]postNotificationName:@"12306" object:self userInfo:@{@"買票": @"票已賣完",@"我要投訴":@"請(qǐng)撥打12306投訴電話"}];//向控制臺(tái)發(fā)送消息
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(woyaotousu:) name:@"12306" object:nil];//成為控制臺(tái)觀察者 一旦接收到12306的消息拒课,就執(zhí)行方法woyaotousu:
-(void)woyaotousu:(NSNotification*)notification{//參數(shù)為一個(gè)控制臺(tái)
NSDictionary*message=notification.userInfo;//得到控制臺(tái)攜帶的消息
NSLog(@"%@ %@",message[@"買票"],message[@"我要投訴"]);
}
程序進(jìn)入后臺(tái)時(shí)徐勃,就會(huì)發(fā)出UIApplicationWillResignActiveNotification通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aa:) name:UIApplicationWillResignActiveNotification object:nil];
// 發(fā)出通知
[MTNotificationCenterpostNotificationName:MTCategoryDidChangeNotification object:niluserInfo:@{MTSelectCategory : category, MTSelectSubcategoryName : category.subcategories[subrow]}];
2.鍵盤通知
UIKeyboard
NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
[center addObserver:selfselector:@selector(keyboardOpen:) name:UIKeyboardWillShowNotificationobject:nil];//注冊(cè)成為觀察者事示,接收來自鍵盤打開的通知
[center addObserver:selfselector:@selector(keyboardClose:) name:UIKeyboardWillHideNotificationobject:nil];//注冊(cè)成為觀察者,接收來自鍵盤關(guān)閉的通知
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];//移除一個(gè)通知
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];//移除一個(gè)通知
NSValue*value=notification.userInfo[UIKeyboardFrameEndUserInfoKey];//讀取消息僻肖,得到字典肖爵,在獲得字典里面的信息
CGRect rect=[value CGRectValue];//獲得鍵盤的frame
```
3.圖標(biāo)通知
```
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 8.0) { // iOS8+ IconBadge需授權(quán)
//創(chuàng)建一個(gè)Setting,申請(qǐng)發(fā)送通知
UIUserNotificationSettings *setting=[UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeBadge categories:nil];
//注冊(cè)一個(gè)Setting
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
}
//在桌面圖標(biāo)上臀脏,顯示提示10個(gè)消息
[UIApplicationsharedApplication].applicationIconBadgeNumber=10;
```
4.給用戶的通知
4.1 OC
```
//注冊(cè)應(yīng)用接收通知
if ([[UIDevicecurrentDevice].systemVersiondoubleValue] > 8.0){
UIUserNotificationSettings *settings = [UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
//創(chuàng)建本地通知
UILocalNotification *localNoti=[[UILocalNotification alloc]init];
// 設(shè)置內(nèi)容
localNoti.alertBody = [NSStringstringWithFormat:@"%@\n%@",message.fromStr,message.body];
// 設(shè)置通知執(zhí)行時(shí)間
localNoti.fireDate = [NSDate date];
//聲音
localNoti.soundName = @"default";
//紅色的溶質(zhì)角標(biāo)
localNoti.applicationIconBadgeNumber = 1;
//執(zhí)行
[[UIApplication sharedApplication] scheduleLocalNotification:localNoti];
```
4.2 swift
```
//取消所有通知
UIApplication.sharedApplication().cancelAllLocalNotifications()
//創(chuàng)建通知設(shè)置
let setting = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Sound | UIUserNotificationType.Badge, categories: nil)
//注冊(cè)通知設(shè)置
UIApplication.sharedApplication().registerUserNotificationSettings(setting)
//創(chuàng)建通知對(duì)象
let notification = UILocalNotification()
//設(shè)置fireDate(推送間隔時(shí)間)
notification.fireDate = NSDate(timeIntervalSinceNow: 10)
//按分鐘重復(fù)
notification.repeatInterval = NSCalendarUnit.CalendarUnitMinute
//通知內(nèi)容
notification.alertBody = "時(shí)間到了"
//紅色的溶質(zhì)角標(biāo)
notification.applicationIconBadgeNumber = 1
//提醒聲音
notification.soundName = “success.caf"
localNoti.soundName = @"default";
//調(diào)用
UIApplication.sharedApplication().scheduleLocalNotification(notification)
//當(dāng)點(diǎn)了通知劝堪,回到應(yīng)用程序的時(shí)候回調(diào)
funcapplicationWillEnterForeground(application: UIApplication) {
application.applicationIconBadgeNumber=0}
//當(dāng)程序在后臺(tái)運(yùn)行時(shí),如果收到了一個(gè)通知揉稚,用戶點(diǎn)擊了這個(gè)通知秒啦,從后臺(tái)進(jìn)入前臺(tái)的時(shí)候回調(diào)
-(void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification *)notification{}
//當(dāng)程序在后臺(tái)運(yùn)行時(shí),如果收到了一個(gè)遠(yuǎn)程通知搀玖,用戶點(diǎn)擊了這個(gè)通知帝蒿,從后臺(tái)進(jìn)入前臺(tái)的時(shí)候回調(diào)
-(void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary *)userInfo{}
```