iOS10新特性-UserNotifications

iOS10增加了新的通知框架UserNotifications,整合了本地通知和APNS,新的API使用起来特别舒服。这篇文章就大概介绍一下新的框架。

新的特性:

  • 通知可以附加图片、音频、视频
  • 可以对通知更新、删除
  • 统一本地通知和APNS

权限申请

 @import UserNotifications;
 // 请求使用通知
 [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            //用户同意使用通知
        }
    }];

第一次调用这个方法时,会弹出一个系统弹窗。


iOS10新特性-UserNotifications_第1张图片
UNNotification_author.png

要注意的是,一旦用户拒绝了这个请求,再次调用该方法也不会再进行弹窗,想要应用有机会接收到通知的话,用户必须自行前往系统的设置中为你的应用打开通知。

远程推送

用户同意了通知申请之后,就可以进行本地通知了。如果要进行远程推送,还需要获取token。然后服务器根据这个token,向Apple Push Notification Service服务器提交请求,然后APNS通过token识别用户,推送给用户。

//向APNS请求token
    [[UIApplication sharedApplication] registerForRemoteNotifications];
//请求token成功
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
}

通知权限
可以通知下面的这个方法,检查当前APP的通知权限。

[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
    }];

发送通知

// 创建通知
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.title = @"这是一条通知的标题";
    content.body = @"这是一条通知的内容";
    content.categoryIdentifier = @"ceshi";
    
    // 创建发送触发
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
    
    // 发送请求标识符
    NSString *identifier = @"com.hanwei.firstNotificationIndentifier";
    
    // 创建一个通知请求
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
    
    
    // 将请求添加到通知中心
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {
            NSLog(@"通知添加成功");
        }
    }];

发送通知成功之后,就可以回到首页,或者锁屏查看了。

iOS10新特性-UserNotifications_第2张图片
UNNotification_home.png

取消和更新通知

远程推送只能更新,不能取消已经展示过的通知。
取消

    // 移除展示过的通知
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[identifier]];
        
    });
    
     //移除还未展示的通知
    [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[identifier]];

更新

    // 不管通知是否已经展示过,都可以根据标示进行通知的更新
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
//        NSLog(@"两秒钟后更新通知内容");
        
        // 创建发送触发
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:2 repeats:NO];
        
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        content.title = @"这是一条通知的标题";
        content.body = @"这是新的内容";
        content.categoryIdentifier = @"ceshi";
        
        // 创建一个通知请求
        // 同一个标示的话就会覆盖之前的通知
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
        
        [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (!error) {
                NSLog(@"新的通知添加成功");
            }
        }];
    });

通知处理

通知处理就是,处理用户直接和通知的交互。比如微信发过来一条消息,你可以直接在通知上进行回复,而不用打开微信app。

应用内展示通知

我们知道当APP在前台使用的过程中,在默认情况下,通知是没有任何提示的。但是要想有展示的效果,可以通过下面的代码设置。

// The method will be called on the delegate only if the application is in the foreground. If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented. The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list. This decision should be based on whether the information in the notification is otherwise visible to the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
    // 用户在前台使用APP的时候,收到通知,会调用此方法.
    // 写这个回调代表,会在APP打开的情况下,弹出通知。
    // 如果不复写次方法,或者`completionHandler();`,APP在前台收到通知将不会弹出提示。
    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}

这是通知中的代理方法,需要先设置代理、签协议。

To guarantee that your app is able to respond to actionable notifications, you must set the value of this property before your app finishes launching. For example, this means assigning a delegate object to this property in an iOS app’s application:willFinishLaunchingWithOptions:or application:didFinishLaunchingWithOptions: method.

需要注意的是,这个代理必须要在程序启动之前设置好。一般在 application:didFinishLaunchingWithOptions:这个方法设置就可以。

    // 设置代理
    [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];

处理用户和通知的交互

/**
     设置通知上面的交互按钮
     UNNotificationActionOptionAuthenticationRequired //操作这个按钮会先检查iPhone是否解锁
     UNNotificationActionOptionDestructive //按钮会被高亮标记(红色)
     UNNotificationActionOptionForeground // 点击按钮会将APP唤起
     */
    UNNotificationAction *unlocking = [UNNotificationAction actionWithIdentifier:@"unlocking"
                                                                           title:@"unlocking"
                                                                         options:UNNotificationActionOptionAuthenticationRequired];
    UNNotificationAction *destructive = [UNNotificationAction actionWithIdentifier:@"destructive"
                                                                             title:@"destructive"
                                                                           options:UNNotificationActionOptionDestructive];
    UNNotificationAction *foreground = [UNNotificationAction actionWithIdentifier:@"foreground"
                                                                            title:@"foreground"
                                                                          options:UNNotificationActionOptionForeground];
    UNTextInputNotificationAction *input = [UNTextInputNotificationAction actionWithIdentifier:@"text" title:@"text" options:UNNotificationActionOptionAuthenticationRequired textInputButtonTitle:@"text_btn" textInputPlaceholder:@"placeholder"];
    // 注意:这里的category的标示如果和发送通知时,写的category标示不一样的时候,发过来的通知不会显示action!!!发送通知的时候可以不写标示。
    // 我这里是,两边都写得 “ceshi”
    UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"ceshi"
                                                                              actions:@[unlocking, destructive, foreground, input] intentIdentifiers:@[@""]
                                                                              options:UNNotificationCategoryOptionNone];
    
    [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:category]];
iOS10新特性-UserNotifications_第3张图片
UNNotification_text_01.png
iOS10新特性-UserNotifications_第4张图片
UNNotification_text_02.png
// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
{
    //当用户是通过和通知的交互进入app的时候,拿到通知对象。
    UNNotificationContent *content = response.notification.request.content;
    
    if ([content.categoryIdentifier isEqualToString:@"ceshi"]) {
        
        if ([response.actionIdentifier isEqualToString:@"text"]) {
            UNTextInputNotificationResponse *textResp = (UNTextInputNotificationResponse *)response;
            NSLog(@"输入文字的内容是%@", textResp.userText);
        }
        else if ([response.actionIdentifier isEqualToString:@"unlocking"]) {
            NSLog(@"unlocking");
        }
        else if ([response.actionIdentifier isEqualToString:@"destructive"]) {
            NSLog(@"destructive");
        }
        else if ([response.actionIdentifier isEqualToString:@"foreground"]) {
            NSLog(@"foreground");
        }
    }
    
    completionHandler();
}

刚才我看评论,有一人问我是不是自己实践的,我都是自己尝试过,才发上去的,多谢大家支持。这里放上我的Demo

关于通知发送富文本部分,正在整理,等整理好了,我会继续补充!

2016-10-09补充:
多媒体通知已经更新:iOS10新特性-UserNotifications(二)

参考:
https://onevcat.com/2016/08/notification/

你可能感兴趣的:(iOS10新特性-UserNotifications)