一直更新了iOS8,可是一直沒有開始研究這個iOS8,今天由於項目用到了推送,因而體驗了iOS8的推送,先講講這個推送。目前分爲四個推送:用戶推送,本地推送,遠程推送,地理位置推送。
git
咱們先開始講這個用戶推送,咱們要使用以前必須先註冊這個推送,用戶要容許這個程序進行推送json
註冊過程:app
if (IS_IOS8) { //1.建立消息上面要添加的動做(按鈕的形式顯示出來) UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init]; action.identifier = @"action";//按鈕的標示 action.title=@"Accept";//按鈕的標題 action.activationMode = UIUserNotificationActivationModeForeground;//當點擊的時候啓動程序 // action.authenticationRequired = YES; // action.destructive = YES; UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; action2.identifier = @"action2"; action2.title=@"Reject"; action2.activationMode = UIUserNotificationActivationModeBackground;//當點擊的時候不啓動程序,在後臺處理 action.authenticationRequired = YES;//須要解鎖才能處理,若是action.activationMode = UIUserNotificationActivationModeForeground;則這個屬性被忽略; action.destructive = YES; //2.建立動做(按鈕)的類別集合 UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init]; categorys.identifier = @"alert";//這組動做的惟一標示,推送通知的時候也是根據這個來區分 [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)]; //3.建立UIUserNotificationSettings,並設置消息的顯示類類型 UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil nil]]; [application registerUserNotificationSettings:notiSettings]; }else{ [application registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound]; } - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { // UIUserNotificationSettings *settings = [application currentUserNotificationSettings]; // UIUserNotificationType types = [settings types]; // //只有5跟7的時候包含了 UIUserNotificationTypeBadge // if (types == 5 || types == 7) { // application.applicationIconBadgeNumber = 0; // } //註冊遠程通知 [application registerForRemoteNotifications]; }
咱們如今僅僅是註冊了通知的設置,還要註冊推送通知的行爲,在iOS8中,行爲能直接在推送消息進行,如回覆消息,拒絕消息等總結就是三個方法進行註冊ide
咱們如何能進行這些行爲,首先咱們需註冊這些行爲。ui
Actionsspa
UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init]; acceptAction.identifier = @"RickAction"; acceptAction.title = @"Accept"; acceptAction.activationMode = UIUserNotificationActivationModeBackground; acceptAction.destructive = NO; acceptAction.authenticationRequired = NO;
Categories代理
UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init]; inviteCategory.identifier = @"INVITE_CATEGORY"; [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault];
咱們須要注意這個UIUserNotificationActionContextDefault,若是咱們使用這個,咱們會獲得這個推送行爲,Maybe和Acceptcode
咱們還可使用UIUserNotificationActionContextMinimal獲得的是Decline和Accept行爲orm
Settingstoken
在這些行爲註冊以後,咱們加上以前提到的推送設置就完成了註冊推送的這個流程了
NSSet *categories = [NSSet setWithObjects:inviteCategory, nil nil]; UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert ; UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories]; [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
遠程推送,全部消息大小不超過2KB,咱們獲取遠程推送的json格式的消息,解析這個消息就是咱們的遠程推送了:
{ 「aps」: { "content-available": 1, "alert": "This is the alert text", "badge": 1, "sound": "default" } }
若要使用遠程推送,知足兩個條件:1、用戶須要調用註冊用戶推送registerUserNotificationSettings;2、在info.plist文件中UIBackgroundModes必須包含遠程通知。
[[UIApplication sharedApplication] registerForRemoteNotifications]; - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *token=[NSString stringWithFormat:@"%@",deviceToken]; token=[token stringByReplacingOccurrencesOfString:@"<" withString:@""]; token=[token stringByReplacingOccurrencesOfString:@">" withString:@""]; token=[token stringByReplacingOccurrencesOfString:@" " withString:@""]; } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { }
iOS7通知代理方法
後來又增長了本地通知的代理方法
iOS8的推送代理方法只有兩個了
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler { } - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler { } - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler { if ([identifier isEqualToString:@"RickAction"]) { [self handleAcceptActionWithNotification:notification]; } completionHandler(); } - (void)handleAcceptActionWithNotification:(UILocalNotification*)notification { }
這個推送是新的API纔有的特性,必須配合CLLocation定位一塊兒使用。
//Location Notification CLLocationManager *locMan = [[CLLocationManager alloc] init]; locMan.delegate = self; [locMan requestWhenInUseAuthorization]; #pragma mark - CLLocationManager - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { BOOL canUseLocationNotifications = (status == kCLAuthorizationStatusAuthorizedWhenInUse); if (canUseLocationNotifications) { [self startShowLocationNotification]; } } - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { CLRegion *region = notification.region; if (region) { } } - (void)startShowLocationNotification { CLLocationCoordinate2D local2D ; local2D.latitude = 123.0; local2D.longitude = 223.0; UILocalNotification *locNotification = [[UILocalNotification alloc] init]; locNotification.alertBody = @"你接收到了"; locNotification.regionTriggersOnce = YES; locNotification.region = [[CLCircularRegion alloc] initWithCenter:local2D radius:45 identifier:@"local-identity"]; [[UIApplication sharedApplication] scheduleLocalNotification:locNotification]; }
若是沒有開啓Core Location 那麼上面的didReceiveLocalNotification不會被調用
最後再總結一下,整個推送流程我以爲是這樣子的,先註冊推送,而後推送消息,客戶端接收推送消息,執行推送行爲。若是有錯誤,還請在文章下面評論,歡迎指正。