衆所周知,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中關於推送都已經整理完畢。你們有不懂的歡迎留言相互交流