{"id":7382,"date":"2025-05-31T12:25:37","date_gmt":"2025-06-05T05:09:37","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7382"},"modified":"2025-06-05T12:25:37","modified_gmt":"2025-06-05T05:09:37","slug":"https-badgameshow-com-steven-362","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-362\/","title":{"rendered":"Swift \u901a\u77e5\u4e2d\u5fc3\u8207\u63a8\u64ad\u901a\u77e5\uff1a\u70ba\u60a8\u7684 App \u52a0\u5165\u5373\u6642\u901a\u77e5\u529f\u80fd\uff082025 \u6700\u65b0\u6559\u5b78\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, \u901a\u77e5\u4e2d\u5fc3, \u63a8\u64ad\u901a\u77e5, iOS\u958b\u767c, UserNotifications\"><\/p>\n<h1>Swift \u901a\u77e5\u4e2d\u5fc3\u8207\u63a8\u64ad\u901a\u77e5\uff1a\u70ba\u60a8\u7684 App \u52a0\u5165\u5373\u6642\u901a\u77e5\u529f\u80fd\uff082025 \u6700\u65b0\u6559\u5b78\uff09<\/h1>\n<p>\u96a8\u8457\u667a\u6167\u578b\u624b\u6a5f\u7684\u666e\u53ca\uff0c\u63a8\u64ad\u901a\u77e5\u5df2\u7d93\u6210\u70ba\u73fe\u4ee3 App \u958b\u767c\u7684\u5fc5\u5099\u529f\u80fd\u3002\u5728 iOS \u958b\u767c\u4e2d\uff0cSwift \u63d0\u4f9b\u4e86\u4e00\u500b\u53eb\u505a\u901a\u77e5\u4e2d\u5fc3\u7684\u6846\u67b6\uff0c\u8b93\u958b\u767c\u8005\u8f15\u9b06\u70ba App \u52a0\u5165\u63a8\u64ad\u901a\u77e5\u529f\u80fd\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u7684\u901a\u77e5\u4e2d\u5fc3\u4f86\u5be6\u73fe\u63a8\u64ad\u901a\u77e5\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u5be6\u4f5c\u7bc4\u4f8b\u53ca\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u901a\u77e5\u4e2d\u5fc3\uff1f<\/h2>\n<p>\u901a\u77e5\u4e2d\u5fc3\u662f iOS \u4e2d\u7684 UserNotifications \u6846\u67b6\uff0c\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u4e00\u5957\u7c21\u55ae\u7684 API \u4f86\u7ba1\u7406\u672c\u5730\u548c\u63a8\u64ad\u901a\u77e5\u3002\u958b\u767c\u8005\u53ef\u4ee5\u900f\u904e\u9019\u500b\u6846\u67b6\u8a2d\u5b9a\u63a8\u64ad\u901a\u77e5\u7684\u6642\u9593\u3001\u5167\u5bb9\u53ca\u5176\u4ed6\u5c6c\u6027\uff0c\u4f7f\u5f97\u4f7f\u7528\u8005\u5728\u9069\u7576\u7684\u6642\u523b\u63a5\u6536\u5230\u91cd\u8981\u4fe1\u606f\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528\u901a\u77e5\u4e2d\u5fc3\uff1f<\/h2>\n<p>\u8981\u4f7f\u7528\u901a\u77e5\u4e2d\u5fc3\u4f86\u5be6\u73fe\u63a8\u64ad\u901a\u77e5\u529f\u80fd\uff0c\u60a8\u9700\u8981\u5728 Xcode \u4e2d\u5c0e\u5165 UserNotifications.framework\uff0c\u7136\u5f8c\u5728 `AppDelegate.swift` \u4e2d\u5be6\u73fe\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">import UserNotifications\n\nfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {\n    \/\/ \u8a3b\u518a\u901a\u77e5\u4e2d\u5fc3\n    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in\n        if granted {\n            print(\"\u4f7f\u7528\u8005\u5df2\u540c\u610f\u63a5\u6536\u901a\u77e5\")\n        } else {\n            print(\"\u4f7f\u7528\u8005\u62d2\u7d55\u63a5\u6536\u901a\u77e5\")\n        }\n    }\n    return true\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u6703\u8a62\u554f\u4f7f\u7528\u8005\u662f\u5426\u540c\u610f\u63a5\u6536\u901a\u77e5\u3002\u5982\u679c\u4f7f\u7528\u8005\u540c\u610f\uff0c\u63a5\u4e0b\u4f86\u60a8\u53ef\u4ee5\u5efa\u7acb\u901a\u77e5\u5167\u5bb9\u4e26\u8a2d\u5b9a\u767c\u9001\u6642\u9593\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u5efa\u7acb\u4e00\u500b\u901a\u77e5\u5167\u5bb9\nlet content = UNMutableNotificationContent()\ncontent.title = \"\u63a8\u64ad\u901a\u77e5\"\ncontent.body = \"\u60a8\u5df2\u6210\u529f\u8a2d\u7f6e\u63a8\u64ad\u901a\u77e5\uff01\"\ncontent.sound = UNNotificationSound.default\n\n\/\/ \u8a2d\u5b9a\u63a8\u64ad\u901a\u77e5\u7684\u6642\u9593\nlet trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)\n\n\/\/ \u5efa\u7acb\u4e00\u500b\u901a\u77e5\u8acb\u6c42\nlet request = UNNotificationRequest(identifier: \"notification\", content: content, trigger: trigger)\n\n\/\/ \u5c07\u901a\u77e5\u8acb\u6c42\u52a0\u5165\u901a\u77e5\u4e2d\u5fc3\nUNUserNotificationCenter.current().add(request) { (error) in\n    if let error = error {\n        print(\"\u932f\u8aa4: \\(error.localizedDescription)\")\n    }\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5efa\u7acb\u4e00\u500b\u63a8\u64ad\u901a\u77e5\uff0c\u8a2d\u5b9a\u901a\u77e5\u7684\u5167\u5bb9\u548c\u767c\u9001\u6642\u9593\uff0c\u4e26\u5c07\u8acb\u6c42\u52a0\u5165\u901a\u77e5\u4e2d\u5fc3\u3002\u7576\u4f7f\u7528\u8005\u6536\u5230\u63a8\u64ad\u901a\u77e5\u6642\uff0c\u5c07\u986f\u793a\u60a8\u8a2d\u5b9a\u7684\u63a8\u64ad\u5167\u5bb9\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u5be6\u4f5c\u901a\u77e5\u529f\u80fd\u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u554f\u984c\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u932f\u8aa4\u6392\u9664\u7684\u5efa\u8b70\uff1a<\/p>\n<p>1. **\u901a\u77e5\u672a\u986f\u793a**\uff1a\u78ba\u4fdd\u61c9\u7528\u7a0b\u5e8f\u5df2\u7372\u5f97\u901a\u77e5\u6b0a\u9650\uff0c\u4e26\u4e14\u7528\u6236\u672a\u5728\u8a2d\u5b9a\u4e2d\u7981\u7528\u901a\u77e5\u3002<br \/>\n2. **\u97f3\u6548\u672a\u64ad\u653e**\uff1a\u6aa2\u67e5\u662f\u5426\u6b63\u78ba\u8a2d\u5b9a `content.sound` \u53c3\u6578\uff0c\u4e26\u78ba\u8a8d\u8a2d\u5099\u672a\u975c\u97f3\u3002<br \/>\n3. **\u6e2c\u8a66\u901a\u77e5**\uff1a\u5728\u958b\u767c\u968e\u6bb5\uff0c\u4f7f\u7528\u771f\u6a5f\u6e2c\u8a66\u901a\u77e5\u529f\u80fd\uff0c\u6a21\u64ec\u5668\u53ef\u80fd\u4e0d\u652f\u63f4\u63a8\u64ad\u901a\u77e5\u7684\u5b8c\u6574\u529f\u80fd\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u63a8\u64ad\u901a\u77e5\u529f\u80fd\u4e4b\u5916\uff0c\u958b\u767c\u8005\u9084\u53ef\u4ee5\u8003\u616e\u4ee5\u4e0b\u5ef6\u4f38\u61c9\u7528\uff1a<\/p>\n<p>&#8211; **\u672c\u5730\u901a\u77e5**\uff1a\u70ba\u7528\u6236\u63d0\u4f9b\u5b9a\u6642\u63d0\u9192\uff0c\u4f8b\u5982\u65e5\u66c6\u4e8b\u4ef6\u6216\u4efb\u52d9\u5230\u671f\u3002<br \/>\n&#8211; **\u4e92\u52d5\u5f0f\u901a\u77e5**\uff1a\u8b93\u4f7f\u7528\u8005\u53ef\u4ee5\u76f4\u63a5\u5728\u901a\u77e5\u4e2d\u56de\u8986\u6d88\u606f\u6216\u57f7\u884c\u5176\u4ed6\u64cd\u4f5c\u3002<br \/>\n&#8211; **\u63a8\u64ad\u901a\u77e5\u4e2d\u7684\u5716\u7247**\uff1a\u4f7f\u7528 `UNNotificationAttachment` \u9644\u52a0\u5716\u7247\u5167\u5bb9\uff0c\u4f7f\u901a\u77e5\u66f4\u5177\u5438\u5f15\u529b\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Swift \u7684\u901a\u77e5\u4e2d\u5fc3\u8b93\u958b\u767c\u8005\u8f15\u9b06\u70ba App \u52a0\u5165\u63a8\u64ad\u901a\u77e5\u529f\u80fd\uff0c\u53ea\u9700\u8981\u5c0e\u5165 UserNotifications.framework\uff0c\u4e26\u5be6\u73fe\u4e00\u4e9b\u7c21\u55ae\u7684\u7a0b\u5f0f\u78bc\uff0c\u5373\u53ef\u8b93\u4f7f\u7528\u8005\u6536\u5230\u91cd\u8981\u901a\u77e5\u3002\u96a8\u8457 iOS \u7684\u4e0d\u65b7\u66f4\u65b0\uff0c\u6301\u7e8c\u5b78\u7fd2\u548c\u5be6\u8e10\u662f\u6210\u70ba\u512a\u79c0\u958b\u767c\u8005\u7684\u95dc\u9375\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u901a\u77e5\u4e2d\u5fc3-\u63a8\u64ad\u901a\u77e5-\u70ba\u60a8\u7684app\u52a0\u5165\u901a\u77e5\u529f\u80fd\uff01\ud83d\udd14_1675475071.9341462.webp\" alt=\"Swift \u901a\u77e5\u4e2d\u5fc3 &#038; \u63a8\u64ad\u901a\u77e5 | \u70ba\u60a8\u7684app\u52a0\u5165\u901a\u77e5\u529f\u80fd\uff01\ud83d\udd14\" title=\"Swift \u901a\u77e5\u4e2d\u5fc3\u3001\u63a8\u64ad\u901a\u77e5\u3001App \u901a\u77e5\u529f\u80fd\u3001Swift \u65b9\u6cd5\u3001App \u901a\u77e5\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u6aa2\u67e5\u7528\u6236\u662f\u5426\u5df2\u5141\u8a31\u901a\u77e5\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>UNUserNotificationCenter.current().getNotificationSettings<\/code> \u65b9\u6cd5\u4f86\u6aa2\u67e5\u7528\u6236\u7684\u901a\u77e5\u8a2d\u5b9a\u3002<\/p>\n<h3>2. \u63a8\u64ad\u901a\u77e5\u8207\u672c\u5730\u901a\u77e5\u6709\u4ec0\u9ebc\u5340\u5225\uff1f<\/h3>\n<p>\u63a8\u64ad\u901a\u77e5\u662f\u7531\u4f3a\u670d\u5668\u767c\u9001\u7684\uff0c\u800c\u672c\u5730\u901a\u77e5\u662f\u7531\u61c9\u7528\u5167\u90e8\u7684\u4e8b\u4ef6\u89f8\u767c\u7684\u3002<\/p>\n<h3>3. \u5982\u4f55\u8a2d\u7f6e\u91cd\u8907\u7684\u63a8\u64ad\u901a\u77e5\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>UNTimeIntervalNotificationTrigger<\/code> \u7684 <code>repeats<\/code> \u53c3\u6578\u8a2d\u7f6e\u70ba <code>true<\/code>\uff0c\u4ee5\u4fbf\u5728\u7279\u5b9a\u6642\u9593\u9593\u9694\u5167\u91cd\u8907\u767c\u9001\u901a\u77e5\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u901a\u77e5\u4e2d\u5fc3&#038;\u63a8\u64ad\u901a\u77e5\uff0c\u70ba\u60a8\u7684App\u52a0\u5165\u901a\u77e5\u529f\u80fd\uff01\u672c\u6587\u5c07\u4ecb\u7d39Swift\u4e2d\u7684\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\u4f86\u70ba\u60a8\u7684App\u52a0\u5165\u901a\u77e5\u529f\u80fd\uff01<\/p>\n","protected":false},"author":1,"featured_media":7381,"comment_status":"open","ping_status":"closed","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-7382","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u901a\u77e5\u4e2d\u5fc3-\u63a8\u64ad\u901a\u77e5-\u70ba\u60a8\u7684app\u52a0\u5165\u901a\u77e5\u529f\u80fd\uff01\ud83d\udd14_1675475071.9341462.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1V4","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7382","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=7382"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7382\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7381"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}