1、本地推送數組
1.創建本地推送安全
1 UILocalNotification *notification = [[UILocalNotification alloc] init]; 2 //推送的內容 3 notification.alertBody = @"起牀了";//設備收到本地通知時橫額或鎖屏時的主要文字內容 4 notification.alertAction = @"起牀了(ˇˍˇ)";//鎖屏時顯示的slide to後面的文字內容 5 //推送時間 6 notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2]; 7 //多久重複一次 8 notification.repeatInterval = NSCalendarUnitHour; 9 //啓動圖片 10 notification.alertLaunchImage = @"AS_QQ@2x.png"; 11 //收到推送聲音 12 notification.soundName = UILocalNotificationDefaultSoundName;//默認的 13 notification.soundName = @"shake_sound_male.wav";//自定義的 14 //時區 15 notification.timeZone = [NSTimeZone localTimeZone]; 16 //用戶自定義數據 17 notification.userInfo = @{kNotificationKey:@"notification1"}; 18 // 設置應用程序右上角的提醒個數 19 notification.applicationIconBadgeNumber++; 20 // 將通知添加到系統中 21 [[UIApplication sharedApplication] scheduleLocalNotification:notification];
注意這個方法只有在程序啓動以後纔會執行,所以當程序處於後臺時,該方法不會執行。服務器
有一點須要注意,若是咱們的應用程序給系統發送的本地通知是週期性的,那麼即便把程序刪了重裝,以前的本地通知在重裝時依然存在(沒有從系統中移除)。app
所以咱們須要取消通知的方法,固然該對象也會在scheduledLocalNotifications數組中移除。ide
2.移除推送通知測試
① 暴力方法:直接取消全部推送spa
1 [[UIApplication sharedApplication] cancelAllLocalNotifications];
② 移除個別推送通知:code
1 //獲取全部的本地推送 2 NSArray *allLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; 3 //取消指定推送 4 //1.遍歷全部的本地推送 5 for (UILocalNotification *notification in allLocalNotifications) 6 { 7 //2.找指定的推送 8 if ([notification.userInfo[kNotificationKey] isEqualToString:@"notification1"]) 9 { 10 //3.取消推送 11 [[UIApplication sharedApplication] cancelLocalNotification:notification]; 12 } 13 }
2、遠程推送orm
① 概念:遠程推送是在iOS 3.0之後被引入的功能,是當程序沒有啓動或不在前臺運行時,告訴用戶有新消息的一種途徑,是從外部服務器發送到應用程序上的。通常說來,當要顯示消息或下載數據的時候,通知是由遠程服務器(程序的提供者)發送,而後經過蘋果的推送通知服務APNS(Apple Push Notification Service)推送到設備的程序上。對象
做爲提供者爲程序開發和部署推送通知,必須經過iOS Developer Program Portal得到SSL證書。每一個證書限用於一個程序,使用程序的bundle ID做爲標識。
證書有兩種用途的:一種是針對sandbox(用於開發和測試),另一種針對發佈產品。這兩種運行環境擁有爲各自指定的IP地址而且須要不一樣的證書。還必須爲兩種不一樣的環境獲取各自的provisioning profiles。
② APNS:
1.APNS提供了兩項基本的服務:
a.消息推送 --- 使用流式TCP套接字將推送通知做爲二進制數據發送給APNs。消息推送有分別針對開發和測試用的sandbox、發佈產品的兩個接口,每一個都有各自的地址和端口。無論用哪一個接口,都須要經過TLS或SSL,使用SSL證書來創建一個安全的信道。提供者編制通知信息,而後經過這個信道將其發送給APNs。
b.反饋服務 --- 能夠獲得針對某個程序的發送失敗記錄。提供者應該使用反饋服務週期性檢查哪些設備一直收不到通知,不須要重複發送通知到這些設備,下降推送服務器的負擔。
2.APNS的工做機制:
a.應用程序註冊遠程消息推送
1 if ([UIDevice version] >= 8.0) 2 { 3 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil]; 4 //設置類型 5 [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 6 //註冊遠程推送 7 [[UIApplication sharedApplication] registerForRemoteNotifications]; 8 } 9 else 10 { 11 //iOS8如下必須設置類型 12 [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 13 UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert]; 14 }
b.應用程序向APNS獲取DeviceToken
c.APNS接收後返回DeviceToken
1 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 2 { 3 NSLog(@"%@",deviceToken); 4 //提示:提交蘋果服務器須要去除<>和空格 5 NSString *string = [NSString stringWithFormat:@"%@",deviceToken]; 6 string = [string stringByReplacingOccurrencesOfString:@"<" withString:@""]; 7 string = [string stringByReplacingOccurrencesOfString:@">" withString:@""]; 8 string = [string stringByReplacingOccurrencesOfString:@" " withString:@""]; 9 NSLog(@"%@",string); 10 }
d.應用程序將DeviceToken發送給PUSH服務端程序(Provider)
e.Provider將DeviceToken和推送內容上傳至APNS
f.APNS將推送內容推送至全部註冊過的iPhone上