:fa-exclamation-triangle:注意本地通知/推送和[廣播通知NSNotificationCenter/key-value 觀察通知]沒有關係html
本地通知是由本地應用觸發的,是一種基於時間行爲的通知算法
|-例如:鬧鐘定時,待辦事項提醒數組
若是沒有受權即便添加通知也無效服務器
-(void)registLocalNotification{ if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { //UIMutableUserNotificationAction用來添加自定義按鈕 UIMutableUserNotificationAction * responseAction = [[UIMutableUserNotificationAction alloc] init]; responseAction.identifier = @"response"; responseAction.title = @"回覆"; responseAction.activationMode = UIUserNotificationActivationModeForeground; //點擊的時候啓動程序 UIMutableUserNotificationAction *deleteAction = [[UIMutableUserNotificationAction alloc] init]; deleteAction.identifier = @"delete"; deleteAction.title = @"刪除"; deleteAction.activationMode = UIUserNotificationActivationModeBackground; //點擊的時候不啓動程序,後臺處理 deleteAction.authenticationRequired = YES;//須要解鎖權限 deleteAction.destructive = YES; //YES爲紅色,NO爲藍色 UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init]; category.identifier = @"alert"; //UIUserNotificationActionContextDefault:默認添加能夠添加兩個自定義按鈕 //UIUserNotificationActionContextMinimal:四個自定義按鈕 [category setActions:@[responseAction, deleteAction] forContext:UIUserNotificationActionContextDefault]; //通知類型 UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; //通知設定 UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:type categories:[NSSet setWithObject:category]]; //註冊通知方法是在IOS8以後纔開始有的 [[UIApplication sharedApplication] registerUserNotificationSettings:setting]; } }
註冊完了後,觸發該代理方法app
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
:fa-location-arrow:建立一個本地通知通暢須要如下幾步:框架
1 建立UILocalNotification對象ide
2 設置處理通知時間fireDatepost
3 配置通知內容:通知主體,聲音,圖標數字等ui
4 配置通知傳遞的自定義數據參數userInfo(可選)操作系統
5 調用通知
-(void)addLocalNotification{ UILocalNotification *notification = [[UILocalNotification alloc] init]; //設置時區 notification.timeZone = [NSTimeZone defaultTimeZone]; //設定推送時間 notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; //通知內容 notification.alertBody = @"最近添加了許多功能,是否體驗?"; //鎖屏狀態下的左下角滑動動做提示 notification.alertAction = @"打開應用"; //打開程序時的加載畫面 notification.alertLaunchImage = @"Default"; //用來顯示鎖屏時,滑動顯示自定義按鈕,名字與註冊時的category的identifier一致 notification.category = @"alert"; //icon右上角數字 NSInteger number = [UIApplication sharedApplication].applicationIconBadgeNumber; notification.applicationIconBadgeNumber = number+1; //設置提示聲音 notification.soundName = UILocalNotificationDefaultSoundName; //設置重複次數 notification.repeatInterval = 2; notification.userInfo = @{@"id":@1, @"user":@"yanxiaoyu"}; //執行註冊通知 [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }
===IOS4===
:fa-bomb:程序未退出狀態:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
:fa-bomb:程序退出狀態:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; }
===IOS8===
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{ if ([identifier isEqualToString:@"deleteAction"]) { } completionHandler(); }
===IOS9===
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler;
-(void)cancelLocalNotification{ //獲取本地通知數組 NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; //刪除指定通知 [[UIApplication sharedApplication] cancelLocalNotification:notifications[0]]; //刪除全部通知 [[UIApplication sharedApplication] cancelAllLocalNotifications]; }
:fa-exclamation-triangle:本地通知最多隻能有64個,超過會被系統忽略
:fa-exclamation-triangle:通知的聲音格式必須是Linear PCM,MA4(IMA/ADPCM),uLaw,alaw中的一種,並且時間必須在30秒內,聲音文件必須放到main boundle中
:fa-exclamation-triangle:本地通知是操做系通通一調度的,只有在退出後臺或關閉才能收到通知
###2.遠程推送
:fa-location-arrow:推送通知的過程分爲三步
1 應用服務器提供商將服務器端要發送的**消息和設備令牌(device token)**發送給蘋果的消息推送服務器APNS
2 APNS根據設備令牌在已註冊的設備中查找對應設備,並將消息發送給響應設備
3 客戶端接將接收到的消息傳遞給應用程序,應用程序根據用戶設置彈出消息通知
更加詳細的流程圖參照下圖:
1.APP註冊遠程推送APNS
___a:[application registerUserNotificationSettings:];
___b:①準備開發配置文件(provisioning profile,也就是.mobileprovision後綴的文件) App ID不能使用手機的Apple ID,必須使用指定的ID,且在生成的配置文件中選擇Push Notifications服務,
②應用程序的Bundle Identifier必須和生成配置文件使用的APP ID徹底一致2.iOS請求獲取Device Token,app獲取Device Token
___a:註冊成功後,接收Device Token
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken___b:獲取Device Token失敗
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error3.IOS應用將Device Token發送給APP提供商,告訴服務器當前設備容許接受消息
___a:爲確保token生成算法變化後,仍能正常接收通知,APP每次啓動都會從新獲取Token
___b:IOS最好將上次的Device Token存儲起來,當向服務器發送token時,將新舊token一塊兒發送給服務器,當服務器發現token發生變化,刪除舊的token,新增新token
4.APP提供商將Device Token發送給APNS
___a:發送時,指定Device Token和消息內容,並嚴格遵照蘋果官方的消息格式,能夠藉助第三方消息推送框架來完成。
5.APNS根據消息中的Device Token查找已註冊設備,並推送消息
___a:若用戶已卸載程序,消息推送失敗,APNS會將錯誤消息通知給服務器,服務器根據錯誤消息,刪除已經存儲的Device Token,下次再也不發送。
更多詳細內容請參照以下連接
http://www.cnblogs.com/kenshincui/p/4168532.html#pushNotification
廣播通知是IOS內部的一種消息廣播機制,主要爲了解決程序內部不一樣對象之間的解耦,基於觀察者模式,不能垮應用程序通信,流程圖以下:
NSNotificationCenter
NSNotification
NSNotificationCenter時通知系統的中心,用於註冊和發出通知。
:fa-chevron-circle-right:添加監聽
- (void)addObserver:(id)observer //監聽者 selector:(SEL)aSelector //監聽方法:監聽者間聽到通知後,執行的方法 name:(NSString *)aName //監聽的通知名稱 object:(id)anObject; //通知的發送者 - (id <NSObject>)addObserverForName:(NSString *)name //監聽的通知名稱 object:(id)obj //通知的發出者 queue:(NSOperationQueue *)queue //操做隊列 usingBlock:(void (^)(NSNotification *note))block//監聽到通知後執行
:fa-chevron-circle-right:發送通知
- (void)postNotification:(NSNotification *)notification; //通知對象 - (void)postNotificationName:(NSString *)aName //通知名稱 object:(id)anObject; //通知發送者 - (void)postNotificationName:(NSString *)aName //通知名稱 object:(id)anObject //通知發送者 userInfo:(NSDictionary *)aUserInfo; //通知參數
:fa-chevron-circle-right:刪除監聽
- (void)removeObserver:(id)observer; //監聽對象 - (void)removeObserver:(id)observer //監聽對象 name:(NSString *)aName //通知名稱 object:(id)anObject; //通知發送者
//1.添加登錄成功的監聽事件 //若是登錄成功,執行loginSuccess方法 [[NSNotificationCenter defalutCenter] addObserver:self selector:@selector(loginSuccess:) name:@"LOGIN_SUCCESS" object:nil]; //2.若是登錄成功,給監聽事件發送通知 NSDictionary *userInfo = @{@"loginSuccess":@"歡迎登錄成功"}; NSNotification *notification = [NSNotification notificationWithName:@"LOGIN_SUCCESS" object:self userInfo:userInfo]; [[NSNotificationCenter defalutCenter] postNotification: notification]; //等價於下面的方法 [[NSNotificationCenter defaultCenter] postNotificationName:@"LOGIN_SUCCESS" object:self userInfo:userInfo]; //3.移除監聽事件 [[NSNotificationCenter defalutCenter] removeObserver:self name:@"LOGIN_SUCCESS" object:nil];