一、简介

  An object that modifies the content of a remote notification before it's delivered to the user.

  是一个能在远程通知传递到用户之前进行修改内容的对象。

二、概述

  A UNNotificationServiceExtension object provides the entry point for a Notification Service app extension, which lets you customize the content of a remote notification before it is delivered to the user. A Notification Service app extension doesn't present any UI of its own. Instead, it is launched on demand when a notification of the appropriate type is delivered to the user’s device. You use this extension to modify the notification’s content or download content related to the extension. For example, you could use the extension to decrypt an encrypted data block or to download images associated with the notification.

  UNNotificationServiceExtension对象提供通知服务应用程序扩展的入口点,允许您在远程通知传递给用户之前对其内容进行自定义。通知服务应用程序扩展本身不提供任何UI,相反,当通知发送到用户设备时,你可以根据需要启动,可以通过通知扩展来修改通知内容或下载关联的扩展内容。例如:你可以使用扩展来加解密数据或下载与通知关联的图像。

  You don't create UNNotificationServiceExtension objects yourself. Instead, the Xcode template for a Notification Service Extension target contains a subclass for you to modify. Use the methods of that subclass to implement your app extension’s behavior. When a remote notification for your app is received, the system loads your extension and calls its didReceive(_:withContentHandler:) method only when both of the following conditions are met:

  你不需要自己创建UNNotificationServiceExtension对象,相反,Xcode模板已经包含了一个通知扩展需要修改的子类,使用子类方法来实现引用程序的扩展行为,当时一个远程通知到应用程序时,系统加载你的扩展并调用didReceive(_:withContentHandler:) 方法,但需要通知满足以下条件:

  The remote notification is configured to display an alert.

  远程推送通知被设置为alert提示
  The remote notification’s aps dictionary includes the mutable-content key with the value set to 1.

  远程通知的aps字典中包含mutable-content键值,而且其值设置为1.

  The didReceive(_:withContentHandler:) method performs the main work of your extension. You use that method to make any changes to the notification’s content. That method has a limited amount of time to perform its task and execute the provided completion block. If your method does not finish in time, the system calls the serviceExtensionTimeWillExpire() method to give you one last chance to submit your changes. If you don't update the notification content before time expires, the system displays the original content.

   didReceive(_:withContentHandler:)方法执行扩展的主要工作。你可以是使用该方法对通知内容做一些更改,该方法提供了有限的时间去执行任务和提供的数据块,如果你的方法在该时间内没有完成,系统会执行serviceExtensionTimeWillExpire()方法给你最后一次机会来提交你的修改,如果你不能在时间过期之前完成更新通知内容,系统会按照原始内容进行显示。

  As for any app extension, you deliver a Notification Service app extension class as a bundle inside your app. The template provided by Xcode configures the Info.plist file automatically for this app extension type. Specifically, it sets the value of the NSExtensionPointIdentifier key to com.apple.usernotifications.service and sets the value of the NSExtensionPrincipalClass key to the name of your UNNotificationServiceExtensionsubclass.

  对于任何应用程序的扩展,都会将通知服务扩展类交付给应用程序中,对于Xcode中提供的末班,在info.plist文件中都会自动扩展类型。具体说,为通知扩展子类添加两个键值NSExtensionPointIdentifier 和 NSExtensionPrincipalClass

  For information about how to set up and send remote notifications, see Setting Up a Remote Notification Server.

  如何设置和发送远程通知,看链接。

三、Subclassing Notes

  The Xcode templates provide a subclass of UNNotificationServiceExtension for you to modify. You must implement the didReceive(_:withContentHandler:) method and use it to process incoming notifications. It is also strongly recommended that you override the serviceExtensionTimeWillExpire() method.

  Xcode模板为通知扩展提供了子类,你必须实现完成以下两个方法。

func didReceive(UNNotificationRequest, withContentHandler: (UNNotificationContent) -> Void)
//Asks you to make any needed changes to the notification and notify the system when you're done.
func serviceExtensionTimeWillExpire()
//Tells you that your extension is about to be terminated.

  如何创建UNNotificationServiceExtension请自行百度。

【Swift】UNNotificationServiceExtension的更多相关文章

  1. 【Swift】iOS开发历险记(二)

    前言 这个系列主要是一些开发中遇到的坑记录分享,有助于初学者跨过这些坑,攒够 7 条发一篇. 声明  欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯 ...

  2. 【Swift】UITableViewCell 中 TTTAttributedLabel 超链接无法点击的问题

    前言 还以为是自己代码写的有问题,用法和别的地方都一样,但是这个是在 UITableViewCell 中使用,另外在 tableHeaderView 中使用也没用这个问题 —— 使用 TTTAttri ...

  3. 【Swift】iOS开发笔记(二)

    前言 这个系列主要是一些开发中遇到的坑记录分享,有助于初学者跨过这些坑,攒够 7 条发一篇. 声明  欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯 ...

  4. 【swift】用Xib实现自定义警告框(Alert)(安卓叫法:Dialog对话框)

    在写这篇博客前,先感谢两篇博客 [如何自定义的思路]:https://www.cnblogs.com/apprendre-10-28/p/10507794.html [如何绑定Xib并且使用]:htt ...

  5. 【Swift】CoreData的使用

    CoreData只是iOS数据持久化的其中一个方法,所有数据持久化如下 1.plist文件(属性列表),通常用于储存用户设置,也zhi可以用于存储捆绑的信息: 2.preference(偏好设置),常 ...

  6. 【Swift】 GET&POST请求 网络缓存的简单处理

     GET & POST 的对比 源码:https://github.com/SpongeBob-GitHub/Get-Post.git 1. URL - GET 所有的参数都包含在 URL 中 ...

  7. 【Swift】Runtime动态性分析

    Swift是苹果2014年发布的编程开发语言,可与Objective-C共同运行于Mac OS和iOS平台,用于搭建基于苹果平台的应用程序.Swift已经开源,目前最新版本为2.2.我们知道Objec ...

  8. 【swift】长按事件绑定,平移滑动事件+坐标获取

    为何把这两个事件归类在一起? 我后来才明白,iOS有一个手势事件(UiGestureRecognizer) 事件里有7个功能,不过我只试过前两个,也就是标题的这两个(长按.平移滑动) UILongPr ...

  9. 【swift】Xcode未响应(卡死、卡住、CPU满载、忙碌、转圈圈)

    在尝试了网上的方法,依然没能解决问题,尝试如下: 1.去自己项目的路径,找到<你的项目名.xcodeproj>,点击[显示包内容],删除xcuserdata文件夹 2.去Library,把 ...

随机推荐

  1. Oracle VM VirtualBOX桥接网卡

    1.在VirtualBOX设置中的网络菜单,连接方式选择桥接网卡 2.启动系统,设置静态IP sudo vim /etc/network/interfaces auto enp0s3         ...

  2. CS184.1X 计算机图形学导论 作业0

    1.框架下载 在网站上下载了VS2012版本的作业0的框架,由于我的电脑上的VS是2017版的,根据提示安装好C++的版本,并框架的解决方案 重定解决方案目标为2017版本. 点击运行,可以出来界面. ...

  3. Jenkins介绍

    持续集成: 持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通过每个成员每天至少集成一次,也就意味着每天可能会发生多次集成.每次集成都通过自动化的构建(包括编译,发布,自动化测试)来验证 ...

  4. 运用第一性原理探寻AI本质

    摘要:使用第一性原理探讨理解AI本质. 文章来源:宜信技术学院|宜信支付结算团队技术分享第二期-支付结算机器学习技术团队负责人 刘创 分享<AI与第一性原理> 分享者:宜信支付结算机器学习 ...

  5. [Noip1997] 棋盘问题(2)

    题目描述 在N×NN \times NN×N的棋盘上(1≤N≤10)(1≤N≤10)(1≤N≤10),填入1,2,…,N21,2,…,N^21,2,…,N2共N2N^2N2个数,使得任意两个相邻的数之 ...

  6. Python开发【第七篇】列表

    问题:当我们要用一系列数字的时候,我们需要将数字进行存储,我们就需要找个容器把数字装起来,我们需要用的时候再拿出来.如何将计算机运算的数据存储在一个地方,同时方便 对数据进行 增.删.改.查 列表 列 ...

  7. libevent::事件::定时器2

    #define evtimer_new(b, cb, arg) event_new((b), -1, 0, (cb), (arg)) #include <cstdio> #include ...

  8. SpringBoot中如何优雅的读取yml配置文件?

    YAML是一种简洁的非标记语言,以数据为中心,使用空白.缩进.分行组织数据,从而使得表示更加简洁易读.本文介绍下YAML的语法和SpringBoot读取该类型配置文件的过程. 本文目录 一.YAML基 ...

  9. JVM(6) 字节码执行引擎

    编译器(javac)将Java源文件(.java文件)编译成Java字节码(.class文件). 类加载器负责加载编译后的字节码,并加载到运行时数据区(Runtime Data Area) 通过类加载 ...

  10. 数据结构(四十五)选择排序(1.直接选择排序(O(n²))2.堆排序(O(nlogn)))

    一.选择排序的定义 选择排序的基本思想是:每次从待排序的数据元素集合中选取最小(或最大)的数据元素放到数据元素集合的最前(或最后),数据元素集合不断缩小,当数据元素集合为空时排序过程结束.常用的选择排 ...