iOS8新特性之交互式远程通知

一、通过调用
[[UIApplication sharedApplication] registerForRemoteNotifications];来实现

application:didRegisterForRemoteNotificationsWithDeviceToken:和application:didFailToRegisterForRemoteNotificationsWithError:的回调

二、设置 UIUserNotificationAction和UIMutableUserNotificationCategory

UIUserNotificationAction的设置:

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

//User notificaton aciton的唯一标示
[cancelAction setIdentifier:@"CancelNotificationActionIdentifier"];

//User notificaton aciton button的显示标题
[cancelAction setTitle:@"Cancel"];

//UIUserNotificationActivationModeForeground 激活App并打开到前台展示
//UIUserNotificationActivationModeBackground 在后台激活App,测试时发现如果没有启动App(App不在后台也没有打开),那么执行这种模式下的操作,App不会打开;如果App已经在前台了,那么执行这种模式下的操作,App依然在前台
[cancelAction setActivationMode:UIUserNotificationActivationModeBackground];

//如果设置为YES,执行这个操作的时候必须解锁设备,反之,无需解锁。
//如果activationMode为UIUserNotificationActivationModeForeground时,authenticationRequired被作为YES处理。
//测试时发现,如果用户设备有密码,在锁屏时authenticationRequired设置为YES执行操作时需要用户输入密码,如果设置为NO则不需要用户输入密码,如果用户设备没有密码,那么无论如何设置都不需要输入密码。
[cancelAction setAuthenticationRequired:YES];

//标示操作按钮是否为红色,只有在锁屏和从通知中心向左滑动出现时才有这种突出显示,在顶部消息展示时没有这种突出效果。
[cancelAction setDestructive:YES];

UIMutableUserNotificationCategory的设置:

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

//category的唯一标示,identifier的值与payload(从服务器推送到客户端的内容)中category值必须一致。
[notificationCategory setIdentifier:@"NotificationCategoryIdentifier"];

//UIUserNotificationAction 数组,如果设置为nil,那么将不会显示操作按钮。
//UIUserNotificationActionContextDefault 通知操作的默认Context,在这种情况下,你可以指定4个自定义操作。(还未验证)
UIUserNotificationActionContextMinimal 通知操作的最小Context,在这种情况下,你可以指定2个自定义操作。(还未验证)
[notificationCategory setActions:@[acceptAction, cancelAction]                forContext:UIUserNotificationActionContextDefault];

三、设置 UIUserNotificationSettings
设置通知所支持的类型和Category,这里没有难点,不过多解释。

UIUserNotificationType notificationTypes = (UIUserNotificationTypeAlert|                                        UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);

NSSet *categoriesSet = [NSSet 
setWithObject:notificationCategory];

UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationTypes                                                                          categories:categoriesSet];

四、注册通知 registerUserNotificationSettings

[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

五、处理回调事件

- (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();
    }
}

例子:

//Types
    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

    //Actions
    UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];

    acceptAction.identifier = @"ACCEPT_IDENTIFIER";
    acceptAction.title = @"Accept";

    acceptAction.activationMode = UIUserNotificationActivationModeForeground;
    acceptAction.destructive = NO;
    acceptAction.authenticationRequired = NO;

    //Categories
    UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init];

    inviteCategory.identifier = @"INVITE_CATEGORY";

    [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault];

    [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextMinimal];


    NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];


    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories];

    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

    //注册远程通知
    [[UIApplication sharedApplication] registerForRemoteNotifications];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值