{"id":4039,"date":"2025-05-31T11:10:45","date_gmt":"2025-06-05T04:00:45","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4039"},"modified":"2025-06-05T11:10:45","modified_gmt":"2025-06-05T04:00:45","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e6%9c%ac%e5%9c%b0%e9%80%9a%e7%9f%a5","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e6%9c%ac%e5%9c%b0%e9%80%9a%e7%9f%a5\/","title":{"rendered":"\u6df1\u5165\u5b78\u7fd2 Swift \u672c\u5730\u901a\u77e5\uff1a2025 \u6700\u65b0\u6307\u5357\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, UNUserNotificationCenter, \u672c\u5730\u901a\u77e5, \u6642\u9593, \u6a19\u984c, \u5167\u5bb9, \u8072\u97f3, \u9ede\u64ca\u4e8b\u4ef6, iOS \u958b\u767c\"><\/p>\n<h1>\u6df1\u5165\u5b78\u7fd2 Swift \u672c\u5730\u901a\u77e5\uff1a2025 \u6700\u65b0\u6307\u5357\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Swift\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc iOS \u61c9\u7528\u7a0b\u5e8f\u7684\u958b\u767c\u3002\u81ea iOS 10 \u8d77\uff0cApple \u63a8\u51fa\u4e86\u65b0\u7684\u901a\u77e5\u6846\u67b6 UNUserNotificationCenter\uff0c\u53d6\u4ee3\u4e86\u820a\u7684 UILocalNotification\u3002\u56e0\u6b64\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 UNUserNotificationCenter \u4f86\u767c\u9001\u672c\u5730\u901a\u77e5\uff0c\u9019\u662f2025\u5e74\u6700\u65b0\u7684\u5be6\u8e10\u65b9\u6cd5\u3002\u672c\u6587\u4e5f\u5c07\u6db5\u84cb\u901a\u77e5\u7684\u8a2d\u7f6e\u3001\u932f\u8aa4\u6392\u9664\u4ee5\u53ca\u5ef6\u4f38\u61c9\u7528\u7684\u6280\u5de7\u3002<\/p>\n<h2>\u4f7f\u7528 UNUserNotificationCenter \u767c\u9001\u672c\u5730\u901a\u77e5<\/h2>\n<p>\u5728\u4f7f\u7528 UNUserNotificationCenter \u767c\u9001\u672c\u5730\u901a\u77e5\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u8acb\u6c42\u4f7f\u7528\u901a\u77e5\u7684\u6b0a\u9650\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5275\u5efa\u548c\u767c\u9001\u672c\u5730\u901a\u77e5\u7684\u6b65\u9a5f\uff1a<\/p>\n<p>1. **\u8acb\u6c42\u901a\u77e5\u6b0a\u9650**<\/p>\n<pre class=\"brush: swift\">\nimport UserNotifications\n\nUNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in\n    if granted {\n        print(\"\u6b0a\u9650\u7372\u5f97\u6210\u529f\")\n    } else if let error = error {\n        print(\"\u8acb\u6c42\u6b0a\u9650\u6642\u767c\u751f\u932f\u8aa4\uff1a\\(error.localizedDescription)\")\n    }\n}\n<\/pre>\n<p>2. **\u5275\u5efa\u901a\u77e5\u5167\u5bb9**<\/p>\n<pre class=\"brush: swift\">\nlet content = UNMutableNotificationContent()\ncontent.title = \"\u672c\u5730\u901a\u77e5\"\ncontent.body = \"\u9019\u662f\u4e00\u500b\u672c\u5730\u901a\u77e5\u7684\u5167\u5bb9\"\ncontent.sound = UNNotificationSound.default\n<\/pre>\n<p>3. **\u8a2d\u7f6e\u901a\u77e5\u7684\u89f8\u767c\u6642\u9593**<\/p>\n<pre class=\"brush: swift\">\nlet trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)\n<\/pre>\n<p>4. **\u5275\u5efa\u8acb\u6c42\u4e26\u767c\u9001\u901a\u77e5**<\/p>\n<pre class=\"brush: swift\">\nlet request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)\nUNUserNotificationCenter.current().add(request) { error in\n    if let error = error {\n        print(\"\u767c\u9001\u901a\u77e5\u6642\u51fa\u73fe\u932f\u8aa4\uff1a\\(error.localizedDescription)\")\n    }\n}\n<\/pre>\n<h2>\u8a2d\u7f6e\u901a\u77e5\u7684\u6642\u9593\u3001\u6a19\u984c\u3001\u5167\u5bb9\u3001\u8072\u97f3\u7b49\u4fe1\u606f<\/h2>\n<p>UNMutableNotificationContent \u985e\u6709\u591a\u7a2e\u5c6c\u6027\uff0c\u53ef\u7528\u65bc\u8a2d\u7f6e\u901a\u77e5\u7684\u8a73\u7d30\u4fe1\u606f\uff1a<\/p>\n<ul>\n<li><strong>title<\/strong>\uff1a\u901a\u77e5\u7684\u6a19\u984c\u3002<\/li>\n<li><strong>body<\/strong>\uff1a\u901a\u77e5\u7684\u5167\u5bb9\u3002<\/li>\n<li><strong>sound<\/strong>\uff1a\u901a\u77e5\u7684\u8072\u97f3\uff0c\u53ef\u4ee5\u8a2d\u7f6e\u70ba UNNotificationSound.default \u4f7f\u7528\u7cfb\u7d71\u9810\u8a2d\u8072\u97f3\u3002<\/li>\n<li><strong>userInfo<\/strong>\uff1a\u9644\u52a0\u4fe1\u606f\uff0c\u53ef\u4ee5\u7528\u4f86\u651c\u5e36\u81ea\u5b9a\u7fa9\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2>\u8655\u7406\u901a\u77e5\u7684\u9ede\u64ca\u4e8b\u4ef6<\/h2>\n<p>\u7576\u7528\u6236\u9ede\u64ca\u901a\u77e5\u6642\uff0c\u60a8\u53ef\u4ee5\u5728\u61c9\u7528\u7a0b\u5e8f\u7684 AppDelegate \u4e2d\u8655\u7406\u8a72\u4e8b\u4ef6\u3002\u4ee5\u4e0b\u662f\u8655\u7406\u9ede\u64ca\u4e8b\u4ef6\u7684\u4ee3\u78bc\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: swift\">\nfunc userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {\n    let userInfo = response.notification.request.content.userInfo\n    \/\/ \u6839\u64da userInfo \u8655\u7406\u9ede\u64ca\u4e8b\u4ef6\n    completionHandler()\n}\n<\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>&#8211; \u78ba\u4fdd\u60a8\u7684\u61c9\u7528\u5df2\u7372\u5f97\u901a\u77e5\u6b0a\u9650\uff0c\u5426\u5247\u901a\u77e5\u5c07\u7121\u6cd5\u986f\u793a\u3002<br \/>\n&#8211; \u6e2c\u8a66\u6642\uff0c\u8acb\u5728\u771f\u5be6\u8a2d\u5099\u4e0a\u9032\u884c\uff0c\u6a21\u64ec\u5668\u6709\u6642\u7121\u6cd5\u6b63\u78ba\u986f\u793a\u901a\u77e5\u3002<br \/>\n&#8211; \u70ba\u4e0d\u540c\u5834\u666f\u8a2d\u7f6e\u9069\u7576\u7684\u901a\u77e5\u5167\u5bb9\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u7c21\u55ae\u7684\u672c\u5730\u901a\u77e5\uff0c\u60a8\u9084\u53ef\u4ee5\u900f\u904e UNUserNotificationCenter \u5be6\u73fe\u66f4\u8907\u96dc\u7684\u901a\u77e5\u529f\u80fd\uff0c\u5982\u65e5\u66c6\u4e8b\u4ef6\u63d0\u9192\u3001\u91cd\u8907\u901a\u77e5\u7b49\u3002\u900f\u904e\u7d50\u5408\u7528\u6236\u7684\u884c\u70ba\u6578\u64da\uff0c\u60a8\u53ef\u4ee5\u8a2d\u8a08\u51fa\u66f4\u5177\u91dd\u5c0d\u6027\u7684\u901a\u77e5\u3002<\/p>\n<h2>\u7d50\u8a9e<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528 UNUserNotificationCenter \u4f86\u767c\u9001\u672c\u5730\u901a\u77e5\uff0c\u4e26\u63d0\u4f9b\u4e86\u8a2d\u7f6e\u901a\u77e5\u7684\u8a73\u7d30\u904e\u7a0b\u548c\u6700\u4f73\u5be6\u8e10\u3002\u900f\u904e\u9019\u4e9b\u77e5\u8b58\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u70ba\u60a8\u7684 iOS \u61c9\u7528\u6dfb\u52a0\u672c\u5730\u901a\u77e5\u529f\u80fd\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>\u5982\u4f55\u8acb\u6c42\u901a\u77e5\u6b0a\u9650\uff1f<\/h3>\n<p>\u5728\u60a8\u7684\u61c9\u7528\u555f\u52d5\u6642\uff0c\u4f7f\u7528 UNUserNotificationCenter \u7684 requestAuthorization \u65b9\u6cd5\u8acb\u6c42\u7528\u6236\u7684\u901a\u77e5\u6b0a\u9650\u3002<\/p>\n<h3>\u5982\u4f55\u8655\u7406\u901a\u77e5\u7684\u9ede\u64ca\u4e8b\u4ef6\uff1f<\/h3>\n<p>\u5728 AppDelegate \u4e2d\u5be6\u73fe userNotificationCenter(_:didReceive:withCompletionHandler:) \u65b9\u6cd5\u4ee5\u8655\u7406\u7528\u6236\u7684\u9ede\u64ca\u4e8b\u4ef6\u3002<\/p>\n<h3>\u6211\u53ef\u4ee5\u8a2d\u7f6e\u91cd\u8907\u7684\u672c\u5730\u901a\u77e5\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 UNTimeIntervalNotificationTrigger \u7684 repeats \u5c6c\u6027\u8a2d\u7f6e\u91cd\u8907\u901a\u77e5\uff0c\u6216\u4f7f\u7528 UNCalendarNotificationTrigger \u8a2d\u7f6e\u6839\u64da\u65e5\u66c6\u6642\u9593\u91cd\u8907\u7684\u901a\u77e5\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u672c\u5730\u901a\u77e5\uff0c\u5305\u62ec\u4f7f\u7528UILocalNotification\u7c7b\u4f86\u767c\u9001\u672c\u5730\u901a\u77e5\u3001\u8a2d\u7f6e\u901a\u77e5\u7684\u6642\u9593\u3001\u6a19\u984c\u3001\u5167\u5bb9\u3001\u8072\u97f3\u7b49\u4fe1\u606f\u3001\u5982\u4f55\u8655\u7406\u901a\u77e5\u7684\u9ede\u64ca\u4e8b\u4ef6\u7b49\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-4039","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-139","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=4039"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4039\/revisions"}],"predecessor-version":[{"id":4040,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4039\/revisions\/4040"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}