{"id":1715,"date":"2021-02-04T22:26:32","date_gmt":"2021-02-04T14:26:32","guid":{"rendered":"http:\/\/123.57.164.21\/?p=1715"},"modified":"2021-02-05T10:12:37","modified_gmt":"2021-02-05T02:12:37","slug":"swift-usernotifications%e6%a1%86%e6%9e%b6%e4%bd%bf%e7%94%a8%e8%af%a6%e8%a7%a35%ef%bc%88actionable%e5%8f%af%e4%ba%a4%e4%ba%92%e9%80%9a%e7%9f%a5%ef%bc%89","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=1715","title":{"rendered":"Swift &#8211; UserNotifications\u6846\u67b6\u4f7f\u7528\u8be6\u89e35\uff08Actionable\u53ef\u4ea4\u4e92\u901a\u77e5\uff09"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"443\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-61-1024x443.png\" alt=\"\" class=\"wp-image-1716\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-61-1024x443.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-61-300x130.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-61-768x332.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-61-1536x664.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-61-830x359.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-61-230x99.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-61-350x151.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-61-480x207.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-61.png 1624w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"492\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-63-1024x492.png\" alt=\"\" class=\"wp-image-1718\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-63-1024x492.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-63-300x144.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-63-768x369.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-63-1536x737.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-63-830x398.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-63-230x110.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-63-350x168.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-63-480x230.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-63.png 1558w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"202\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-64-1024x202.png\" alt=\"\" class=\"wp-image-1719\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-64-1024x202.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-64-300x59.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-64-768x151.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-64-1536x302.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-64-830x163.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-64-230x45.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-64-350x69.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-64-480x94.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-64.png 1616w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"517\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-65-1024x517.png\" alt=\"\" class=\"wp-image-1720\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-65-1024x517.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-65-300x152.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-65-768x388.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-65-1536x776.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-65-830x419.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-65-230x116.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-65-350x177.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-65-480x242.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-65.png 1592w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"148\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-66-1024x148.png\" alt=\"\" class=\"wp-image-1721\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-66-1024x148.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-66-300x43.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-66-768x111.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-66-1536x221.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-66-830x120.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-66-230x33.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-66-350x50.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-66-480x69.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-66.png 1624w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\nimport UserNotifications\n \n\/\/\u901a\u77e5category\u6807\u8bc6\u7b26\u679a\u4e3e\nenum NotificationCategory: String {\n    case news  \/\/\u65b0\u95fb\u8d44\u8baf\u901a\u77e5category\n}\n \n\/\/\u901a\u77e5category\u7684action\u6807\u8bc6\u7b26\u679a\u4e3e\nenum NotificationCategoryAction: String {\n    case like\n    case cancel\n    case comment\n}\n \n\/\/\u901a\u77e5\u54cd\u5e94\u5bf9\u8c61\nclass NotificationHandler: NSObject, UNUserNotificationCenterDelegate {\n    \/\/\u5bf9\u901a\u77e5\u8fdb\u884c\u54cd\u5e94\uff08\u7528\u6237\u4e0e\u901a\u77e5\u8fdb\u884c\u4ea4\u4e92\u65f6\u88ab\u8c03\u7528\uff09\n    func userNotificationCenter(_ center: UNUserNotificationCenter,\n                                didReceive response: UNNotificationResponse,\n                                withCompletionHandler completionHandler:\n        @escaping () -> Void) {\n        \/\/\u6839\u636ecategory\u6807\u8bc6\u7b26\u505a\u76f8\u5e94\u7684\u5904\u7406\n        let categoryIdentifier = response.notification.request.content.categoryIdentifier\n        if let category = NotificationCategory(rawValue: categoryIdentifier) {\n            switch category {\n            case .news:\n                handleNews(response: response)\n            }\n        }\n        completionHandler()\n    }\n     \n    \/\/\u5904\u7406\u65b0\u95fb\u8d44\u8baf\u901a\u77e5\u7684\u4ea4\u4e92\n    private func handleNews(response: UNNotificationResponse) {\n        let message: String\n         \n        \/\/\u5224\u65ad\u70b9\u51fb\u662f\u90a3\u4e2aaction\n        if let actionType = NotificationCategoryAction(rawValue: response.actionIdentifier) {\n            switch actionType {\n            case .like: message = \"\u4f60\u70b9\u51fb\u4e86\u201c\u70b9\u4e2a\u8d5e\u201d\u6309\u94ae\"\n            case .cancel: message = \"\u4f60\u70b9\u51fb\u4e86\u201c\u53d6\u6d88\u201d\u6309\u94ae\"\n            case .comment:\n                message = \"\u4f60\u8f93\u5165\u7684\u662f\uff1a\\((response as! UNTextInputNotificationResponse).userText)\"\n            }\n        } else {\n            \/\/\u76f4\u63a5\u70b9\u51fb\u901a\u77e5\uff0c\u6216\u8005\u70b9\u51fb\u5220\u9664\u8fd9\u4e2a\u901a\u77e5\u4f1a\u8fdb\u5165\u8fd9\u4e2a\u5206\u652f\u3002\n            message = \"\"\n        }\n         \n        \/\/\u5f39\u51fa\u76f8\u5173\u4fe1\u606f\n        if !message.isEmpty {\n            showAlert(message: message)\n        }\n    }\n     \n    \/\/\u5728\u6839\u89c6\u56fe\u63a7\u5236\u5668\u4e0a\u5f39\u51fa\u666e\u901a\u6d88\u606f\u63d0\u793a\u6846\n    private func showAlert(message: String) {\n        if let vc = UIApplication.shared.keyWindow?.rootViewController {\n            let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)\n            alert.addAction(UIAlertAction(title: \"\u786e\u5b9a\", style: .cancel))\n            vc.present(alert, animated: true)\n        }\n    }\n}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"76\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-67-1024x76.png\" alt=\"\" class=\"wp-image-1722\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-67-1024x76.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-67-300x22.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-67-768x57.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-67-1536x113.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-67-830x61.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-67-230x17.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-67-350x26.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-67-480x35.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-67.png 1626w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mport UIKit\nimport UserNotifications\n \n@UIApplicationMain\nclass AppDelegate: UIResponder, UIApplicationDelegate {\n     \n    var window: UIWindow?\n     \n    let notificationHandler = NotificationHandler()\n     \n    func application(_ application: UIApplication, didFinishLaunchingWithOptions\n        launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {\n        \/\/\u8bf7\u6c42\u901a\u77e5\u6743\u9650\n        UNUserNotificationCenter.current()\n            .requestAuthorization(options: [.alert, .sound, .badge]) {\n                (accepted, error) in\n                if !accepted {\n                    print(\"\u7528\u6237\u4e0d\u5141\u8bb8\u6d88\u606f\u901a\u77e5\u3002\")\n                }\n        }\n         \n        \/\/\u6ce8\u518ccategory\n        registerNotificationCategory()\n        UNUserNotificationCenter.current().delegate = notificationHandler\n         \n        return true\n    }\n     \n    func applicationWillResignActive(_ application: UIApplication) {\n    }\n     \n    func applicationDidEnterBackground(_ application: UIApplication) {\n    }\n     \n    func applicationWillEnterForeground(_ application: UIApplication) {\n    }\n     \n    func applicationDidBecomeActive(_ application: UIApplication) {\n    }\n     \n    func applicationWillTerminate(_ application: UIApplication) {\n    }\n     \n    \/\/\u6ce8\u518c\u4e00\u4e2acategory\n    private func registerNotificationCategory() {\n        let newsCategory: UNNotificationCategory = {\n            \/\/\u521b\u5efa\u8f93\u5165\u6587\u672c\u7684action\n            let inputAction = UNTextInputNotificationAction(\n                identifier: NotificationCategoryAction.comment.rawValue,\n                title: \"\u8bc4\u8bba\",\n                options: [.authenticationRequired],\n                textInputButtonTitle: \"\u53d1\u9001\",\n                textInputPlaceholder: \"\u5728\u8fd9\u91cc\u7559\u4e0b\u4f60\u60f3\u8bf4\u7684\u8bdd...\")\n             \n            \/\/\u521b\u5efa\u666e\u901a\u7684\u6309\u94aeaction\n            let likeAction = UNNotificationAction(\n                identifier: NotificationCategoryAction.like.rawValue,\n                title: \"\u70b9\u4e2a\u8d5e\",\n                options: [.foreground])\n             \n            \/\/\u521b\u5efa\u666e\u901a\u7684\u6309\u94aeaction\n            let cancelAction = UNNotificationAction(\n                identifier: NotificationCategoryAction.cancel.rawValue,\n                title: \"\u53d6\u6d88\",\n                options: [.destructive])\n             \n            \/\/\u521b\u5efacategory\n            return UNNotificationCategory(identifier: NotificationCategory.news.rawValue,\n                                          actions: [inputAction, likeAction, cancelAction],\n                                          intentIdentifiers: [], options: [.customDismissAction])\n        }()\n         \n        \/\/\u628acategory\u6dfb\u52a0\u5230\u901a\u77e5\u4e2d\u5fc3\n        UNUserNotificationCenter.current().setNotificationCategories([newsCategory])\n    }\n}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"108\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-68-1024x108.png\" alt=\"\" class=\"wp-image-1723\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-68-1024x108.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-68-300x32.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-68-768x81.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-68-1536x162.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-68-830x88.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-68-230x24.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-68-350x37.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-68-480x51.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-68.png 1612w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\nimport UserNotifications\n \nclass ViewController: UIViewController {\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n         \n        \/\/\u8bbe\u7f6e\u63a8\u9001\u5185\u5bb9\n        let content = UNMutableNotificationContent()\n        content.title = \"hangge.com\"\n        content.body = \"\u56e4\u79efiPhoneX\u7684\u9ec4\u725b\u8d54\u5230\u6000\u7591\u4eba\u751f?\"\n        \/\/\u8bbe\u7f6e\u901a\u77e5\u5bf9\u5e94\u7684category\u6807\u8bc6\u7b26\n        content.categoryIdentifier = NotificationCategory.news.rawValue\n         \n        \/\/\u8bbe\u7f6e\u901a\u77e5\u89e6\u53d1\u5668\n        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)\n         \n        \/\/\u8bbe\u7f6e\u8bf7\u6c42\u6807\u8bc6\u7b26\n        let requestIdentifier = \"com.hangge.testNotification\"\n         \n        \/\/\u8bbe\u7f6e\u4e00\u4e2a\u901a\u77e5\u8bf7\u6c42\n        let request = UNNotificationRequest(identifier: requestIdentifier,\n                                            content: content, trigger: trigger)\n         \n        \/\/\u5c06\u901a\u77e5\u8bf7\u6c42\u6dfb\u52a0\u5230\u53d1\u9001\u4e2d\u5fc3\n        UNUserNotificationCenter.current().add(request) { error in\n            if error == nil {\n                print(\"Time Interval Notification scheduled: \\(requestIdentifier)\")\n            }\n        }\n    }\n     \n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n    }\n}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"79\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-69-1024x79.png\" alt=\"\" class=\"wp-image-1724\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-69-1024x79.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-69-300x23.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-69-768x59.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-69-1536x118.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-69-830x64.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-69-230x18.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-69-350x27.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-69-480x37.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-69.png 1712w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n  \"aps\":{\n    \"alert\":{\n      \"title\":\"hangge.com\",\n      \"body\":\"\u56e4\u79efiPhoneX\u7684\u9ec4\u725b\u8d54\u5230\u6000\u7591\u4eba\u751f?\"\n    },\n    \"sound\":\"default\",\n    \"badge\":1,\n    \"category\":\"news\"\n  }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1715"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1715"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1715\/revisions"}],"predecessor-version":[{"id":1780,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1715\/revisions\/1780"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}