一、简介

  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. 使用Spring-boot-starter标准改造项目内的RocketMQ客户端组件

    一.背景介绍 我们在使用Spring Cloud全家桶构建微服务应用时,经常能看到spring-boot-xxx-starter的依赖,像spring-boot-starter-web.spring- ...

  2. Android仿美团地址选择

    最近做了这个功能,分享一下,用的是百度地图api,和美团外卖的地址选择界面差不多,也就是可以搜索或者滑动地图展示地址列表给用户选择,看下效果图先. 文章重点 1.展示地图并定位到“我”的位置2.滑动地 ...

  3. 最简单的ArcGIS Engine应用程序(中)

    上一篇中只能添加代码中指定的shp文件(最简单的ArcGIS Engine应用程序(上)) 为了让用户可以浏览磁盘加载指定的shp文件,需做一下调整. 从工具箱往窗体上添加一个OpenFileDial ...

  4. 安装Ubuntu时界面显示不全,无法点击continue按钮

    按住win键和鼠标左键即可拖动界面

  5. 攻防世界(XCTF)WEB(进阶区)write up(二)

    国庆就要莫得了   起床刷几道 NewsCenter mfw Training-WWW-Robots NaNNaNNaNNaN-Batman bug NewsCenter search传参那里发现有注 ...

  6. Oracle基于布尔的盲注总结

    0x01 decode 函数布尔盲注 decode(字段或字段的运算,值1,值2,值3) 这个函数运行的结果是,当字段或字段的运算的值等于值1时,该函数返回值2,否则返回3 当然值1,值2,值3也可以 ...

  7. Flash安全总结

    ActionScript AS是基于ECMAScript的语言,为了交互的需要flash应用引入ActionScript.ActionScript一共有三个版本,其中3.0较之前两个版本变化很大.Ac ...

  8. C#读取邮件附件的方法

    基于需求需要从邮件里读取附件,从网络搜索整理如下: 1 使用 Spire.Email 从官网下载安装并引用,地址:https://www.e-iceblue.com/Download/email-fo ...

  9. SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图

    在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: ...

  10. vue使用 封装websocket心跳包

    ---恢复内容开始--- 这套代码可以拿过去直接用 一些注意我会在下面代码中加上注释: 谢谢支持 核心代码 //这里需要引入vuex import store from './store'; let ...