iOS10新特性-UserNotifications(二)

我的Blog链接

上篇文章主要介绍了新的通知框架的基本使用,这篇文章主要说一下多媒体通知扩展

扩展(Notification Extension)

  • Notification Service Extension
    收到远程推送通知之后,我们可以利用Notification Service Extension这个扩展进行通知的修改。
    iOS10新特性-UserNotifications(二)_第1张图片
    un_service_extension.png

建好target之后可以看到,系统就为我们实现了基本的方法。我这里在收到通知之后修改了body

import UserNotifications

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        
        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
            if bestAttemptContent.categoryIdentifier == "ceshi" {
                bestAttemptContent.body = "\(bestAttemptContent.body) 加上修改内容了!"
            }
            contentHandler(bestAttemptContent)
        }
    }
    
    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
}

可以看到结果:


iOS10新特性-UserNotifications(二)_第2张图片
un.extension.modify.png

注:Notification Service Extension 现在只对远程推送的通知有效。因此Demo中我集成了极光推送,为了方便测试使用。

喵神认为这个特性可以方便传输安全性的信息。下面是他的blog原话:

使用在本机截取推送并替换内容的方式,可以完成端到端 (end-to-end) 的推送加密。你在服务器推送 payload 中加入加密过的文本,在客户端接到通知后使用预先定义或者获取过的密钥进行解密,然后立即显示。这样一来,即使推送信道被第三方截取,其中所传递的内容也还是安全的。使用这种方式来发送密码或者敏感信息,对于一些金融业务应用和聊天应用来说,应该是必备的特性。

  • Notification Content Extension
    我们可以利用Notification Content Extension这个扩展进行通知视图的自定义。
    iOS10新特性-UserNotifications(二)_第3张图片
    un.content.create.png

NotificationViewController继承于普通的UIViewController,我们可以自己定义各种UI。这里就是创建好之后,默认自带的一个label。

注意:需要在info.plist文件里设置对应的UNNotificationExtensionCategory要不然,这个自定义的UI是不起作用的。

iOS10新特性-UserNotifications(二)_第4张图片
un.content.info.plist.png
iOS10新特性-UserNotifications(二)_第5张图片
un.content.plist.categoryIden.png
#import "NotificationViewController.h"
#import 
#import 

@interface NotificationViewController () 

@property IBOutlet UILabel *label;

@end

@implementation NotificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any required interface initialization here.
}

- (void)didReceiveNotification:(UNNotification *)notification {
    self.label.text = @"这是自定义的label";//notification.request.content.body;
}

效果图:


iOS10新特性-UserNotifications(二)_第6张图片
un.content.customUI.png

多媒体通知(通知中携带图片、音频、视频)

具体文件类型和大小限制如下:


iOS10新特性-UserNotifications(二)_第7张图片
un.attachment.supportFileType.png
  • 为本地通知增加多媒体内容
    //多媒体通知(发送一个图片)
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"attachment_pic" ofType:@"jpg"];
    NSURL *imageURL = [NSURL fileURLWithPath:imagePath];
    UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"attachment.identifier" URL:imageURL options:nil error:nil];
    content.attachments = @[attachment,attachment];

效果:


iOS10新特性-UserNotifications(二)_第8张图片
attachment_pic.png
iOS10新特性-UserNotifications(二)_第9张图片
attachment_pic_big.png

可以添加音频:

//多媒体通知(发送一个mp3)
    NSString *mp3Path = [[NSBundle mainBundle] pathForResource:@"Fly" ofType:@"mp3"];
    NSURL *mp3URL = [NSURL fileURLWithPath:mp3Path];
    NSError *error = nil;
    UNNotificationAttachment *mp3Attachment = [UNNotificationAttachment attachmentWithIdentifier:@"mp3.attachment.identifier" URL:mp3URL options:nil error:&error];
    if (error) {
        NSLog(@"error = %@", error);
    }
    content.attachments = @[mp3Attachment];

结果:


iOS10新特性-UserNotifications(二)_第10张图片
un.test.mp3.png

如果同时添加了图片和mp3也就是添加多个附件的时候,只会展示第一个。

因为没有合适的视频,视频我估计也就差不多,我这边就不测试了。

  • 为远程通知增加多媒体内容
    远程通知携带多媒体内容需要借助上面提到的Notification Service Extension,在拦截到通知的时候,可以是一个远程的url,也可以是bundle中的一个url,在处理完之后,更改通知的attachment就可以了。

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