標籤:
ios通知分為遠程通知和本地通知,遠程通知需要串連網路,本地通知是不需要的,不管使用者是開啟應用還是關閉應用,我們的通知都會發出,並被用戶端收到
我們使用遠程通知主要是隨時更新最新的資料給使用者,使用本地通知主要是提醒使用者來完成一些任務
遠程通知 Remote Notification:
其主要的工作原理為:用戶端發送自己的UUID和Bundle ID給蘋果的APNs伺服器-->蘋果的APNs伺服器加密後返回一個deviceToken給用戶端-->用戶端拿到devideToken後將其發送給app公司提供的伺服器-->此伺服器將用戶端的deviceToken儲存到資料庫-->當伺服器要發送遠程通知給用戶端的時候,會在資料庫中拿到此用戶端的deviceToken-->發送資料到蘋果的APNs伺服器,然後再發送到用戶端
遠程通知是需要真機的,另外還需要去蘋果開發人員中心申請認證:真機調試認證,遠程推送認證(要在哪台電腦上調試或發布哪個app),描述檔案認證(哪台電腦利用哪個裝置調試哪個app)
我們可以使用PushMebaby來類比伺服器,也可以利用第三方軟體來發送通知如Jpush等
下面是代碼的實現:
1 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 2 { 3 if ([UIDevice currentDevice].systemVersion.doubleValue < 8.0){ // 小於ios8 4 5 UIRemoteNotificationType type = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge; 6 7 // 系統自動發送UUID和Bundle ID到蘋果APNs伺服器 8 [application registerForRemoteNotificationTypes:type]; 9 }else{ // 大於等於ios810 11 UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound;12 13 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];14 // 通知類型15 [application registerUserNotificationSettings:settings];16 17 // 註冊通知18 [application registerForRemoteNotifications];19 }20 21 // 可以擷取到userInfo資料22 NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsAnnotationKey];23 24 return YES;25 }26 27 // 獲得deviceToken28 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken29 {30 NSLog(@"%@",deviceToken);31 }32 33 // ios7之前調用,接收到遠程通知的內容會調用34 // 程式是開啟狀態,不管前台還是後台,會調用這個方法35 // 如果程式是關閉狀態不會調用這個,會調用application: didFinishLaunchingWithOptions:36 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo37 {38 NSLog(@"%@",userInfo);39 }40 41 // ios7之後調用,如果接收到遠程通知的內容會調用這個方法42 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler43 {44 // 這個方法需要調用這個block來通知系統更新UI介面45 // UIBackgroundFetchResultNewData, 接收到資料46 // UIBackgroundFetchResultNoData, 沒有接收到資料47 // UIBackgroundFetchResultFailed 接收資料失敗48 completionHandler(UIBackgroundFetchResultNewData);49 50 }
本地通知 Local Notification
基本屬性和方法:
屬性:
- 指定通知發送的時間:NSDate *fireDate
- 指定發送通知的時區:NSTimeZone *timeZone
- 重複的周期: repeatInterval
- 通知內容:NSString *alertBody
- 鎖屏狀態的標題:NSString *alertAction
- 點擊通知之後的啟動圖片:NSString *alertLaunchImage
- 收到通知播放的音樂:NSString *soundName
- 表徵圖提醒數字:NSInteger applicationIconBadgeNumber
- 額外的資訊:NSDictionary *userInfo
方法:
- 立即執行:- (void)presentLocalNotificationNow:(UILocalNotification *)notification
- 註冊通知,根據指定發送時間執行:- (void)scheduleLocalNotification:(UILocalNotification *)notification
- 取消單個通知:- (void)cancelLocalNotification:(UILocalNotification *)notification
- 取消所有通知:- (void)cancelAllLocalNotifications
下面是代碼實現:
1 // 建立本地通知對象 2 UILocalNotification *noti = [[UILocalNotification alloc] init]; 3 4 // 指定通知發送的時間10s 5 noti.fireDate = [NSDate dateWithTimeIntervalSinceNow:10.0f]; 6 // 指定時區 7 noti.timeZone = [NSTimeZone defaultTimeZone]; 8 // 指定通知內容 9 noti.alertBody = @"這是通知的內容";10 11 // 設定通知重複的周期(1分鐘)12 noti.repeatInterval = NSCalendarUnitSecond;13 14 // 指定鎖定畫面的資訊15 noti.alertAction = @"這是鎖定畫面的資訊";16 17 // 設定點擊通知進入程式時候的啟動圖片18 noti.alertLaunchImage = @"xxx";19 20 // 收到通知播放的音樂21 noti.soundName = @"hehe.wav";22 23 // 設定應用程式的提醒表徵圖24 noti.applicationIconBadgeNumber = 99;25 26 // 註冊通知時可以指定將來點擊通知之後需要傳遞的資料27 noti.userInfo = @{@"dogName":@"xx1",28 @"weight":@(20)29 };30 31 // 註冊添加通知32 UIApplication *app = [UIApplication sharedApplication];33 [app scheduleLocalNotification:noti];
注意:在ios8中需要提前註冊通知類型
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // Override point for customization after application launch. 3 4 // 注意: 在iOS8中, 必須提前註冊通知類型 5 if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) { 6 UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound; 7 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil]; 8 // 註冊通知類型 9 [application registerUserNotificationSettings:settings];10 }11 }12 13 // 接收到本地通知時就會調用,前台自動調用,後台點擊通知後調用14 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification15 {16 NSLog(@"%@",notification.userInfo);17 }
ios 遠程通知(Remote Notification)和本地通知(Local Notification)