八、處理通知
UserNotifications?框架為我們提供了查找、更新、刪除通知等相關(guān)的?API?方法。其中關(guān)鍵在于?request?的?identifier,即在創(chuàng)建時指定的通知標(biāo)識符。
1,查找通知
(1)獲取所有待推送的通知
UNUserNotificationCenter.current().getPendingNotificationRequests { (requests)?in
????//遍歷所有未推送的request
????for?request?in?requests {
????????print(request)
????}
}
目前我們只有一個未推送的通知:

(2)獲取所有已推送的通知
UNUserNotificationCenter.current().getDeliveredNotifications { (notifications)?in
????//遍歷所有已推送的通知
????for?notification?in?notifications {
????????print(notification)
????}
}
2,更新通知
多次推送同一標(biāo)識符的通知即可進行更新。比如我們有一條標(biāo)識符為“com.hangge.testNotification”的通知還未觸發(fā)推送。如果創(chuàng)建并添加一條同樣標(biāo)示符的通知,那么原先的那條通知就會被替換。(而如果原先的通知已展示,則會在通知中心中更新這條通知)
//使用同樣的請求標(biāo)識符來設(shè)置一個新的通知
let?requestIdentifier =?"com.hangge.testNotification"
let?request =?UNNotificationRequest(identifier: requestIdentifier,
????????????????????????????????????content: content, trigger: trigger)
//將通知請求添加到發(fā)送中心
UNUserNotificationCenter.current().add(request) { error?in
????if?error ==?nil?{
????????print("Time Interval Notification scheduled: \(requestIdentifier)")
????}
}
遠(yuǎn)程推送也可以進行通知的更新:
在使用?Provider API?向?APNs?提交請求時,在?HTTP/2?的?header?中?apns-collapse-id key?的內(nèi)容將被作為該推送的標(biāo)識符進行使用。多次推送同一標(biāo)識符的通知即可進行更新。
3,刪除通知
(1)取消未發(fā)送的通知
//根據(jù)identifier來取消指定通知
let?identifier =?"com.hangge.testNotification"
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [identifier])
//取消全部未發(fā)送通知
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
(2)刪除已發(fā)送的通知(清除通知中心里的記錄)
//根據(jù)identifier來刪除指定通知
let?identifier =?"com.hangge.testNotification"
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [identifier])
//刪除全部已發(fā)送通知
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
遠(yuǎn)程推送無法刪除已展示的通知:
現(xiàn)在還不能通過類似的方式,向?APNs?發(fā)送一個包含?collapse id?的?DELETE?請求來刪除已經(jīng)展示的推送,APNs?服務(wù)器并不接受一個?DELETE?請求。
九、應(yīng)用內(nèi)展示通知
默認(rèn)情況下當(dāng)應(yīng)用處于前臺時,收到的通知是不進行展示的。如果我們希望在應(yīng)用內(nèi)也能顯示通知的話,需借助?UNUserNotificationCenterDelegate,通過該協(xié)議提供的接口方法實現(xiàn)應(yīng)用內(nèi)展示通知。
1,樣例代碼
我們這里在?AppDelegate?中添加相關(guān)的代理協(xié)議進行處理。
import?UIKit
import?UserNotifications
@UIApplicationMain
class?AppDelegate:?UIResponder,?UIApplicationDelegate?{
????var?window:?UIWindow?
????let?notificationHandler =?NotificationHandler()
????func?application(_ application:?UIApplication, didFinishLaunchingWithOptions
????????launchOptions: [UIApplicationLaunchOptionsKey:?Any]?) ->?Bool?{
????????//請求通知權(quán)限
????????UNUserNotificationCenter.current()
????????????.requestAuthorization(options: [.alert, .sound, .badge]) {
????????????????(accepted, error)?in
????????????????if?!accepted {
????????????????????print("用戶不允許消息通知。")
????????????????}
????????}
????????//設(shè)置通知代理
????????UNUserNotificationCenter.current().delegate = notificationHandler
????????return?true
????}
}
class?NotificationHandler:?NSObject,?UNUserNotificationCenterDelegate?{
????//在應(yīng)用內(nèi)展示通知
????func?userNotificationCenter(_ center:?UNUserNotificationCenter,
????????????????????????????????willPresent notification:?UNNotification,
????????????????????????????????withCompletionHandler completionHandler:
????????@escaping?(UNNotificationPresentationOptions) ->?Void) {
????????completionHandler([.alert, .sound])
????????// 如果不想顯示某個通知,可以直接用空 options 調(diào)用 completionHandler:
????????// completionHandler([])
????}
}
2,效果圖
可以看到當(dāng)通知觸發(fā)時,即使當(dāng)前應(yīng)用處于前臺,收到的通知也仍然會進行展示。

十、通知的響應(yīng)回調(diào)
UNUserNotificationCenterDelegate?還有另外一個代理方法,會在用戶與推送的通知進行交互時被調(diào)用。比如:用戶通過點擊通知打開了應(yīng)用、點擊或觸發(fā)了某個?action。
1,樣例代碼
(1)我們在程序頁面加載完畢后(ViewController.swift)創(chuàng)建一條簡單的通知消息(30?秒后觸發(fā))。特別要注意的是創(chuàng)建的通知出了基本的標(biāo)題內(nèi)容外,還附上了一些額外的信息。
import?UIKit
import?UserNotifications
class?ViewController:?UIViewController?{
????override?func?viewDidLoad() {
????????super.viewDidLoad()
????????//設(shè)置推送內(nèi)容
????????let?content =?UNMutableNotificationContent()
????????content.title =?"hangge.com"
????????content.body =?"做最好的開發(fā)者知識平臺"
????????content.userInfo = ["userName":?"hangge",?"articleId": 10086]
????????//設(shè)置通知觸發(fā)器
????????let?trigger =?UNTimeIntervalNotificationTrigger(timeInterval: 30, repeats:?false)
????????//設(shè)置請求標(biāo)識符
????????let?requestIdentifier =?"com.hangge.testNotification"
????????//設(shè)置一個通知請求
????????let?request =?UNNotificationRequest(identifier: requestIdentifier,
????????????????????????????????????????????content: content, trigger: trigger)
????????//將通知請求添加到發(fā)送中心
????????UNUserNotificationCenter.current().add(request) { error?in
????????????if?error ==?nil?{
????????????????print("Time Interval Notification scheduled: \(requestIdentifier)")
????????????}
????????}
????}
????override?func?didReceiveMemoryWarning() {
????????super.didReceiveMemoryWarning()
????}
}
(2)然后在?AppDelegate?中添加相關(guān)的代理協(xié)議來處理用戶與通知的交互操作。當(dāng)用戶點擊通知后,會將該通知的標(biāo)題、內(nèi)容以及前面附加的額外信息給打印出來。(實際應(yīng)用中我們可以根據(jù)?userInfo?的內(nèi)容來決定頁面跳轉(zhuǎn)或者是其他后續(xù)操作)
import?UIKit
import?UserNotifications
@UIApplicationMain
class?AppDelegate:?UIResponder,?UIApplicationDelegate?{
????var?window:?UIWindow?
????let?notificationHandler =?NotificationHandler()
????func?application(_ application:?UIApplication, didFinishLaunchingWithOptions
????????launchOptions: [UIApplicationLaunchOptionsKey:?Any]?) ->?Bool?{
????????//請求通知權(quán)限
????????UNUserNotificationCenter.current()
????????????.requestAuthorization(options: [.alert, .sound, .badge]) {
????????????????(accepted, error)?in
????????????????if?!accepted {
????????????????????print("用戶不允許消息通知。")
????????????????}
????????}
????????//設(shè)置通知代理
????????UNUserNotificationCenter.current().delegate = notificationHandler
????????return?true
????}
}
class?NotificationHandler:?NSObject,?UNUserNotificationCenterDelegate?{
????//對通知進行響應(yīng)(用戶與通知進行交互時被調(diào)用)
????func?userNotificationCenter(_ center:?UNUserNotificationCenter,
????????????????????????????????didReceive response:?UNNotificationResponse,
????????????????????????????????withCompletionHandler completionHandler:
????????@escaping?() ->?Void) {
????????print(response.notification.request.content.title)
????????print(response.notification.request.content.body)
????????//獲取通知附加數(shù)據(jù)
????????let?userInfo = response.notification.request.content.userInfo
????????print(userInfo)
????????//完成了工作
????????completionHandler()
????}
}
2,效果圖
(1)程序打開后退出,等待?30?秒后會收到推送通知。

(2)點擊通知則自動打開程序,同時控制臺中會輸出該通知的標(biāo)題、內(nèi)容以及附加信息。

原文出自:www.hangge.com??轉(zhuǎn)載請保留原文鏈接:https://www.hangge.com/blog/cache/detail_1846.html