目前分爲四個推送:用戶推送,本地推送,遠程推送,地理位置推送。數組
1、經過調用 [[UIApplicationsharedApplication]registerForRemoteNotifications];來實現服務器
application:didRegisterForRemoteNotificationsWithDeviceToken:和application:didFailToRegisterForRemoteNotificationsWithError:的回調app
2、設置 UIUserNotificationAction和UIMutableUserNotificationCategoryide
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";
};
}