十一拴鸵、Actionable 可交互通知
從?iOS 8?起蘋果就引入了可以交互的通知杆怕,其實(shí)現(xiàn)方式是把一組?action?放到一個?category?中,然后將這個?category?進(jìn)行注冊,最后在發(fā)送通知時將通知的?category?設(shè)置為要使用的?category?即可蓬豁。到了?iOS10,蘋果又對這些?action?做了統(tǒng)一菇肃。
1地粪,效果圖?
(1)下面是一個帶有?action?交互的通知。設(shè)備接收到通知后默認(rèn)的展示效果和普通的通知一樣琐谤,點(diǎn)擊通知同樣也會打開應(yīng)用蟆技。
(2)而當(dāng)我們下拉或者使用?3D touch?展開通知后,就可以看到下方會出現(xiàn)對應(yīng)的?action?按鈕斗忌。
(3)這里我們添加了三個按鈕质礼,點(diǎn)擊后的功能分別如下:
點(diǎn)擊“點(diǎn)個贊”:自動打開應(yīng)用,同時在程序界面上彈出用戶操作信息织阳。
點(diǎn)擊“取消”:自動清除通知眶蕉,且不會打開應(yīng)用。(如果我們之后手動打開程序唧躲,界面上也會彈出用戶操作信息造挽。)
點(diǎn)擊“評論”:界面下方會出現(xiàn)一個輸入框。用戶輸入文字并提交后弄痹,會自動打開應(yīng)用饭入,同時在程序界面上彈出剛才輸入的文字內(nèi)容。
2肛真,樣例代碼
(1)NotificationHandler.swift
為方便管理維護(hù)谐丢,這里我們將通知響應(yīng)(action?響應(yīng))放在一個單獨(dú)的文件中。并且將通知的?category?和?action?的標(biāo)識符都定義成枚舉蚓让。
import?UIKit
import?UserNotifications
//通知category標(biāo)識符枚舉
enum?NotificationCategory:?String?{
????case?news?//新聞資訊通知category
}
//通知category的action標(biāo)識符枚舉
enum?NotificationCategoryAction:?String?{
????case?like
????case?cancel
????case?comment
}
//通知響應(yīng)對象
class?NotificationHandler:?NSObject,?UNUserNotificationCenterDelegate?{
????//對通知進(jìn)行響應(yīng)(用戶與通知進(jìn)行交互時被調(diào)用)
????func?userNotificationCenter(_ center:?UNUserNotificationCenter,
????????????????????????????????didReceive response:?UNNotificationResponse,
????????????????????????????????withCompletionHandler completionHandler:
????????@escaping?() ->?Void) {
????????//根據(jù)category標(biāo)識符做相應(yīng)的處理
????????let?categoryIdentifier = response.notification.request.content.categoryIdentifier
????????if?let?category =?NotificationCategory(rawValue: categoryIdentifier) {
????????????switch?category {
????????????case?.news:
????????????????handleNews(response: response)
????????????}
????????}
????????completionHandler()
????}
????//處理新聞資訊通知的交互
????private?func?handleNews(response:?UNNotificationResponse) {
????????let?message:?String
????????//判斷點(diǎn)擊是那個action
????????if?let?actionType =?NotificationCategoryAction(rawValue: response.actionIdentifier) {
????????????switch?actionType {
????????????case?.like: message =?"你點(diǎn)擊了“點(diǎn)個贊”按鈕"
????????????case?.cancel: message =?"你點(diǎn)擊了“取消”按鈕"
????????????case?.comment:
????????????????message =?"你輸入的是:\((response as! UNTextInputNotificationResponse).userText)"
????????????}
????????}?else?{
????????????//直接點(diǎn)擊通知乾忱,或者點(diǎn)擊刪除這個通知會進(jìn)入這個分支。
????????????message =?""
????????}
????????//彈出相關(guān)信息
????????if?!message.isEmpty {
????????????showAlert(message: message)
????????}
????}
????//在根視圖控制器上彈出普通消息提示框
????private?func?showAlert(message:?String) {
????????if?let?vc =?UIApplication.shared.keyWindow?.rootViewController {
????????????let?alert =?UIAlertController(title:?nil, message: message, preferredStyle: .alert)
????????????alert.addAction(UIAlertAction(title:?"確定", style: .cancel))
????????????vc.present(alert, animated:?true)
????????}
????}
}
(2)AppDelegate.swift
這里除了申請通知權(quán)限外凭疮,還注冊了一個?category饭耳,里面包括兩個標(biāo)準(zhǔn)按鈕?action,以及一個文本輸入?action执解。
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("用戶不允許消息通知寞肖。")
????????????????}
????????}
????????//注冊category
????????registerNotificationCategory()
????????UNUserNotificationCenter.current().delegate = notificationHandler
????????return?true
????}
????//注冊一個category
????private?func?registerNotificationCategory() {
????????let?newsCategory:?UNNotificationCategory?= {
????????????//創(chuàng)建輸入文本的action
????????????let?inputAction =?UNTextInputNotificationAction(
????????????????identifier:?NotificationCategoryAction.comment.rawValue,
????????????????title:?"評論",
????????????????options: [.foreground],
????????????????textInputButtonTitle:?"發(fā)送",
????????????????textInputPlaceholder:?"在這里留下你想說的話...")
????????????//創(chuàng)建普通的按鈕action
????????????let?likeAction =?UNNotificationAction(
????????????????identifier:?NotificationCategoryAction.like.rawValue,
????????????????title:?"點(diǎn)個贊",
????????????????options: [.foreground])
????????????//創(chuàng)建普通的按鈕action
????????????let?cancelAction =?UNNotificationAction(
????????????????identifier:?NotificationCategoryAction.cancel.rawValue,
????????????????title:?"取消",
????????????????options: [.destructive])
????????????//創(chuàng)建category
????????????return?UNNotificationCategory(identifier:?NotificationCategory.news.rawValue,
??????????????????????????????????????????actions: [inputAction, likeAction, cancelAction],
??????????????????????????????????????????intentIdentifiers: [], options: [.customDismissAction])
????????}()
????????//把category添加到通知中心
????????UNUserNotificationCenter.current().setNotificationCategories([newsCategory])
????}
}
(3)ViewController.swift
我們在程序界面打開后就創(chuàng)建通知(5?秒后推送)纲酗。特別注意的是要將通知的?categoryIdentifier?設(shè)置為需要的?category?標(biāo)識符,這樣系統(tǒng)就知道這個通知對應(yīng)的是哪個?category新蟆。
import?UIKit
import?UserNotifications
class?ViewController:?UIViewController?{
????override?func?viewDidLoad() {
????????super.viewDidLoad()
????????//設(shè)置推送內(nèi)容
????????let?content =?UNMutableNotificationContent()
????????content.title =?"hangge.com"
????????content.body =?"囤積iPhoneX的黃牛賠到懷疑人生?"
????????//設(shè)置通知對應(yīng)的category標(biāo)識符
????????content.categoryIdentifier =?NotificationCategory.news.rawValue
????????//設(shè)置通知觸發(fā)器
????????let?trigger =?UNTimeIntervalNotificationTrigger(timeInterval: 5, 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()
????}
}
源碼下載:
3觅赊,遠(yuǎn)程推送使用 category
遠(yuǎn)程推送也可以使用?category,只需要在?payload?報文中添加?category?字段琼稻,并指定預(yù)先定義的?category id?就可以了吮螺。
{
??"aps":{
????"alert":{
??????"title":"hangge.com",
??????"body":"囤積iPhoneX的黃牛賠到懷疑人生?"
????},
????"sound":"default",
????"badge":1,
????"category":"news"
??}
}
原文出自:www.hangge.com??轉(zhuǎn)載請保留原文鏈接:https://www.hangge.com/blog/cache/detail_1847.html