【Swift】UNNotificationServiceExtension

一、简介

  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请自行百度。

原文地址:https://www.cnblogs.com/xjf125/p/11875402.html

时间: 2024-08-02 18:33:47

【Swift】UNNotificationServiceExtension的相关文章

【Swift】学习笔记(二)——基本运算符

运算符是编程中用得最多的,其包括一元,二元和三元 三种运算符.swift也和其它编程语言一样基本就那些,下面总结一下,也有它特有的运算符,比如区间运算符 1.一元运算符 =   赋值运算符,用得最多的啦,其不带任何返回值 +  加法(数字相加,也可用于字符拼接var ss = "a"+"b") -   减法 *   乘法 /   除法 % 求余(负号忽略,浮点数也可以求余) >  大于 <  小于 2.二元运算符 ++  自增(就是i = i + i的缩

【Swift】 应用内显示 AppStore 某个应用的详情

前言 应用内跳转到 AppStore 的文章很多,一般都是用 SKStoreProductViewController 来实现的,不知道有没有在意一个问题:打开很慢!!怎么忍?! 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 一般网上的文章的代码: func openAppStore(url: String){ if let number = url.rangeOfStrin

【Swift】iOS开发小坑历险记(二)

前言 这个系列主要是一些开发中遇到的坑记录分享,有助于初学者跨过这些坑,攒够 7 条发一篇. 声明  欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 1.用动画更新约束没有动画效果? 缺少 layoutIfNeeded ,事例: UIView.animateWithDuration(0.15, animations: { () -> Void in self.heightConst

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

GET & POST 的对比 源码: https://github.com/SpongeBob-GitHub/Get-Post.git 1. URL - GET 所有的参数都包含在 URL 中 1. 如果需要添加参数,脚本后面使用 `?` 2. 参数格式:值对 参数名=值 3. 如果有多个参数,使用 `&` 连接 4. 在 get 方法中,url字符串中不能包含中文或者特殊符号 空格 如果出现,需要添加百分号转义 - POST URL中不包含参数,直接指定登录脚本即可 2. Request

【Swift】Alamofile网络请求数据更新TableView的坑

写这篇BLOG前,有些话不得不提一下,就仅当发发恼骚吧... 今天下午为了一个Alamofire取得数据而更新TableView的问题,查了一下午的百度(360也是见鬼的一样),竟然没有一个简单明了的回答, 而唯一几个比较接近答案的,说要 self.tableView.reloadData(),也没有贴上代码,说要放在哪个函数内, 都犹抱琵琶半遮面,让初学者自己采坑,于是郁闷了一下午,刚刚回到家,试想想,要不试试英文网,毕竟Swift就是人家老外的, 说不定老外会告诉你,怎么取得数据并绑定Tab

【swift】BlockOperation和GCD实用代码块

//BlockOperation // // ViewController.swift import UIKit class ViewController: UIViewController { @IBOutlet weak var image1: UIImageView! @IBOutlet weak var image2: UIImageView! @IBOutlet weak var image3: UIImageView! @IBOutlet weak var image4: UIIma

【Swift】swift中使用kvc赋值的时候,注意事项

swift中使用kvc赋值的时候.假如给基础数据类型赋值,必须先给基础数据类型的变量一个初始值. 否则将会报错. var id : Int = 0 setValue(dict["id"], forKey: "id")

【Swift】学习笔记(八)——类和结构体

1.类和结构体的定义 <pre name="code" class="html">struct Resolution { var width = 0 var height = 0 } class VideoMode { var resolution = Resolution() var interlaced = false var frameRate = 0.0 var name: String? //值为可选String类型的name } 类:keyw

【Swift】闭包(Closures)

闭包表达式(Closure Expressions) 嵌套函数 是一个在较复杂函数中方便进行命名和定义自包含代码模块的方式.当然,有时候撰写小巧的没有完整定义和命名的类函数结构也是很有用处的,尤其是在您处理一些函数并需要将另外一些函数作为该函数的参数时. 闭包表达式是一种利用简洁语法构建内联闭包的方式. 闭包表达式提供了一些语法优化,使得撰写闭包变得简单明了. 下面闭包表达式的例子通过使用几次迭代展示了sort(_:)方法定义和语法优化的方式. 每一次迭代都用更简洁的方式描述了相同的功能. so