[Diving into WWDC 2017] Best Practices and What’s New in User Notifications

用户通知的新特性与最佳实践 Notifications overview Hidden content Modifying content Customizing rich notifications User input customization

一 Notification 初步介绍

本地通知(Local Notification)

远程通知(Remote Notification)

通知触发的条件(Notification Triggers)

通知的内容,展现,动作响应,删除等等

通知的扩展(Notification Extensions)

Service Extension Content Extension

二 Hidden Notification Content( iOS 11 新特性)

(What’s new)iOS 11 之新特性

通知内容可以自定义展示与隐藏,主要包括一下几点:

Extended support to all apps Global setting Settings per app API to customize notification content

iOS 11 系统,我们可以在手机系统设置里面设置 Push 的展示与隐藏属性

隐藏通知内容的实际展示效果

Hidden Notification Content Best practices

Session 内这段讲述了使用 API 自定义通知的推荐实践方式。主要有下面几种:

User defined setting Retrieve settings using showPreviewsSetting Thread identifiers Pluralization

实践核心代码

通知结果展示,预览部分被替换成了 Comment

接下来我们可以看看做更多元化的定制

通知条数控制展示

实际展示效果如下

更多通知属性的自定义设置

例子核心代码展示如下

实际通知展示样式如下

三 Modifying Push Content

Modifying Push Content 就是修改 Push 内容

修改 Push 内容的使用场景

Modifying Push Content Use cases Download media attachments Content encryption** Context specific content—location, time, health data**

实际开发过程中,我们使用 Service Extension 和 Content Extension。

3.1 Service Extension Template 最佳实践

Modifying Push Content Service Extension best practices

主要应用在如下场景

Short execution time and memory limit Fallback Notifications will be presented Different from silent notifications

Service Extension的工作原理

为了开发 Service Extension 的扩展,你需要通过如下方式选择 template。

其核心代码如下。

这样你就完成了一个带下载功能的 Service Extension的编写。

3.2 Content Extension Template 最佳实践

Content Extension 主要是为了完成 Customizing Rich Notifications,为我们的 Push 定制便利化。

Customizing Rich Notifications 的主要工作是:

Motivation App specific look and feel Custom display of notification content Interactive and dynamic UI

Content Extension工作原理简要介绍如下

为了开发 Content Extension 的扩展,你需要通过如下方式选择 template

在下面的例子中,我们需要在 plist 文件中修改添加一些新的设置

例子核心代码如下,代码演示了如何定制自定义的 NotificationViewController

收到 Push 后展示结果如下

3.3 Customize Rich Notifications Best practices

自定义通知的推荐实践,推荐实践主要基于如下几个方面:

Custom UI elements Display all relevant information Reuse app view controllers Correct sizing Fast loading and layout

3.4 User Input Customization Best practices

用户在 Push 处理输入的最佳实践,主要在于以下几个方面:

Media buttons Context specific actions To dismiss or not to dismiss Custom user input view gives you full control

对于 3.3 和 3.4 自定义通知和用户输入的推荐实践,Session 给了一个例子。

其核心代码如下

获取Push后展示UI如下

3.5 Action handling

如果我们需要处理一些交互事件,我们需要加上 Action handling。

Action handling的工作原理如图

利用一个简单的例子展示如何用代码实践

实现的效果如下

更进一步的UIResponder

看看如何处理点击实践,这是推荐的例子。

实际运行UI展示结果如下。


四 总结

Session 708 主要讲述 iOS 11 Push 的新特性以及使用 Push Notifications 的推荐实践,我们可以了解 iOS 11 Push 有关的最新特性;并通过例子了解如何简单方便的实践 Service Extension 和 Content Extension;并对如何定制 Push VC 的 UI 交互方式有比较清晰的理解。

五 参考文献与链接

https://developer.apple.com/videos/play/wwdc2017/708/ https://medium.com/compileswift/whats-new-in-ios-11-notifications-446f22a4c279 https://hackernoon.com/the-must-follow-best-practices-for-your-push-notifications-5f878565d2a9

文章来源:

Author:shajie
link:https://techblog.toutiao.com/2017/07/05/session708/