WWDC2016-session707 Introduction to Notifications

User Notifications

IMG_0014.PNG

通知中心向来是 iOS上非常重要的一部分。在 iOS 10 上,Apple 对通知进行了加强和革新。现在,为了更好地处理和管理通知,和本地及推送通知相关的 API 被封装到了全新的框架 UserNotifications.framework 中。在 iOS 10 中,开发者的服务器有机会在本地或者远程通知发送给用户之前再进行修改。

另外,在之前加入了 notification action 以及 text input 的基础上,iOS 10 又新增了为通知添加音频,图片,甚至视频的功能。现在,你的通知不仅仅是提醒用户回到应用的入口,更成为了一个展示应用内容,向用户传递多媒体信息的窗口。

主要内容

  • Registration
  • content
  • schedule
  • manage
  • actions
  • service Extension

在设置通知的时候,需要先进行注册,获取授权

registration(注册)



    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            //允许通知
            NSLog(@"用户允许通知");
        }else
            NSLog(@"用户不允许通知");
        }
    }];```
-------
#### 获取授权之后,应该是需要做的是设置通知的内容,下面的是local notification 设置示例 
###content(通知内容)
```objc
    //本地通知

    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];

    content.title = @"大医生,糖尿病义诊进行时";
    content.subtitle = @"义诊:糖尿病患者该吃些什么";
    content.body = @"应该多吃清淡食物,适量的水果!";

远程通知的内容


WWDC2016-session707 Introduction to Notifications_第1张图片
IMG_0018.PNG

设置好通知内容,需要选择对应的触发器

trigger iOS10 有4种触发器

UNPushNotificationTrigger
触发APNs 服务,系统自动设置

UNTimeIntervalNotificationTrigger

    // Fire in 30 minutes (30 times 60 seconds)
    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
                                                  triggerWithTimeInterval:(30*60) repeats: NO];

UNCalendarNotificationTrigger

    NSDateComponents* date = [[NSDateComponents alloc] init];
    date.hour = 8;
    date.minute = 30;
    
    UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
                                   triggerWithDateMatchingComponents:date repeats:YES];

UNLocationNotificationTrigger
基于位置触发,可以是到达某地,或者是离开某地,或者都有

    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(37.335400, -122.009201);
    CLCircularRegion* region = [[CLCircularRegion alloc] initWithCenter:center
                                                                 radius:2000.0 identifier:@"Headquarters"];
    region.notifyOnEntry = YES;
    region.notifyOnExit = NO;
    
    UNLocationNotificationTrigger* trigger = [UNLocationNotificationTrigger
                                              triggerWithRegion:region repeats:NO];

schedule

//添加发送通知请求
    UNNotificationRequest *requset = [UNNotificationRequest requestWithIdentifier:@"samplerequset" content:content trigger:[UNTimeIntervalNotificationTrigger triggerWithTimeInterval:2.0 repeats:NO]];
//通知加到通知中心
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:requset withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"%@",error);
    }];

manage

接收到通知需要进行相关处理

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
    NSLog(@"%@",notification);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
    NSLog(@"%@",response);
}

action

收到通知的时候需要进行怎样的自定义处理


![IMG_0013.PNG](http://upload-images.jianshu.io/upload_images/425102-2daa8937425cc9da.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


    //需要确定是那一条通知 
    UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"samplerequset" title:@"new" options:UNNotificationActionOptionAuthenticationRequired];
    
    UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"samplerequset" actions:@[action] minimalActions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
    
    [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:category]];

service Extension

通知中展示多媒体内容,如视频,GIF,图片等,可自定义UI
详细在session708 Advanced Notifications
下周详细说明下。

你可能感兴趣的:(WWDC2016-session707 Introduction to Notifications)