目前分为四个推送:用户推送,本地推送,远程推送,地理位置推送。数组
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";
};
}