iOS8新特性之互動式通知

來源:互聯網
上載者:User

標籤:ios   interactive   notification   

iOS8以前的遠程通知想必大家都非常熟悉了,這裡不做過多介紹,直接介紹iOS8中的互動式遠程通知。


一、通過調用 [[UIApplicationsharedApplication]registerForRemoteNotifications];來實現

application:didRegisterForRemoteNotificationsWithDeviceToken:和application:didFailToRegisterForRemoteNotificationsWithError:的回調


二、設定 UIUserNotificationAction和UIMutableUserNotificationCategory

UIUserNotificationAction的設定:

UIMutableUserNotificationAction *cancelAction = [[UIMutableUserNotificationAction alloc] init];[cancelAction setIdentifier:@"CancelNotificationActionIdentifier"];[cancelAction setTitle:@"Cancel"];[cancelAction setActivationMode:UIUserNotificationActivationModeBackground];[cancelAction setAuthenticationRequired:YES];[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];
<strong><em>identifier</em></strong>
category的唯一標示,identifier的值與payload(從伺服器推送到用戶端的內容)中category值必須一致。

actions 

UIUserNotificationAction 數組,如果設定為nil,那麼將不會顯示操作按鈕。

context 

UIUserNotificationActionContextDefault 通知操作的預設Context,在這種情況下,你可以指定4個自訂動作。(還未驗證)

UIUserNotificationActionContextMinimal 通知操作的最小Context,在這種情況下,你可以指定2個自訂動作。(還未驗證)


三、設定 UIUserNotificationSettings

UIUserNotificationType notificationTypes = (UIUserNotificationTypeAlert|                                            UIUserNotificationTypeSound|                                            UIUserNotificationTypeBadge);NSSet *categoriesSet = [NSSet setWithObject:notificationCategory];UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationTypes                                                                                     categories:categoriesSet];
設定通知所支援的類型和Category,這裡沒有痛點,不過多解釋。

四、註冊通知 registerUserNotificationSettings

[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
在iOS8中通過以上方式註冊通知,可以根據作業系統版本做區分處理

五、處理回調事件

- (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();    }}
<strong><em>application</em></strong>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">收到通知的對象</span>

identifier

UIUserNotificationAction的唯一標示

userInfo

payload的內容

completionHandler

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


我測試用的paload是

{    aps =     {        alert = "Thank you very much";        badge = 1;        category = NotificationCategoryIdentifier;        sound = "ping.caf";    };}


iOS8新特性之互動式通知

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.