版本记录
版本号 | 时间 |
---|---|
V1.0 | 2020.05.16 星期六 |
前言
我们做APP很多时候都需要推送功能,以直播为例,如果你关注的主播开播了,那么就需要向关注这个主播的人发送开播通知,提醒用户去看播,这个只是一个小的方面,具体应用根据公司的业务逻辑而定。前面已经花了很多篇幅介绍了极光推送,其实极光推送无非就是将我们客户端和服务端做的很多东西封装了一下,节省了我们很多处理逻辑和流程,这一篇开始,我们就利用系统的原生推送类结合工程实践说一下系统推送的集成,希望我的讲解能让大家很清楚的理解它。感兴趣的可以看上面几篇。
1. 系统推送的集成(一) —— 基本集成流程(一)
2. 系统推送的集成(二) —— 推送遇到的几个坑之BadDeviceToken问题(一)
3. 系统推送的集成(三) —— 本地和远程通知编程指南之你的App的通知 - 本地和远程通知概览(一)
4. 系统推送的集成(四) —— 本地和远程通知编程指南之你的App的通知 - 管理您的应用程序的通知支持(二)
5. 系统推送的集成(五) —— 本地和远程通知编程指南之你的App的通知 - 调度和处理本地通知(三)
6. 系统推送的集成(六) —— 本地和远程通知编程指南之你的App的通知 - 配置远程通知支持(四)
7. 系统推送的集成(七) —— 本地和远程通知编程指南之你的App的通知 - 修改和显示通知(五)
8. 系统推送的集成(八) —— 本地和远程通知编程指南之苹果推送通知服务APNs - APNs概览(一)
9. 系统推送的集成(九) —— 本地和远程通知编程指南之苹果推送通知服务APNs - 创建远程通知Payload(二)
10. 系统推送的集成(十) —— 本地和远程通知编程指南之苹果推送通知服务APNs - 与APNs通信(三)
11. 系统推送的集成(十一) —— 本地和远程通知编程指南之苹果推送通知服务APNs - Payload Key参考(四)
12. 系统推送的集成(十二) —— 本地和远程通知编程指南之Legacy信息 - 二进制Provider API(一)
13. 系统推送的集成(十三) —— 本地和远程通知编程指南之Legacy信息 - Legacy通知格式(二)
14. 系统推送的集成(十四) —— 发送和处理推送通知流程详解(一)
15. 系统推送的集成(十五) —— 发送和处理推送通知流程详解(二)
16. 系统推送的集成(十六) —— 自定义远程通知(一)
开始
首先看下主要内容
学习如何在推送通知呈现给用户之前修改和增强推送通知,如何围绕推送内容创建自定义UI,等等!内容来自翻译 。
接着看下写作环境
Swift 5, iOS 13, Xcode 11
下面就是正文了。
如果你在过去十年里一直使用移动设备,你可能会遇到无数的推送通知。推送通知允许应用程序向用户广播alerts
—— 即使他们不积极使用设备。
虽然通知可以向用户提供有用的信息,但是通知的真正威力来自于称为富通知(rich notifications)
的概念。富通知允许您拦截通知负载(payloads)
,并为您提供时间以最适合用户需求的方式对其进行修饰(自己加的:就像历史一样,任人装扮)。这允许您显示自定义UI,其中可以包含为用户提供快捷方式的按钮操作。
本教程假设您对推送通知有一定的了解。
本教程将进一步介绍这些知识。您将学习如何修改和增强传入的内容,如何围绕您的推送内容创建自定义UI和更多!
因为推送通知只能在物理设备上工作,所以您需要使用几个属性来配置Apple developer portal
,以便在本教程中使用。Xcode可以通过自动配置为您处理大部分问题。
1. Setting up New App Values
在Xcode中打开starter
起始项目。如果您还没有登录到您的development team
,请转到Preferences
,选择Accounts
选项卡,然后使用+
按钮登录。
接下来,在文件导航器File navigator
中选择Wendercast
项目节点。确保选择了Wendercast
目标。在中间的窗格中,转到Signing & Capabilities
选项卡并选中Automatically manage signing
框。
设置以下值:
- 1) 从
team
下拉列表中选择您的开发团队。 - 2) 在
Bundle Identifier
中,输入唯一的Bundle ID
。 - 3) 在
App Groups
下,点击+
。保留group
前缀,并输入上一步中使用的相同bundle ID
。 - 4) 确保
Push Notifications
出现在capabilities
列表中。
如果您看到两个不同的Signing (debug)
和Signing (release)
部分,请在Automatically manage signing, Team
和Bundle Identifier
这两个部分上进行配置。使用相同的值。
最后,打开DiskCacheManager.swift
。然后将groupIdentifier
属性更新为您的新app group ID
:
let groupIdentifier = "[[group identifier here]]"
2. Creating an Authentication Key
对于这一步,登录到Apple developer门户Apple developer portal。单击Account
选项卡,然后按照以下说明操作:
- 1) 从左侧侧边栏选择
Certificates, ID, & Profiles
。 - 2) 从
Certificates, ID, & Profiles
屏幕中选择Keys
。 - 3) 单击
+
按钮。 - 4) 填写
Key Name
字段,选择Apple Push Notifications Service (APNs)
,然后点击Continue
。
- 5) 点击
Register
。 - 6) 记下您的
Key ID
。您将在以后的步骤中需要它。 - 7) 单击
Download
将.p8
文件保存到磁盘。 - 8) 再次记下你的
team ID
,它显示在页面的右下角(在您的姓名或公司名称下面)。
这已经是很多了。现在您已经完成了门户的工作,是时候重新配置starter
应用程序了。
这一步将允许应用程序读写共享应用程序容器。在添加扩展(extension)
时,这是必要的,这样应用程序和扩展(app and extension)
都可以访问Core Data store
。
你将创建不是一个而是两个扩展,应用程序将使用Core Data
。
Running the App
Wendercast
应用程序获取并解析Ray Wenderlic
h的播客feed
。然后在UITableView
中显示结果。用户可以点击任何一集,打开一个详细视图,开始播放该集。他们也有能力喜欢任何一集。
如前所述,应用程序使用Core Data
在会话之间进行持久化。在feed
刷新期间,应用程序只插入新剧集。
首先,确保您的iPhone
是所选设备。然后构建并运行。您应该会看到一个podcast
列表和一个启用通知的即时提示。利用Allow
。
点击列表中的任何一集,你就会看到一个详细的屏幕。选择的播客应该立即开始播放。
现在,您将发送一个测试推送通知(push notification)
。
打开Xcode
控制台。你应该看到device token
打印在日志:
Permission granted: true
Notification settings:
<UNNotificationSettings: 0x2808abaa0; authorizationStatus: Authorized,
notificationCenterSetting: Enabled, soundSetting: Enabled,
badgeSetting: Enabled, lockScreenSetting: Enabled,
carPlaySetting: NotSupported, announcementSetting: NotSupported,
criticalAlertSetting: NotSupported, alertSetting: Enabled,
alertStyle: Banner,
groupingSetting: Default providesAppNotificationSettings: No>
Device Token: [[device-token-here]]
保存这个值,因为您现在就需要它。
1. Testing a Push Notification
在测试推送通知之前,您需要能够发送它们。
发送推送通知需要调用Apple Push Notification Server (APNS)
上的REST API
。这绝对不是最简单的方法——尤其是当你必须手工操作的时候。
幸运的是,还有另一种方法。让一个应用程序为你做。
遵循这些步骤:
- 1) 下载并安装Push Notifications Tester app。
- 2) 启动app
注意:如果
macOS
提示无法启动应用程序,在Finder
中右键单击应用程序并选择Open
。
- 3) 选择
iOS
选项卡。 - 4) 选择
Authentication
下的Token
。 - 5) 单击
Select P8
,然后选择保存到磁盘的. p8
文件。 - 6) 在
Enter key id
区域中输入.p8 key ID
。您在下载p8
文件之前复制了它。 - 7) 在
Enter team id
区域中输入您的team ID
。 - 8) 在
Body
下输入应用程序的bundle ID
。 - 9) 输入上一步保存的
device token
。 - 10) 保留
Collapse id
为空。 - 11) 将有
payload
按照原样放在body
中。
结果,应该是这样的:
将应用程序放置到设备后台,但不锁设备。在Push Notifications Tester
应用程序中点击Send
。你会收到一个推送通知和一条成功消息。
现在,您已经完成了所有的设置,现在是深入研究代码的时候了。
Modifying Push Content
苹果创造了一种方法,可以在service extensions
交付之前修改推送内容。Service extensions
允许您拦截来自APNS
的推送内容,对其进行修改,然后将修改后的有效负载(payload)
交付给用户。
Service extensions
位于APNS server
和推送通知的最终内容之间:
1. Introducing Service Extensions
service extension
获得有限的执行时间来对传入的推送负载执行一些逻辑。你可以做的一些事情来修改和增加推送负载:
- 更新推送的标题、副标题或主体
(body)
。 - 添加媒体附件
(attachment)
到推送。
2. Adding a Service Extension
回到Wendercast
项目,通过点击File ▸ New ▸ Target…
创建一个新的target
。
选择Notification Service Extension
并单击Next
:
将扩展名为WendercastNotificationService
。字段应该是这样的:
验证字段输入之后,单击Finish
。如果出现提示,不要激活新scheme
。
这样,您就向项目中添加了notification service extension
,并准备拦截一些推送通知。
3. Exposing Files to the Extension
首先,将项目中包含的一些helper
类公开给您创建的新services extension
。在Network
目录下,你会找到两个文件:ImageDownloader.swift
和NetworkError.swift
。
在File inspector
中,选中WendercastNotificationService
target
的复选框,这样它们就可以在services extension
中使用:
4. Saving Files
在WendercastNotificationService
组中,打开NotificationService.swift
并在文件的顶部导入UIKit
。
import UIKit
在NotificationService
的底部,添加这个方便的方法来保存一个图像到磁盘:
private func saveImageAttachment(
image: UIImage,
forIdentifier identifier: String
) -> URL? {
// 1
let tempDirectory = URL(fileURLWithPath: NSTemporaryDirectory())
// 2
let directoryPath = tempDirectory.appendingPathComponent(
ProcessInfo.processInfo.globallyUniqueString,
isDirectory: true)
do {
// 3
try FileManager.default.createDirectory(
at: directoryPath,
withIntermediateDirectories: true,
attributes: nil)
// 4
let fileURL = directoryPath.appendingPathComponent(identifier)
// 5
guard let imageData = image.pngData() else {
return nil
}
// 6
try imageData.write(to: fileURL)
return fileURL
} catch {
return nil
}
}
以下是你所做的:
- 1) 获取对
temp
文件目录的引用。 - 2) 使用
temp
文件目录,使用惟一的字符串创建目录URL。 - 3)
FileManager
负责创建用于存储数据的实际文件。调用createDirectory(at: winthintermediatedirectory:attributes:)
创建一个空目录。 - 4) 基于
image identifier
创建文件URL。 - 5) 从映像创建一个
Data
对象。 - 6) 尝试将文件写入磁盘。
现在您已经创建了一种存储图像的方法,接下来将把注意力转移到下载实际的图像上。
5. Downloading an Image
添加另一种方法从URL下载图像:
private func getMediaAttachment(
for urlString: String,
completion: @escaping (UIImage?) -> Void
) {
// 1
guard let url = URL(string: urlString) else {
completion(nil)
return
}
// 2
ImageDownloader.shared.downloadImage(forURL: url) { result in
// 3
guard let image = try? result.get() else {
completion(nil)
return
}
// 4
completion(image)
}
}
它的作用很简单:
- 1) 确保您可以从
urlString
属性创建URL
。 - 2) 使用链接到此
target
的ImageDownloader
尝试下载。 - 3) 确保生成的映像不是
nil
。 - 4) 调用
completion
块,传递UIImage
结果。
Modifying the Push Content From the Server
您需要向发送到设备的push
有效负载添加一些额外的值。进入Push Notification Tester
应用,用这个payload
替换body
:
{
"aps": {
"alert": {
"title": "New Podcast Available",
"subtitle": "Antonio Leiva – Clean Architecture",
"body": "This episode we talk about Clean Architecture with Antonio Leiva."
},
"mutable-content": 1
},
"podcast-image": "https://koenig-media.raywenderlich.com/uploads/2016/11/Logo-250x250.png",
"podcast-guest": "Antonio Leiva"
}
这个payload
给推送通知一个title, a subtitle and a body
。
注意,mutable-content
的值为1
。该值告诉iOS
内容是可更新的,从而在将其交付给用户之前调用service extension
。
添加了两个自定义键:podcast-image
和podcast-guest
。在向用户显示通知之前,您将使用与这些键关联的值来更新推送内容。
现在发送带有以上内容的推送。您将看到一个更新的推送通知,其中添加了标题、副标题和描述。它是这样的:
1. Updating the Title
notification service extension
的强大功能来自于它拦截推送的能力。在本节中,您将对此有所了解。在WendercastNotificationService
中,打开NotificationService.swift
并定位didReceive(_:withContentHandler:)
。当推送通知出现时,将调用此函数,并允许您对将要显示给用户的内容执行一些调整。
将if let
块替换为:
if let bestAttemptContent = bestAttemptContent {
// 1
if let author = bestAttemptContent.userInfo["podcast-guest"] as? String {
// 2
bestAttemptContent.title = "New Podcast: \(author)"
}
// 3
contentHandler(bestAttemptContent)
}
以下是你所做的:
- 1) 在通知内容
notification content
的userInfo
中检查键podcast-guest
的值。 - 2) 如果存在,请更新
notification content
的title
。 - 3) 调用
completion handler
来交付推送。如果podcast-author
的值不存在,push
将显示原始标题。
构建和运行。然后将应用程序发送到后台。现在从push Notifications Tester
应用程序发送一个推送。你会看到一个标题更新的推送通知,现在包含了podcast-author
条目的值。
2. Adding an Image
接下来,您将使用push
有效负载payload
下载表示podcast
的图像。
将contentHandler(bestAttemptContent)
替换为以下内容:
// 1
guard let imageURLString =
bestAttemptContent.userInfo["podcast-image"] as? String else {
contentHandler(bestAttemptContent)
return
}
// 2
getMediaAttachment(for: imageURLString) { [weak self] image in
// 3
guard
let self = self,
let image = image,
let fileURL = self.saveImageAttachment(
image: image,
forIdentifier: "attachment.png")
// 4
else {
contentHandler(bestAttemptContent)
return
}
// 5
let imageAttachment = try? UNNotificationAttachment(
identifier: "image",
url: fileURL,
options: nil)
// 6
if let imageAttachment = imageAttachment {
bestAttemptContent.attachments = [imageAttachment]
}
// 7
contentHandler(bestAttemptContent)
}
这是上面做的事情:
- 1) 检查您是否有
podcast-image
的值。如果没有,则调用content handler
来交付推送和返回。 - 2) 调用便利方法来检索带有从
push payload
接收到的URL的图像。 - 3) 当
completion block
触发时,检查图像是否为nil
;否则,请尝试将其保存到磁盘。 - 4) 如果有
URL
,则操作成功;如果这些检查失败,调用content handler
并返回。 - 5) 使用文件URL创建一个
UNNotificationAttachment
。将标识符图像命名为最终通知上的映像。 - 6) 如果创建附件成功,则将其添加到
bestAttemptContent
上的attachments
属性。 - 7) 调用
content handler
来传递推送通知。
构建和运行。将app发送到后台。
现在,从Push Notifications Tester
应用程序发送另一个带有相同负载的推送。你应该会看到推送的右上角的图片:
下拉通知。你会看到它会扩展了,并使用图像填充屏幕的大部分:
现在可以更新notification content
了。接下来,您将进一步围绕推送内容创建自定义UI。
Creating a Custom UI
通过在推送内容的顶部添加一个自定义UI,您可以进一步使用富通知。这个界面将通过一个应用扩展来取代标准的推送通知UI。
这个界面是一个符合UNNotificationContentExtension
的视图控制器(view controller)
。通过实现didReceive(_:)
,您可以拦截通知并设置自定义界面。
1. Adding the Target
和之前一样,点击File New Target…
,选择Notification Content Extension
:
将content extension
命名为WendercastNotificationContent
,并确保区域正确(使用您自己的team and organization name
):
这一次,单击schema activation confirmation
屏幕上的Activate
。
2. Configuring Info.plist
接下来,您需要配置新target
的Info.plist
信息,以显示您的content extension
。
- 1) 在
WendercastNotificationContent
组中,打开Info.plist
。 - 2) 扩展
NSExtension dictionary
字典。 - 3) 扩展
NSExtensionAttribute
字典。 - 4) 将
UNNotificationExtensionCategory
中的值更新为new_podcast_available
。 - 5) 单击
+
向NSExtensionAttribute
字典添加一个新的键-值对。 - 6) 将键
UNNotificationExtensionDefaultContentHidden
添加为布尔值,并将值设置为YES
。 - 7) 再添加一个布尔键值对。将键设置为
UNNotificationExtensionUserInteractionEnabled
,将值设置为YES
。
你最后的Info.plist
信息应该是这样的(键的顺序并不重要):
以下是这些参数的用途:
-
UNNotificationExtensionCategory
- 该条目的值必须与传入推送内容中的值匹配。
iOS
需要这个来决定使用哪个UI来显示通知。 - 您需要它,因为您可能希望为不同类别的推送通知提供自定义UI。
- 如果该值丢失,
iOS
将不会调用您的扩展。
- 该条目的值必须与传入推送内容中的值匹配。
-
UNNotificationExtensionInitialContentSizeRatio
- 这个值是一个介于
0
和1
之间的数字。它表示自定义界面的宽高比。 - 默认值
1
告诉iOS你的初始界面高度和它的宽度是一样的。 - 例如,如果你将这个值设置为
0.5
,那么这将告诉iOS
你的界面的高度是其宽度的一半。 - 这是一个估计值,允许
iOS
设置界面的初始大小,防止不必要的调整。
- 这个值是一个介于
-
UNNotificationExtensionDefaultContentHidden
- 当设置为YES时,推送内容的标准
title, subtitle and body
是不可见的。 - 当设置为
NO
时,标准推送内容将显示在自定义UI
下。
UNNotificationExtensionUserInteractionEnabled
- 当设置为
YES
时,它允许用户与UIKit
元素交互。
- 当设置为YES时,推送内容的标准
3. Adding the App Group
添加你为main app target
创建的相同的app group
:
- 1) 在文件导航器
File navigator
中,单击project
节点。 - 2) 选择
WendercastNotificationContent
的target
。 - 3) 选择
Signing & Capabilities
选项卡。 - 4) 单击
+ Capability
。 - 5) 选择
App Groups
。 - 6) 选择您在本教程开始时创建的相同应用
group ID
。
Building the Custom UI
您可能希望将重点放在构建content extension
逻辑上,而不是将时间浪费在冗长的Interface Builder
上。为了帮助您,使用已经包含的一个准备使用的故事板。您可以随意使用它来替换由Xcode自动创建的故事板。
以下是如何做到这一点:
- 1) 在Xcode中,删除
MainInterface.storyboard
,来自WendercastNotificationContent
组。选择Move to Trash
提示。 - 2) 从
ContentStoryboard
文件夹拖动MainInterface.storyboard
文件到Xcode
的WendercastNotificationContent
文件夹。 - 3) 选中
Copy items if needed
,并在Add to targets
列表中选择WendercastNotificationContent
的target
。 - 4) 单击
Finish
。 - 5) 在Xcode中打开故事板。
您将看到这个故事板为notification UI
提供了一个很好的起点(starting point)
。它为title, podcast image, favorites button and play button
提供UI元素。另外,自动布局约束已经设置好了。
现在你可以专注于构建视图控制器。
Setting up NotificationViewController
NotificationViewController
负责为用户显示自定义通知视图(custom notification view)
。你将做出必要的修改来呈现你的新推送通知视图。
1. Adding Shared Files
打开以下文件并将WendercastNotificationContent
添加到它们在File inspector
中的target membership
中:
CoreDataManager.swift
PodcastItem.swift
Podcast.swift
DiskCacheManager.swift
Wendercast.xcdatamodel
您可以通过选中WendercastNotificationContent
框来做到这一点。
这将使数据模型和网络类对content extension
可用。
注意:
Xcode
在这一点上可能有些混乱,显示了一些错误。您可以通过简单地按下Command-B
键构建目标来清除它们。
2. Customizing the UI
在NotificationViewController
中查看类声明。删除由Xcode自动生成的代码:
label
viewDidLoad()
didReceive(_:)‘s body
然后在类声明下面添加以下输出:
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var favoriteButton: UIButton!
@IBOutlet weak var podcastTitleLabel: UILabel!
@IBOutlet weak var podcastBodyLabel: UILabel!
然后添加一个属性来保存当前的podcast
:
var podcast: Podcast?
最后,添加以下方便的方法从共享data store
加载podcast
:
private func loadPodcast(from notification: UNNotification) {
// 1
let link = notification.request.content.userInfo["podcast-link"] as? String
// 2
guard let podcastLink = link else {
return
}
// 3
let podcast = CoreDataManager.shared.fetchPodcast(
byLinkIdentifier: podcastLink)
// 4
self.podcast = podcast
}
以下是你正在做的事情:
- 1) 尝试从附加到通知的
userInfo
对象获取到podcast
的链接。podcast
链接是podcast
在Core Data
存储中的唯一标识符。 - 2) 如果链接不存在,请尽早返回。
- 3) 使用该链接从
Core Data
存储获取Podcast
模型对象。 - 4) 设置一个带有响应的
podcast
。
3. Customizing the Notification
将didReceive(_:)
的body
替换为:
// 1
loadPodcast(from: notification)
// 2
let content = notification.request.content
podcastTitleLabel.text = content.subtitle
podcastBodyLabel.text = content.body
// 3
guard
let attachment = content.attachments.first,
attachment.url.startAccessingSecurityScopedResource()
else {
return
}
// 4
let fileURLString = attachment.url
guard
let imageData = try? Data(contentsOf: fileURLString),
let image = UIImage(data: imageData)
else {
attachment.url.stopAccessingSecurityScopedResource()
return
}
// 5
imageView.image = image
attachment.url.stopAccessingSecurityScopedResource()
一旦推送通知来了,下面是你正在做的事情:
- 1) 调用便利方法从
Core Data
存储加载podcast
。这设置了以后使用的podcast
。 - 2) 将
title and body labels
设置为从推送通知接收到的值。 - 3) 尝试访问附加到
service extension
的媒体。如果没有,早点return
。对startAccessingSecurityScopedResource()
的调用允许您访问附件。 - 4) 获取附件的URL。尝试从磁盘检索它并将数据转换为图像。如果失败了,早点
return
。 - 5) 如果图像检索成功,设置
podcast
图像并停止访问资源。
4. Implementing the Favorite Action
UI
有一个按钮,用于将通知的podcast
添加到收藏列表中。这是一个如何让推送通知变得可操作和酷的完美例子。
添加以下方法来处理点击收藏按钮:
@IBAction func favoriteButtonTapped(_ sender: Any) {
// 1
guard let podcast = podcast else {
return
}
// 2
let favoriteSetting = podcast.isFavorite ? false : true
podcast.isFavorite = favoriteSetting
// 3
let symbolName = favoriteSetting ? "star.fill" : "star"
let image = UIImage(systemName: symbolName)
favoriteButton.setBackgroundImage(image, for: .normal)
// 4
CoreDataManager.shared.saveContext()
}
要处理收藏按钮点击,你是:
- 1) 检查确保
podcast
已经设置好。 - 2) 根据
podcast
切换isFavorite
。 - 3) 更新收藏按钮UI以匹配模型状态。
- 4) 使用更改更新
Core Data
存储。
这足以测试对content extension
的第一组更改。将scheme
设置回Wendercast
,然后构建并运行,并将应用程序放到后台。接下来,从Push Notifications Tester app
发送以下内容:
{
"aps": {
"category": "new_podcast_available",
"alert": {
"title": "New Podcast Available",
"subtitle": "Antonio Leiva – Clean Architecture",
"body": "This episode we talk about Clean Architecture with Antonio Leiva."
},
"mutable-content": 1
},
"podcast-image": "https://koenig-media.raywenderlich.com/uploads/2016/11/Logo-250x250.png",
"podcast-link": "https://www.raywenderlich.com/234898/antonio-leiva-s09-e13",
"podcast-guest": "Antonio Leiva"
}
一旦通知出现,就拉下它。你会看到你的更新自定义界面:
点击收藏(Favorite)
按钮,你会看到它改变了状态。如果将Wendercast
应用程序打开到同一个podcast
,您会注意到收藏按钮的状态与notification UI
的状态相匹配。太棒了!
5. Implementing the Play Action
现在你将实现一个深层链接到应用程序的播放动作。添加以下方法到NotificationViewController
:
@IBAction func playButtonTapped(_ sender: Any) {
extensionContext?.performNotificationDefaultAction()
}
这告诉notification extension
打开应用程序并以标准推送方式交付所有内容。
接下来,看看Wendercast/App/SceneDelegate.swift
中的扩展。这段代码执行了很多相同的工作,你一直在做的扩展:
- 寻找
podcast link
的存在。 - 尝试从
Core Data
存储获取podcast
。 - 告诉
PodcastFeedTableViewController
加载指定的podcast
。 - 播放指定的
podcast
构建和运行。将应用程序发送到后台,并推送与上次发送的相同的通知负载(payload)
。这一次,点击播放按钮。该应用程序将会深入到podcast
的细节,并开始播放该集。你已经做到了!
恭喜你!你已经深入研究了丰富的推送通知。你学会了如何:
- 修改推送内容
- 附加媒体
- 创建自定义用户界面
- 导航扩展和它们的主应用程序之间的交互
要了解更多,可以研究通知操作以及它们如何应用于通知内容扩展(notification content extensions)
。通过启用可定制的UI和用户交互(customizable UI and user interaction)
,各种可能性实际上是无穷无尽的!首先,看看官方文件:
后记
本篇主要讲述了APNs从工程配置到自定义通知UI全流程解析,感兴趣的给个赞或者关注~~~