iOS8新特性之交互式通知

目前分爲四個推送:用戶推送,本地推送,遠程推送,地理位置推送。數組

  1. if (IS_IOS8) {  
  2.         //1.建立消息上面要添加的動做(按鈕的形式顯示出來)  
  3.         UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];  
  4.         action.identifier = @"action";//按鈕的標示  
  5.         action.title=@"Accept";//按鈕的標題  
  6.         action.activationMode = UIUserNotificationActivationModeForeground;//當點擊的時候啓動程序  
  7.         //    action.authenticationRequired = YES;  
  8.         //    action.destructive = YES;  
  9.           
  10.         UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  
  11.         action2.identifier = @"action2";  
  12.         action2.title=@"Reject";  
  13.         action2.activationMode = UIUserNotificationActivationModeBackground;//當點擊的時候不啓動程序,在後臺處理  
  14.         action.authenticationRequired = YES;//須要解鎖才能處理,若是action.activationMode = UIUserNotificationActivationModeForeground;則這個屬性被忽略;  
  15.         action.destructive = YES;  
  16.           
  17.         //2.建立動做(按鈕)的類別集合  
  18.         UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];  
  19.         categorys.identifier = @"alert";//這組動做的惟一標示,推送通知的時候也是根據這個來區分  
  20.         [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];  
  21.           
  22.         //3.建立UIUserNotificationSettings,並設置消息的顯示類類型  
  23.         UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil nil]];  
  24.         [application registerUserNotificationSettings:notiSettings];  
  25.           
  26.     }else{  
  27.         [application registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];  
  28.     }  

 

 

 

 

 

 

1、經過調用 [[UIApplicationsharedApplication]registerForRemoteNotifications];來實現服務器

application:didRegisterForRemoteNotificationsWithDeviceToken:application:didFailToRegisterForRemoteNotificationsWithError:的回調app

 

2、設置 UIUserNotificationActionUIMutableUserNotificationCategoryide

UIUserNotificationAction的設置:測試

 

UIMutableUserNotificationAction *cancelAction = [[UIMutableUserNotificationAction alloc] init];ui

[cancelAction setIdentifier:@"CancelNotificationActionIdentifier"];spa

[cancelAction setTitle:@"Cancel"];對象

[cancelAction setActivationMode:UIUserNotificationActivationModeBackground];事件

[cancelAction setAuthenticationRequired:YES];ci

[cancelAction setDestructive:YES];

 

 

identifier

User notificaton aciton的惟一標示

title

User notificaton aciton button的顯示標題

 

activationMode

 

UIUserNotificationActivationModeForeground 激活App並打開到前臺展現

 

UIUserNotificationActivationModeBackground 在後臺激活App,測試時發現若是沒有啓動App(App不在後臺也沒有打開),那麼執行這種模式下的操做,App不會打開;若是App已經在前臺了,那麼執行這種模式下的操做,App依然在前臺。

 

authenticationRequired

若是設置爲YES,執行這個操做的時候必須解鎖設備,反之,無需解鎖。

若是activationMode爲UIUserNotificationActivationModeForeground時,authenticationRequired被做爲YES處理。

測試時發現,若是用戶設備有密碼,在鎖屏時authenticationRequired設置爲YES執行操做時須要用戶輸入密碼,若是設置爲NO則不須要用戶輸入密碼,若是用戶設備沒有密碼,那麼不管如何設置都不須要輸入密碼。

 

destructive

標示操做按鈕是否爲紅色,只有在鎖屏和從通知中心向左滑動出現時纔有這種突出顯示,在頂部消息展現時沒有這種突出效果。

 

UIMutableUserNotificationCategory的設置:

UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];

[notificationCategory setIdentifier:@"NotificationCategoryIdentifier"];

[notificationCategory setActions:@[acceptAction, cancelAction]

                      forContext:UIUserNotificationActionContextDefault];

 

identifier

category的惟一標示,identifier的值與payload(從服務器推送到客戶端的內容)中category值必須一致。

actions

UIUserNotificationAction 數組,若是設置爲nil,那麼將不會顯示操做按鈕。

 

context

UIUserNotificationActionContextDefault 通知操做的默認Context,在這種狀況下,你能夠指定4個自定義操做。(還未驗證)

 

UIUserNotificationActionContextMinimal 通知操做的最小Context,在這種狀況下,你能夠指定2個自定義操做。(還未驗證)

 

 

3、設置 UIUserNotificationSettings

 

UIUserNotificationType notificationTypes = (UIUserNotificationTypeAlert|

                                            UIUserNotificationTypeSound|

                                            UIUserNotificationTypeBadge);

NSSet *categoriesSet = [NSSet setWithObject:notificationCategory];

UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationTypes

                                                                                     categories:categoriesSet];

設置通知所支持的類型和Category,這裏沒有難點,不過多解釋。

 

4、註冊通知 registerUserNotificationSettings

[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

 

 

在iOS8中經過以上方式註冊通知,能夠根據操做系統版本作區分處理

 

5、處理回調事件

 

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler

{

    if([identifier isEqualToString:@"CancelNotificationActionIdentifier"])

    {

        NSLog(@"You chose cancel action.");

    }

    else if ([identifier isEqualToString:@"AcceptNotificationActionIdentifier"])

    {

        NSLog(@"You chose accept action.");

    }

    

    if(completionHandler)

    {

        completionHandler();

    }

}

 

application

收到通知的對象

 

identifier

UIUserNotificationAction的惟一標示

 

userInfo

payload的內容

completionHandler

當執行完指定的操做後,必須在最後調用這個方法

 

我測試用的paload是

 

{

    aps =     {

        alert = "Thank you very much";

        badge = 1;

        category = NotificationCategoryIdentifier;

        sound = "ping.caf";

    };

}

相關文章
相關標籤/搜索