众所周知,iOS中消息推送扮演了不可或缺的位置。无论是本地通知仍是远程通知无时不刻的在影响着咱们的用户体验,以至于在iOS10的时候苹果对推送大规模重构,独立了已UserNotifications
和UserNotificationsUI
两个单独的framework,可见重要性一斑。针对于WWDC18苹果又给咱们带来了什么惊喜呢?安全
随着手机上应用的增多,尤为QQ和微信这两大聊天工具,当手机锁屏的时候,伴随着就是好满屏的推送消息。这一现象不知你们有没有觉着不高效和体验性比较差呢?苹果针对锁屏状况下,对消息进行了分组,从而有效的提升了用户的交互体验,分组形式以下:bash
threadIdentifier
属性则以此属性为依据,进行分组。let content = UNMutableNotificationContent()
content.title = "Notifications Team"
content.body = "WWDC session after party"
content.threadIdentifier = "notifications-team-chat"//经过这个属性设置分组,若是此属性没有设置则以APP为分组依据
复制代码
当苹果自动将推送消息的归拢到一块儿的时候,最下边会有一个消息摘要。默认格式是:n more notifications from xxx
。不过此格式咱们是能够定制的。微信
let summaryFormat = "%u 更多消息啦啦"
return UNNotificationCategory(identifier: "category-identifier",
actions: [],
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: nil,
categorySummaryFormat: summaryFormat,
options: [])
复制代码
let content = UNMutableNotificationContent()
content.body = "..."
content.summaryArgument = "OceanFish"
复制代码
同一个category的不一样格式,苹果会将其合并在一块儿;而且不一样的
summaryArgument
苹果也会将其默认合并到一块儿进行显示session
也能够经过
let summaryFormat = NSString.localizedUserNotificationString(forKey: "NOTIFICATION_SUMMARY", arguments: nil)
来进行本地化服务app
有时会出现另外一个场景:好比发送了2条推送消息,一条是“你有3个邀请函”,另外一条是“你有5个邀请函”。那摘要则会显示你有2更多消息。这显然不是咱们想要的!咱们最好的指望确定是"你有8个邀请函"。那这种效果怎么显示呢?ide
苹果给咱们提供了另一个属性,结合上边的摘要(Summary)格式定制咱们能够实现以上效果。工具
let content = UNMutableNotificationContent()
content.body = "..."
content.threadIdentifier = "..."
content.summaryArgument = "Song by Song"
content.summaryArgumentCount = 3
复制代码
当多个消息归拢到一块儿的时候,苹果会将summaryArgumentCount
值加在一块儿,而后进行显示ui
以前消息是不支持交互的和动态更改Action的,好比界面有个空心喜欢按钮,用户点击则变成了实心喜欢按钮;有个Acction显示“喜欢”,用户点击以后变成"不喜欢"spa
UUNNotificationExtensionUserInteractionEnabled
为YES
import UserNotificationsUI
class NotificationViewController: UIViewController, UNNotificationContentExtension {
@IBOutlet var likeButton: UIButton?
likeButton?.addTarget(self, action: #selector(likeButtonTapped), for: .touchUpInside)
@objc func likeButtonTapped() {
likeButton?.setTitle("♥", for: .normal)
likedPhoto()
}
}
复制代码
// Notification Content Extensions
class NotificationViewController: UIViewController, UNNotificationContentExtension {
func didReceive(_ response: UNNotificationResponse, completionHandler completion:
(UNNotificationContentExtensionResponseOption) -> Void) {
if response.actionIdentifier == "like-action" {
// Update state...
let unlikeAction = UNNotificationAction(identifier: "unlike-action",
title: "Unlike", options: [])
let currentActions = extensionContext?.notificationActions
let commentAction = currentActions![1]
let newActions = [ unlikeAction, commentAction ]
extensionContext?.notificationActions = newActions
}
}
}
复制代码
performNotificationDefaultAction()
用于点击推送的时候启动应用;dismissNotificationContentExtension()
用于关闭锁屏页面的推送具体一条消息code
这个主要是苹果针对消息增长了一个“管理”的按钮,消息左滑便可出现。
import UIKit
import UserNotifications
class AppDelegate: UIApplicationDelegate, UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
openSettingsFor notification: UNNotification? ) {
}
}
复制代码
临时受权主要体现就是推送消息过来会有两个按钮,会主动让用户本身选择
let notificationCenter = UNUserNotificationCenter.current()
noficationCenter.requestAuthorization(options: [.badge,.alert,.sound,.provisional]) { (tag, error) in
}
复制代码
在申请权限的时候,加上provisional
便可。
好比家庭安全、健康、公共安全等因素的时候。此消息须要用户必须采起行动。最简单的一个场景是家里安装了一个摄像头,咱们去上班了,此时若是家中有人,则摄像头会推送消息给咱们。
证书申请 https://developer.apple.com/contact/request/notifications-critical-alerts-entitlement/
本地权限申请
let notificationCenter = UNUserNotificationCenter.current()
noficationCenter.requestAuthorization(options: [.badge,.alert,.sound,.criticalAlert]) { (tag, error) in
}
复制代码
在申请权限的时候,加上criticalAlert
。
let content = UNMutableNotificationContent()
content.title = "WARNING: LOW BLOOD SUGAR"
content.body = "Glucose level at 57."
content.categoryIdentifier = "low-glucose—alert"
content.sound = UNNotificationSound.criticalSoundNamed(@"warning-sound" withAudioVolume: 1.00)
复制代码
// Critical alert push payload
{
// Critical alert push payload
{
"aps" : {
"sound" : {
"critical": 1,
}
}
"name": "warning-sound.aiff",
"volume": 1.0
}
}
复制代码
至此WWDC中关于推送都已经整理完毕。你们有不懂的欢迎留言相互交流