iOS开发

IOS 10 新特性以及介绍-Notifications

2016-09-28  本文已影响121人  i_belive

<pre>上一篇中讲到Message中的一些特性,本次主要讲下Notification的特性</pre>

iOS10相对于之前的版本,主要是开发者可以使用Extension的形式修改和展示内容,主要是<code>UNNotificationServiceExtension</code>和<code>UNNotificationContentExtension</code>。同时也添加了其他方面的支持,例如<code>UNNotificationTrigger</code>、<code>UNNotificationAttachment</code>、<code>UNNotificationAction</code>。本次讲的主要有以下几点:

1、UNNotificationTrigger(通知触发条件设定)

主要是针对本地通知触发时机的设置,可以使用下面方式设置:

  1. UNPushNotificationTrigger:这个是苹果通知服务的 Trigger,对外没有暴露任何接口属性,不需要开发者创建,由系统创建。
  2. UNTimeIntervalNotificationTrigger(时间触发器):通过初始化方法设置通知的timeInterval(触发间隔)
    和repeats(是否重复触发),若 repeats 设置为 false,通知将在 timeInterval 之后被推送。
  3. UNCalendarNotificationTrigger(日期触发器):类似 UNTimeIntervalNotificationTrigger,也是时间触发器,只不过初始化时第一个参数为 DateComponents类型,可设置具体的触发时间比如8:00 AM等,也可设置是否重复触发。
  4. UNLocationNotificationTrigger(位置触发器):通过设置 CLRegion类型参数设置位置信息,当用户处于某一区域时,自动推送通知。

使用:

  //设置Notification内容
  UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
  content.title = [NSString localizedUserNotificationStringForKey:@"title!" arguments:nil];
  content.body = [NSString localizedUserNotificationStringForKey:@"body" arguments:nil];
  content.subtitle = [NSString localizedUserNotificationStringForKey:@"subtitle" arguments:nil];
  content.sound = [UNNotificationSound defaultSound];
  
  //初始化时间触发器
  UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
  UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"Identifier"
                                                                        content:content
                                                                        trigger:trigger];
  
  // 请求计划推送
  [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
      if(error)
      {
          NSLog(@"%@",error);
      }
  }];

2、UNNotificationAttachment(通知附件)

苹果所支持的推送附件类型包含视频,音频,图片,苹果的文档中对文件的类型和大小做了如下限制:传送门
使用说明:

设置<code>UNMutableNotificationContent</code>如下属性

@property (NS_NONATOMIC_IOSONLY, copy) NSArray <UNNotificationAttachment *> *attachments```

通过URL地址下载资源,然后将资源设置到request.content.attachments

示例代码:

- (void)handlerImageWith:(NSString *)attachUrl{
   
   [self downloadFileAndSave:[NSURL URLWithString:attachUrl] handler:^(NSURL *localUrl) {
       if (localUrl){
           UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"image_download" URL:localUrl options:nil error:nil];
           self.bestAttemptContent.attachments = @[attachment];
       }
       self.contentHandler(self.bestAttemptContent);
   }];

}

- (void)downloadFileAndSave:(NSURL *)url handler:(void (^)(NSURL *))handler{
   NSURLSession *session = [NSURLSession sharedSession];
   
   NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url
                                                       completionHandler:^(NSURL * _Nullable location,
                                                                           NSURLResponse * _Nullable response,
                                                                           NSError * _Nullable error) {
                                                           NSURL *localURL = nil;
                                                           if(!error){
                                                               NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
                                                               // response.suggestedFilename : 建议使用的文件名,一般跟服务器端的文件名一致
                                                               NSString *file = [caches stringByAppendingPathComponent:response.suggestedFilename];
                                                               
                                                               // 将临时文件剪切或者复制Caches文件夹
                                                               NSFileManager *mgr = [NSFileManager defaultManager];
                                                               
                                                               // AtPath : 剪切前的文件路径
                                                               // ToPath : 剪切后的文件路径
                                                               [mgr moveItemAtPath:location.path toPath:file error:nil];
                                                               
                                                               if (file && ![file  isEqualToString: @""])
                                                               {
                                                                   localURL = [NSURL URLWithString:[@"file://" stringByAppendingString:file]];
                                                               }
                                                           }
                                                           handler(localURL);
                                                       }];
   [downloadTask resume];
} 

注意事项:

if(notification.request.content.attachments && notification.request.content.attachments.count > 0){
        NSURL *imageUrl = notification.request.content.attachments[0].URL;
        if([imageUrl startAccessingSecurityScopedResource]){
            NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
            UIImage *image = [[UIImage alloc] initWithData:imageData];
            self.imageView.image = image;
            [imageUrl stopAccessingSecurityScopedResource];
        }
    }

3、UNNotificationContentExtension(通知内容扩展)

通知内容扩展需要新建一个 UNNotificationContentExtension Target,之后只需在 viewcontroller 的中实现相应的接口,即可以对 app 的通知页面进行自定义扩展,扩展主要用于自定义 UI。扩展页面样式可以在 plist 中配置,字段说明如下:

使用说明:
远程和本地通知最终都可以使用此扩展自定义 UI,只需将通知的 categoryIdentifier(类型标示) 加入到 plist 中即可。

4、UNNotificationServiceExtension (通知服务扩展)

UNNotificationServiceExtension 提供在远程推送将要被 push 出来前,处理推送显示内容的机会。此时可以对通知的 request.content 进行内容添加,如添加附件,userInfo 等。服务器推送实例:

{
  "aps" : {
    "alert" : {
      "title" : "title",
      "body" : "Your message Here"      
    },
     // 开启可变内容
    "mutable-content" : "1",   
    // 加入自定义数据,图片 url 路径
    "image":"http://....jpg"               
  }
}

示例代码:

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    NSDictionary *apsDic = [request.content.userInfo objectForKey:@"aps"];
    NSString *attachUrl = [apsDic objectForKey:@"image"];
    if(attachUrl){
        [self handlerImageWith:attachUrl];
    }
}

5、UNNotificationAction(通知响应事件)

代表一个响应通知的事件。可以为每个通知设置不同的交互事件。下拉推送通知或处在锁屏界面侧滑通知时,会出现交互按键。
交互事件主要分为以下两类:

示例代码:

- (void)registerNotificationCategory{
    UNTextInputNotificationAction *inputAction = [UNTextInputNotificationAction actionWithIdentifier:@"action_reply" title:@"回复" options:UNNotificationActionOptionForeground];
    UNNotificationAction *cancelAction = [UNNotificationAction actionWithIdentifier:@"action_cancel" title:@"取消" options:UNNotificationActionOptionDestructive];
    
    UNNotificationCategory *saySomething = [UNNotificationCategory categoryWithIdentifier:saySomethingCategory actions:@[inputAction,cancelAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
    [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:saySomething]];
}

响应处理:

// If implemented, the method will be called when the user taps on one
// of the notification actions. The completion handler can be called
// after handling the action to dismiss the notification and forward the
// action to the app if necessary.
- (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption option))completion;{
    
}
// 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{

}

补充:iOS10中可以在前台中收到通知了,需要添加一下代码:

\\ notificationHandler实现UNUserNotificationCenterDelegate
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self.notificationHandler];
上一篇 下一篇

猜你喜欢

热点阅读