{"id":1751,"date":"2021-02-05T09:56:24","date_gmt":"2021-02-05T01:56:24","guid":{"rendered":"http:\/\/123.57.164.21\/?p=1751"},"modified":"2021-02-05T09:56:24","modified_gmt":"2021-02-05T01:56:24","slug":"swift-usernotifications%e6%a1%86%e6%9e%b6%e4%bd%bf%e7%94%a8%e8%af%a6%e8%a7%a37%ef%bc%88%e8%87%aa%e5%ae%9a%e4%b9%89%e9%80%9a%e7%9f%a5%e8%af%a6%e6%83%85%e8%a7%86%e5%9b%be%ef%bc%89","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=1751","title":{"rendered":"Swift &#8211; UserNotifications\u6846\u67b6\u4f7f\u7528\u8be6\u89e37\uff08\u81ea\u5b9a\u4e49\u901a\u77e5\u8be6\u60c5\u89c6\u56fe\uff09"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"205\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-86-1024x205.png\" alt=\"\" class=\"wp-image-1752\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-86-1024x205.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-86-300x60.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-86-768x153.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-86-1536x307.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-86-830x166.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-86-230x46.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-86-350x70.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-86-480x96.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-86.png 1582w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"702\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-87-1024x702.png\" alt=\"\" class=\"wp-image-1754\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-87-1024x702.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-87-300x206.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-87-768x526.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-87-1536x1053.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-87-830x569.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-87-230x158.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-87-350x240.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-87-480x329.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-87.png 1538w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"419\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-88-1024x419.png\" alt=\"\" class=\"wp-image-1756\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-88-1024x419.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-88-300x123.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-88-768x315.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-88-830x340.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-88-230x94.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-88-350x143.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-88-480x197.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-88.png 1494w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"202\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-89-1024x202.png\" alt=\"\" class=\"wp-image-1758\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-89-1024x202.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-89-300x59.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-89-768x151.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-89-1536x302.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-89-830x163.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-89-230x45.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-89-350x69.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-89-480x95.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-89.png 1564w\" 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\nimport UserNotificationsUI\n \nclass NotificationViewController: UIViewController, UNNotificationContentExtension {\n \n    @IBOutlet var label: UILabel?\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n    }\n     \n    func didReceive(_ notification: UNNotification) {\n        self.label?.text = notification.request.content.body\n    }\n}\n<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"273\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-91-1024x273.png\" alt=\"\" class=\"wp-image-1762\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-91-1024x273.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-91-300x80.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-91-768x205.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-91-1536x410.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-91-830x222.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-91-230x61.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-91-350x93.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-91-480x128.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-91.png 1558w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"571\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-92-1024x571.png\" alt=\"\" class=\"wp-image-1764\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-92-1024x571.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-92-300x167.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-92-768x428.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-92-1536x857.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-92-830x463.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-92-230x128.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-92-350x195.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-92-480x268.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-92.png 1882w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"106\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-93-1024x106.png\" alt=\"\" class=\"wp-image-1766\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-93-1024x106.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-93-300x31.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-93-768x79.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-93-1536x159.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-93-830x86.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-93-230x24.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-93-350x36.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-93-480x50.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-93.png 1800w\" 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 = \"\u505a\u6700\u597d\u7684\u5f00\u53d1\u8005\u77e5\u8bc6\u5e73\u53f0\"\n         \n        \/\/\u8bbe\u7f6ecategory\u6807\u8bc6\u7b26\n        content.categoryIdentifier = \"myNotificationCategory\"\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}\n<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"646\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-94-1024x646.png\" alt=\"\" class=\"wp-image-1768\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-94-1024x646.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-94-300x189.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-94-768x484.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-94-1536x968.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-94-830x523.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-94-230x145.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-94-350x221.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-94-480x303.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-94.png 1770w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"772\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-95-1024x772.png\" alt=\"\" class=\"wp-image-1769\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-95-1024x772.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-95-300x226.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-95-768x579.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-95-1536x1159.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-95-830x626.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-95-230x173.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-95-350x264.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-95-480x362.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-95.png 1636w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"631\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-96-1024x631.png\" alt=\"\" class=\"wp-image-1771\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-96-1024x631.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-96-300x185.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-96-768x473.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-96-1536x946.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-96-830x511.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-96-230x142.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-96-350x216.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-96-480x296.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-96.png 1688w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"293\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-97-1024x293.png\" alt=\"\" class=\"wp-image-1773\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-97-1024x293.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-97-300x86.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-97-768x220.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-97-1536x439.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-97-830x237.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-97-230x66.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-97-350x100.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-97-480x137.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-97.png 1672w\" 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\nimport UserNotificationsUI\n \n\/\/\u8d44\u8baf\u6761\u76ee\nstruct NewsItem {\n    let title: String\n    let abstract: String\n    let url: URL\n}\n \nclass NotificationViewController: UIViewController, UNNotificationContentExtension {\n \n    \/\/\u663e\u793a\u8d44\u8baf\u6807\u9898\n    @IBOutlet var titleLabel: UILabel!\n     \n    \/\/\u663e\u793a\u8d44\u8baf\u5185\u5bb9\u6458\u8981\n    @IBOutlet weak var abstractLabel: UILabel!\n     \n    \/\/\u663e\u793a\u8d44\u8baf\u56fe\u7247\n    @IBOutlet weak var imageView: UIImageView!\n     \n    \/\/\u5f53\u524d\u663e\u793a\u7684\u8d44\u8baf\u7d22\u5f15\n    private var index: Int = 0\n     \n    \/\/\u6240\u6709\u8d44\u8baf\u6761\u76ee\n    var items: [NewsItem] = []\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n    }\n     \n    \/\/\u6536\u5230\u901a\u77e5\n    func didReceive(_ notification: UNNotification) {\n        \/\/\u5904\u7406\u8d44\u8baf\u6761\u76ee\n        let content = notification.request.content\n        if let news = content.userInfo[\"news\"] as? [[String: String]] {\n            for i in 0..&lt;news.count {\n                let title = news[i][\"title\"] ?? \"\"\n                let abstract = news[i][\"abstract\"] ?? \"\"\n                let url = content.attachments[i].url\n                let presentItem = NewsItem(title: title, abstract: abstract, url: url)\n                self.items.append(presentItem)\n            }\n        }\n         \n        \/\/\u663e\u793a\u7b2c\u4e00\u6761\u8d44\u8baf\n        updateNews(index: 0)\n    }\n     \n    \/\/\u66f4\u65b0\u663e\u793a\u7684\u8d44\u8baf\u5185\u5bb9\n    private func updateNews(index: Int) {\n        let item = items[index]\n         \n        \/\/\u66f4\u65b0\u6807\u9898\u548c\u5185\u5bb9\u6458\u8981\n        self.titleLabel!.text = item.title\n        self.abstractLabel.text = item.abstract\n \n        \/\/\u66f4\u65b0\u56fe\u7247\n        if item.url.startAccessingSecurityScopedResource() {\n            self.imageView.image = UIImage(contentsOfFile: item.url.path)\n            item.url.stopAccessingSecurityScopedResource()\n        }\n         \n        self.index = index\n    }\n     \n    \/\/Action\u6309\u94ae\u70b9\u51fb\u54cd\u5e94\n    func didReceive(_ response: UNNotificationResponse, completionHandler completion:\n        @escaping (UNNotificationContentExtensionResponseOption) -> Void) {\n        if response.actionIdentifier == \"change\" {\n            \/\/\u5207\u6362\u4e0b\u4e00\u6761\u8d44\u8baf\n            let nextIndex = (index + 1) % items.count\n            updateNews(index: nextIndex)\n            \/\/\u4fdd\u6301\u901a\u77e5\u7ee7\u7eed\u88ab\u663e\u793a\n            completion(.doNotDismiss)\n        } else if response.actionIdentifier == \"open\" {\n            \/\/\u53d6\u6d88\u8fd9\u4e2a\u901a\u77e5\u5e76\u7ee7\u7eed\u4f20\u9012Action\n            completion(.dismissAndForwardAction)\n        } else if response.actionIdentifier == \"dismiss\" {\n            \/\/\u76f4\u63a5\u53d6\u6d88\u8fd9\u4e2a\u901a\u77e5\n            completion(.dismiss)\n        } else {\n            \/\/\u53d6\u6d88\u8fd9\u4e2a\u901a\u77e5\u5e76\u7ee7\u7eed\u4f20\u9012Action\n            completion(.dismissAndForwardAction)\n        }\n    }\n}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"567\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-98-1024x567.png\" alt=\"\" class=\"wp-image-1774\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-98-1024x567.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-98-300x166.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-98-768x426.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-98-1536x851.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-98-830x460.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-98-230x127.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-98-350x194.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-98-480x266.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-98.png 1624w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"128\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-99-1024x128.png\" alt=\"\" class=\"wp-image-1776\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-99-1024x128.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-99-300x37.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-99-768x96.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-99-1536x191.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-99-830x103.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-99-230x29.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-99-350x44.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-99-480x60.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-99.png 1622w\" 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@UIApplicationMain\nclass AppDelegate: UIResponder, UIApplicationDelegate {\n \n    var window: UIWindow?\n \n    func application(_ application: UIApplication, didFinishLaunchingWithOptions\n        launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {\n         \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         \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\u4e09\u4e2a\u666e\u901a\u7684\u6309\u94aeaction\n            let changeAction = UNNotificationAction(\n                identifier: \"change\",\n                title: \"\u6362\u4e00\u6761\",\n                options: [])\n             \n            let openAction = UNNotificationAction(\n                identifier: \"open\",\n                title: \"\u6253\u5f00\",\n                options: [.foreground])\n             \n            \/\/\u521b\u5efa\u666e\u901a\u7684\u6309\u94aeaction\n            let cancelAction = UNNotificationAction(\n                identifier: \"cancel\",\n                title: \"\u53d6\u6d88\",\n                options: [.destructive])\n             \n            \/\/\u521b\u5efacategory\n            return UNNotificationCategory(identifier: \"myNotificationCategory\",\n                                          actions: [changeAction, openAction, cancelAction],\n                                          intentIdentifiers: [], options: [])\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=\"92\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/02\/image-100-1024x92.png\" alt=\"\" class=\"wp-image-1778\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-100-1024x92.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-100-300x27.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-100-768x69.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-100-1536x139.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-100-830x75.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-100-230x21.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-100-350x32.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-100-480x43.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/02\/image-100.png 1662w\" 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.body = \"\u4eca\u65e5\u8d44\u8baf\u7cbe\u9009\u30102017-12-12\u3011\"\n         \n        \/\/\u8bbe\u7f6e\u901a\u77e5category\u6807\u8bc6\u7b26\n        content.categoryIdentifier = \"myNotificationCategory\"\n         \n        \/\/\u8bbe\u7f6e\u901a\u77e5\u9644\u4ef6\u56fe\u7247\n        let imageNames = [\"image1\", \"image2\", \"image3\"]\n        let attachments = imageNames.flatMap { name -> UNNotificationAttachment? in\n            if let imageURL = Bundle.main.url(forResource: name, withExtension: \"png\") {\n                return try? UNNotificationAttachment(identifier: \"\\(name)\", url: imageURL,\n                                                     options: nil)\n            }\n            return nil\n        }\n        content.attachments = attachments\n         \n        \/\/\u8bbe\u7f6e\u901a\u77e5\u9644\u52a0\u4fe1\u606f\uff08\u8d44\u8baf\u6807\u9898\u548c\u5185\u5bb9\u6458\u8981\uff09\n        content.userInfo = [\"news\": [\n            [\"title\": \"\u5168\u56fd\u4eba\u6c11\u559c\u8fce\u6cb9\u4ef7\u4e0a\u6da8\",\n             \"abstract\": \"\u636e\u56fd\u5185\u591a\u5bb6\u6d4b\u8bc4\u673a\u6784\u7684\u5206\u6790\uff0c\u56fd\u5185\u6210\u54c1\u6cb9\u96f6\u552e\u9650\u4ef7\u5c06\u8fce\u6765\u201c\u4e24\u8fde\u6da8\u201d...\"],\n            [\"title\": \"\u623f\u4ef7\u540c\u6bd4\u4e0b\u964d\u57ce\u5e02\u5927\u5e45\u6269\u5bb9\",\n             \"abstract\": \"70\u4e2a\u5927\u4e2d\u57ce\u5e02\u4e2d\u4e00\u4e8c\u4e09\u7ebf\u57ce\u5e02\u623f\u4ef7\u540c\u6bd4\u6da8\u5e45\u7ee7\u7eed\u56de\u843d\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5f80\u5e74...\"],\n            [\"title\": \"\u6bd4\u7279\u5e01\u5e02\u503c\u518d\u521b\u65b0\u9ad8\",\n             \"abstract\": \"\u4e00\u9879\u540d\u4e3aSegWit2X\u7684\u6280\u672f\u53d6\u6d88\u5347\u7ea7\uff0c\u5bfc\u81f4\u5728\u672c\u5468\u4e00\u6bd4\u7279\u5e01\u5e02\u503c\u84b8\u53d1\u591a\u8fbe380\u4ebf\u7f8e\u5143...\"]\n            ]]\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}\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\/1751"}],"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=1751"}],"version-history":[{"count":13,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1751\/revisions"}],"predecessor-version":[{"id":1779,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1751\/revisions\/1779"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}