iOS8互动的新通知

iOS8一旦远程通知想必大家都很熟悉。不要做过多的描述在这里,直接推出iOS8交互式远程通知。

再看互动的通知电话,显示的形式

                

如今来看一下详细实现方式

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

application:didRegisterForRemoteNotificationsWithDeviceToken:application:didFailToRegisterForRemoteNotificationsWithError:的回调


二、设置 UIUserNotificationActionUIMutableUserNotificationCategory

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处理。

測试时发现,假设用户设备有password,在锁屏时authenticationRequired设置为YES运行操作时须要用户输入password,假设设置为NO则不须要用户输入password,假设用户设备没有password,那么不管怎样设置都不须要输入password。

destructive

标示操作button是否为红色,仅仅有在锁屏和从通知中心向左滑动出现时才有这样的突出显示。在顶部消息展示时没有这样的突出效果。


UIMutableUserNotificationCategory的设置:

UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
[notificationCategory setIdentifier:@"NotificationCategoryIdentifier"];
[notificationCategory setActions:@[acceptAction, cancelAction]
                      forContext:UIUserNotificationActionContextDefault];

identifier

category的唯一标示,identifier的值与payload(从server推送到client的内容)中category值必须一致。

actions 

UIUserNotificationAction 数组。假设设置为nil,那么将不会显示操作button。

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

application

收到通知的对象

identifier

UIUserNotificationAction的唯一标示

userInfo

payload的内容

completionHandler

当运行完指定的操作后,必须在最后调用这种方法


我測试用的payload是

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


版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/blfshiye/p/4854575.html