{"id":1038,"date":"2022-10-29T23:13:08","date_gmt":"2022-10-29T15:13:08","guid":{"rendered":"https:\/\/xujiwei.com\/blog\/?p=1038"},"modified":"2022-10-29T23:15:38","modified_gmt":"2022-10-29T15:15:38","slug":"update-dynamic-island-and-live-activity-with-push-notification","status":"publish","type":"post","link":"https:\/\/xujiwei.com\/blog\/2022\/10\/update-dynamic-island-and-live-activity-with-push-notification\/","title":{"rendered":"iOS \u4f7f\u7528\u63a8\u9001\u901a\u77e5\u66f4\u65b0 Dynamic Island \u548c Live Activity"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_65 ez-toc-wrap-right counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\">\u6587\u7ae0\u76ee\u5f55<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69ebbc1c8f7b3\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69ebbc1c8f7b3\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/xujiwei.com\/blog\/2022\/10\/update-dynamic-island-and-live-activity-with-push-notification\/#%E4%BB%80%E4%B9%88%E6%98%AF_Live_Activity_%E5%92%8C_Dynamic_Island\" title=\"\u4ec0\u4e48\u662f Live Activity \u548c Dynamic Island\">\u4ec0\u4e48\u662f Live Activity \u548c Dynamic Island<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/xujiwei.com\/blog\/2022\/10\/update-dynamic-island-and-live-activity-with-push-notification\/#%E5%A6%82%E4%BD%95%E6%9B%B4%E6%96%B0_Live_Activity_%E5%92%8C_Dynamic_Island\" title=\"\u5982\u4f55\u66f4\u65b0 Live Activity \u548c Dynamic Island\">\u5982\u4f55\u66f4\u65b0 Live Activity \u548c Dynamic Island<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/xujiwei.com\/blog\/2022\/10\/update-dynamic-island-and-live-activity-with-push-notification\/#APNs_%E8%AE%A4%E8%AF%81%E6%96%B9%E5%BC%8F%E9%80%89%E6%8B%A9\" title=\"APNs \u8ba4\u8bc1\u65b9\u5f0f\u9009\u62e9\">APNs \u8ba4\u8bc1\u65b9\u5f0f\u9009\u62e9<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/xujiwei.com\/blog\/2022\/10\/update-dynamic-island-and-live-activity-with-push-notification\/#Token-Based_Connection_to_APNs\" title=\"Token-Based Connection to APNs\">Token-Based Connection to APNs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/xujiwei.com\/blog\/2022\/10\/update-dynamic-island-and-live-activity-with-push-notification\/#%E8%8E%B7%E5%8F%96_Push_Token\" title=\"\u83b7\u53d6 Push Token\">\u83b7\u53d6 Push Token<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/xujiwei.com\/blog\/2022\/10\/update-dynamic-island-and-live-activity-with-push-notification\/#%E7%94%9F%E6%88%90_Token\" title=\"\u751f\u6210 Token\">\u751f\u6210 Token<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/xujiwei.com\/blog\/2022\/10\/update-dynamic-island-and-live-activity-with-push-notification\/#%E7%94%9F%E6%88%90_Push_Payload_%E5%B9%B6%E5%8F%91%E9%80%81\" title=\"\u751f\u6210 Push Payload \u5e76\u53d1\u9001\">\u751f\u6210 Push Payload \u5e76\u53d1\u9001<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/xujiwei.com\/blog\/2022\/10\/update-dynamic-island-and-live-activity-with-push-notification\/#%E6%9B%B4%E6%96%B0_Live_Activity\" title=\"\u66f4\u65b0 Live Activity\">\u66f4\u65b0 Live Activity<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/xujiwei.com\/blog\/2022\/10\/update-dynamic-island-and-live-activity-with-push-notification\/#%E5%A6%82%E4%BD%95%E7%BB%B4%E6%8A%A4_Live_Activity_%E6%9B%B4%E6%96%B0%E5%8E%86%E5%8F%B2\" title=\"\u5982\u4f55\u7ef4\u62a4 Live Activity \u66f4\u65b0\u5386\u53f2\">\u5982\u4f55\u7ef4\u62a4 Live Activity \u66f4\u65b0\u5386\u53f2<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/xujiwei.com\/blog\/2022\/10\/update-dynamic-island-and-live-activity-with-push-notification\/#%E7%BB%93%E8%AE%BA\" title=\"\u7ed3\u8bba\">\u7ed3\u8bba<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/xujiwei.com\/blog\/2022\/10\/update-dynamic-island-and-live-activity-with-push-notification\/#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99\" title=\"\u53c2\u8003\u8d44\u6599\">\u53c2\u8003\u8d44\u6599<\/a><\/li><\/ul><\/nav><\/div>\n<p>\u968f\u7740 iOS 16 \u63a8\u51fa\u7684 Live Activity \u4ee5\u53ca iOS 16.1 \u6b63\u5f0f\u652f\u6301\u7684 Dynamic Island \u5df2\u7ecf\u53ef\u4ee5\u63d0\u4ea4 App Store \u53d1\u5e03\u4e86\uff0c\u6700\u8fd1\u5728\u6d4b\u8bd5 Live Activity \u7684\u540e\u53f0\u66f4\u65b0\u78b0\u5230\u4e00\u4e9b\u95ee\u9898\uff0c\u5728\u8fd9\u91cc\u8bb0\u5f55\u4e00\u4e0b\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E4%BB%80%E4%B9%88%E6%98%AF_Live_Activity_%E5%92%8C_Dynamic_Island\"><\/span>\u4ec0\u4e48\u662f Live Activity \u548c Dynamic Island<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Live Activity \u548c Dynamic Island \u5176\u5b9e\u662f\u4f5c\u4e3a\u540c\u4e00\u4e2a Widget \u7684\u4e24\u79cd\u5f62\u6001\u5b58\u5728\u7684\uff0c\u5b83\u4eec\u4f7f\u7528 ActivityKit \u4e2d\u7684  ActivityConfiguration \u540c\u65f6\u8fdb\u884c\u914d\u7f6e\uff0c\u5728\u62e5\u6709 Dynamic Island \u652f\u6301\u7684 iPhone 14 Pro \u548c iPhone 14 Pro Max \u4e0a\u53ef\u4ee5\u6b63\u5e38\u663e\u793a Dynamic Island \u5185\u5bb9\uff0c\u800c\u5728\u5176\u4ed6\u673a\u578b\u4e0a\u5747\u4ee5 Live Activity \u7684\u5f62\u5f0f\u6765\u5c55\u793a\u3002<\/p>\n<pre><code class=\"language-swift\">struct GroceryDeliveryApp: Widget {\n    var body: some WidgetConfiguration {\n        ActivityConfiguration(for: GroceryDeliveryAppAttributes.self) { context in\n            return VStack(alignment: .leading) {\n                HStack {\n                    VStack(alignment: .center) {\n                        Text(context.state.courierName + &quot; is on the way!&quot;).font(.headline)\n                        Text(Date(timeIntervalSince1970: context.state.deliveryTime).formatted(Date.ISO8601FormatStyle()))\n                    }\n                }\n            }.padding(15)\n        } dynamicIsland: { context in\n\n            DynamicIsland {\n              \/\/ \u5c55\u5f00\u72b6\u6001\u65f6\u7684\u754c\u9762\n            } compactLeading: {\n              \/\/ \u6536\u8d77\u72b6\u6001\u65f6\u7684\u5de6\u8fb9\u90e8\u5206\u754c\u9762\n            } compactTrailing: {\n              \/\/ \u6536\u8d77\u72b6\u6001\u65f6\u7684\u53f3\u8fb9\u90e8\u5206\u754c\u9762\n            } minimal: {\n              \/\/ \u6700\u5c0f\u72b6\u6001\u65f6\u7684\u754c\u9762\n            }\n            .keylineTint(.cyan)\n        }\n    }\n}<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E5%A6%82%E4%BD%95%E6%9B%B4%E6%96%B0_Live_Activity_%E5%92%8C_Dynamic_Island\"><\/span>\u5982\u4f55\u66f4\u65b0 Live Activity \u548c Dynamic Island<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u548c\u4e3b\u754c\u9762 Widgets \u4e0d\u540c\u7684\u662f\uff0cLive Activity \u5e76\u6ca1\u6709\u4e00\u4e2a Timeline Provider \u6765\u63d0\u4f9b\u5b9a\u671f\u66f4\u65b0\u673a\u5236\uff0c\u5b83\u53ea\u80fd\u4f9d\u8d56\u4e8e App \u4e3b\u52a8\u66f4\u65b0\uff0c\u6216\u8005\u4f9d\u8d56 Push \u901a\u77e5\u6765\u66f4\u65b0\u3002<\/p>\n<p>\u5982\u679c\u8981\u4f7f\u7528 App \u6765\u4e3b\u52a8\u66f4\u65b0 Live Activity\uff0c\u8fd9\u8981\u6c42 App \u5177\u5907\u4efb\u4e00\u4e00\u79cd\u540e\u53f0\u8fd0\u884c\u6a21\u5f0f\uff0c\u4fdd\u6301 App \u6301\u7eed\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u4ece\u800c\u6839\u636e\u8fd0\u884c\u72b6\u6001\u6765\u66f4\u65b0 Live Activity \u4e0a\u663e\u793a\u7684\u5185\u5bb9\uff0c\u4f46\u662f\u8fd9\u5bf9\u4e8e\u5927\u591a\u6570 App \u6765\u8bf4\u53ef\u80fd\u5e76\u4e0d\u9002\u7528\uff0c\u9664\u4e86\u5bfc\u822a\u7c7b\u3001\u97f3\u9891\u64ad\u653e\u7c7b App\uff0c\u4e00\u822c App \u5f88\u96be\u53ef\u4ee5\u7533\u8bf7\u5230\u6301\u7eed\u540e\u53f0\u8fd0\u884c\u7684\u6743\u9650\u3002<\/p>\n<p>\u90a3\u4e48\u5c31\u53ea\u5269\u4e0b\u4f7f\u7528 Push Notification \u6765\u66f4\u65b0\u8fd9\u4e00\u6761\u8def\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"APNs_%E8%AE%A4%E8%AF%81%E6%96%B9%E5%BC%8F%E9%80%89%E6%8B%A9\"><\/span>APNs \u8ba4\u8bc1\u65b9\u5f0f\u9009\u62e9<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u4f20\u7edf\u7684 APNs \u8fde\u63a5\u65b9\u5f0f\u662f\u4f7f\u7528\u57fa\u4e8e\u8bc1\u4e66\u8ba4\u8bc1\u7684\uff0c\u4f46\u662f\u5728\u4f7f\u7528\u8bc1\u4e66\u8ba4\u8bc1\u7684\u8fde\u63a5\u4e2d\uff0c\u5373\u4f7f\u4f7f\u7528\u4e86\u66f4\u65b0 Live Activity \u6240\u9700\u8981\u7684\u989d\u5916\u4fe1\u606f\uff0c\u4ecd\u7136\u4f1a\u78b0\u5230 <code>TopicDisallowed<\/code> \u9519\u8bef\uff0c\u5728\u82f9\u679c\u5f00\u53d1\u8005\u8bba\u575b\u91cc\u627e\u5230\u4e00\u4e2a\u5e16\u5b50\uff08<a href=\"https:\/\/developer.apple.com\/forums\/thread\/712499\uff09\u63d0\u5230\uff0c\u6b64\u7c7b\u901a\u77e5\u9700\u8981\u4f7f\u7528\">https:\/\/developer.apple.com\/forums\/thread\/712499\uff09\u63d0\u5230\uff0c\u6b64\u7c7b\u901a\u77e5\u9700\u8981\u4f7f\u7528<\/a> Token-Based \u8ba4\u8bc1\u65b9\u5f0f\u6765\u8fde\u63a5 APNs\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Token-Based_Connection_to_APNs\"><\/span>Token-Based Connection to APNs<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u5728\u82f9\u679c\u7684\u5f00\u53d1\u8005\u6587\u6863\u91cc\u53ef\u4ee5\u627e\u5230\u5982\u4f55\u5efa\u7acb\u4e00\u4e2a Token-Based \u8fde\u63a5\u5230 APNs\uff1a<a href=\"https:\/\/developer.apple.com\/documentation\/usernotifications\/setting_up_a_remote_notification_server\/establishing_a_token-based_connection_to_apns\">Establishing a Token-Based Connection to APNs<\/a>\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728 Token-Based \u8ba4\u8bc1\u8fc7\u7a0b\u4e2d\u6240\u9700\u8981\u7684 Key \u4e0e\u539f\u5148\u4f7f\u7528\u8bc1\u4e66\u8ba4\u8bc1\u6240\u751f\u6210\u7684 Key \u5e76\u4e0d\u662f\u540c\u4e00\u4e2a\uff0cToken-Based \u6240\u9700\u8981\u7684 Key \u9700\u8981\u91cd\u65b0\u53bb\u82f9\u679c\u5f00\u53d1\u8005\u540e\u53f0\u751f\u6210\uff0c\u5e76\u4e14\u751f\u4ea7\u548c Sandbox \u73af\u5883\u53ef\u4ee5\u4f7f\u7528\u540c\u4e00\u4e2a Key\uff0c\u9700\u8981\u4fdd\u969c\u6b64 Key \u7684\u5b89\u5168\u3002<\/p>\n<p>\u5982\u4f55\u6765\u83b7\u53d6\u8ba4\u8bc1\u9700\u8981\u6240\u8981\u4f7f\u7528\u7684 Key\uff0c\u5728\u82f9\u679c\u7684\u6587\u6863\u4e2d\u4e5f\u6709\u63d0\u53ca\uff0c\u53ea\u9700\u8981\u7b80\u5355\u586b\u5199\u4e00\u4e2a\u540d\u5b57\u5373\u53ef\u521b\u5efa\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E8%8E%B7%E5%8F%96_Push_Token\"><\/span>\u83b7\u53d6 Push Token<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u4e8b\uff0cLive Activity \u63a8\u9001\u6240\u4f7f\u7528\u7684 Push Token\uff0c\u5e76\u4e0d\u662f\u5728 App \u542f\u52a8\u65f6\u901a\u8fc7 UIApplication \u6ce8\u518c\u65f6\u6240\u83b7\u5f97\u7684 Device Token\uff0c\u9700\u8981\u5355\u72ec\u901a\u8fc7\u521b\u5efa Live Activity \u65f6\u6240\u83b7\u5f97\u7684\u5bf9\u8c61\u6765\u83b7\u53d6\u9488\u5bf9 Live Activity \u8fdb\u884c\u63a8\u9001\u901a\u77e5\u7684 Push Token\u3002<\/p>\n<pre><code class=\"language-swift\">var activities = Activity&lt;GroceryDeliveryAppAttributes&gt;.activities\n\nactivities.forEach { act in\n    if let data = act.pushToken {\n        let mytoken = data.map { String(format: &quot;%02x&quot;, $0) }.joined()\n        print(&quot;act push token&quot;, mytoken)\n    }\n}<\/code><\/pre>\n<p>\u901a\u8fc7 Activity \u5bf9\u8c61\u7684 <code>pushToken<\/code> \u5c5e\u6027\u53ef\u4ee5\u83b7\u5f97\u9488\u5bf9 Live Activity \u63a8\u9001\u6240\u9700\u8981\u4f7f\u7528\u7684 Push Token\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1aActivity \u5bf9\u8c61\u7684 pushToken \u5e76\u4e0d\u4f1a\u5728\u5bf9\u8c61\u521b\u5efa\u5b8c\u6210\u65f6\u5373\u65f6\u751f\u6210\uff0c\u9700\u8981\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u624d\u4f1a\u6709\u503c\uff0c\u56e0\u6b64\u9700\u8981\u7b49\u5f85\u6216\u8005\u76d1\u6d4b pushTokenUpdates \u6765\u83b7\u5f97\u6709\u6548\u7684\u503c\u3002<\/strong><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E7%94%9F%E6%88%90_Token\"><\/span>\u751f\u6210 Token<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u4e3a\u4e86\u6d4b\u8bd5\u53d1\u9001\u63a8\u9001\u901a\u77e5\uff0c\u4e00\u5f00\u59cb\u6211\u627e\u5230\u4e00\u4e2a Mac App \u6765\u8fdb\u884c\u6d4b\u8bd5\uff0c\u4f46\u662f\u5b83\u5e76\u6ca1\u6709\u5bf9 Token-Based \u8ba4\u8bc1\u65b9\u5f0f\u8fdb\u884c\u9002\u914d\uff0c\u5e76\u4e14 Token-Based \u8ba4\u8bc1\u4f7f\u7528\u4e86 JWT \u6765\u751f\u6210 Token\uff0c\u4e3a\u4e86\u65b9\u4fbf\u6d4b\u8bd5\uff0c\u5728 Medium \u4e0a\u627e\u5230\u4e00\u7bc7\u6587\u7ae0\u63d0\u4f9b\u4e86\u4e00\u4e2a\u811a\u672c\u6765\u5feb\u901f\u751f\u6210\u8bf7\u6c42\u7684 Payload \u5e76\u4e14\u53d1\u9001\u5230 APNs\u3002<\/p>\n<p>\u4ee5\u4e0b\u811a\u672c\u6765\u81ea <a href=\"https:\/\/medium.com\/@stefanhermanus\/ios-live-activities-updating-remotely-using-push-notification-34911a1bcc5e\">iOS 16 Live Activities: Updating Remotely Using Push Notification<\/a>\uff1a<\/p>\n<pre><code class=\"language-bash\">export TEAM_ID=YOUR_TEAM_ID\nexport TOKEN_KEY_FILE_NAME=YOUR_AUTHKEY_FILE.p8\nexport AUTH_KEY_ID=YOUR_AUTHKEY_ID\nexport DEVICE_TOKEN=YOUR_PUSH_TOKEN\nexport APNS_HOST_NAME=api.sandbox.push.apple.com\n\nexport JWT_ISSUE_TIME=$(date +%s)\nexport JWT_HEADER=$(printf &#039;{ &quot;alg&quot;: &quot;ES256&quot;, &quot;kid&quot;: &quot;%s&quot; }&#039; &quot;${AUTH_KEY_ID}&quot; | openssl base64 -e -A | tr -- &#039;+\/&#039; &#039;-_&#039; | tr -d =)\nexport JWT_CLAIMS=$(printf &#039;{ &quot;iss&quot;: &quot;%s&quot;, &quot;iat&quot;: %d }&#039; &quot;${TEAM_ID}&quot; &quot;${JWT_ISSUE_TIME}&quot; | openssl base64 -e -A | tr -- &#039;+\/&#039; &#039;-_&#039; | tr -d =)\nexport JWT_HEADER_CLAIMS=&quot;${JWT_HEADER}.${JWT_CLAIMS}&quot;\nexport JWT_SIGNED_HEADER_CLAIMS=$(printf &quot;${JWT_HEADER_CLAIMS}&quot; | openssl dgst -binary -sha256 -sign &quot;${TOKEN_KEY_FILE_NAME}&quot; | openssl base64 -e -A | tr -- &#039;+\/&#039; &#039;-_&#039; | tr -d =)\nexport AUTHENTICATION_TOKEN=&quot;${JWT_HEADER}.${JWT_CLAIMS}.${JWT_SIGNED_HEADER_CLAIMS}&quot;<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%E7%94%9F%E6%88%90_Push_Payload_%E5%B9%B6%E5%8F%91%E9%80%81\"><\/span>\u751f\u6210 Push Payload \u5e76\u53d1\u9001<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u5728\u83b7\u5f97 AuthKey\uff0c\u751f\u6210\u5b8c APNs Token \u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u5f00\u59cb\u771f\u6b63\u53d1\u9001\u63a8\u9001\u901a\u77e5\u5230 Live Activity \u4e86\uff1a<\/p>\n<pre><code class=\"language-bash\">curl -v \\\n--header &quot;apns-topic:YOUR_BUNDLE_ID.push-type.liveactivity&quot; \\\n--header &quot;apns-push-type:liveactivity&quot; \\\n--header &quot;authorization: bearer $AUTHENTICATION_TOKEN&quot; \\\n--data \\\n&#039;{&quot;aps&quot;: {\n   &quot;timestamp&quot;:1666667682,\n   &quot;event&quot;: &quot;end&quot;,\n   &quot;content-state&quot;: {\n      &quot;courierName&quot;: &quot;Iron Man&quot;,\n      &quot;deliveryTime&quot;: 1666661582\n   },\n   &quot;alert&quot;: {\n      &quot;title&quot;: &quot;Track Update&quot;,\n      &quot;body&quot;: &quot;Tony Stark is now handling the delivery!&quot;\n   }\n}}&#039; \\\n--http2 \\\nhttps:\/\/${APNS_HOST_NAME}\/3\/device\/$DEVICE_TOKEN<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684 <code>event<\/code> \u53ef\u4ee5\u4e3a <code>update<\/code> \u6216 <code>end<\/code>\uff0c\u4ee5\u53ca\u5177\u4f53 Payload \u7684\u683c\u5f0f\uff0c\u53ef\u4ee5\u53c2\u89c1\u82f9\u679c\u5f00\u53d1\u8005\u6587\u6863 <a href=\"https:\/\/developer.apple.com\/documentation\/activitykit\/update-and-end-your-live-activity-with-remote-push-notifications\">Updating and ending your Live Activity with remote push notifications<\/a>\u3002<\/p>\n<p>\u53e6\u5916\uff0cPayload \u4e2d\u7684 <code>content-state<\/code> \u662f\u4e0e Live Activity \u4e2d ActivityAttributes \u7684 ContentState \u4fdd\u6301\u4e00\u81f4\uff0c\u56e0\u6b64\u5728\u8fd9\u91cc\u7684\u5c5e\u6027\u5efa\u8bae\u4f7f\u7528\u7b80\u5355\u7684\u539f\u5b50\u7c7b\u578b\uff0c\u4f8b\u5982 String \u6216 Int \u7b49\uff0c\u8fd9\u6837\u5728\u751f\u6210 Payload \u65f6\u53ef\u4ee5\u66f4\u7b80\u5355\u4e00\u4e9b\u3002<\/p>\n<p>\u505a\u4f5c\u5b8c\u8fd9\u4e9b\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u6b63\u5f0f\u53d1\u9001\u63a8\u9001\u901a\u77e5\u5230 Live Activity \u4e86\uff0c\u8fd0\u884c\u5b8c\u4e0a\u8ff0 curl \u6307\u4ee4\u5c31\u5df2\u7ecf\u5b8c\u6210\u4e86\u4e00\u5207\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E6%9B%B4%E6%96%B0_Live_Activity\"><\/span>\u66f4\u65b0 Live Activity<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u4e0e\u4f20\u7edf Home Widget \u4e0d\u540c\u7684\u662f\uff0cLive Activity \u4e2d\u5e76\u6ca1\u6709\u4e00\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u6211\u4eec\u611f\u53d7\u5230\u5df2\u7ecf\u63a5\u6536\u5230\u4e86 Push \u901a\u77e5\uff0c\u5e76\u4e14\u53ef\u4ee5\u66f4\u65b0\u5f53\u524d Live Activity \u7684\u5185\u5bb9\u3002<\/p>\n<p>\u5176\u5b9e\u8fd9\u4e00\u5207\u90fd\u662f\u81ea\u52a8\u7684\uff0c\u5728\u6536\u5230\u901a\u77e5\u65f6\uff0c\u7cfb\u7edf\u5c31\u4f1a\u81ea\u52a8\u5c06 Payload \u4e2d\u7684 <code>content-state<\/code> \u6620\u5c04\u5230 ActivityAttributes \u7684 ContentState \u4e2d\uff0c\u5e76\u4e14\u901a\u8fc7 ActivityConfiguration \u91cd\u65b0\u751f\u6210 Widget \u7684 body\uff0c\u4ece\u800c\u66f4\u65b0 Live Activity \u7684\u754c\u9762\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%A6%82%E4%BD%95%E7%BB%B4%E6%8A%A4_Live_Activity_%E6%9B%B4%E6%96%B0%E5%8E%86%E5%8F%B2\"><\/span>\u5982\u4f55\u7ef4\u62a4 Live Activity \u66f4\u65b0\u5386\u53f2<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u5728\u5b66\u4e60\u8fc7\u7a0b\u4e2d\uff0c\u6211\u78b0\u5230\u4e00\u4e2a\u9700\u6c42\u662f\u53ef\u80fd\u9700\u8981\u4fdd\u6301\u63a8\u9001\u901a\u77e5\u8fc7\u6765\u7684\u5386\u53f2\u6570\u636e\uff0c\u4ece\u800c\u53ef\u4ee5\u7ef4\u62a4\u4e00\u4e2a\u5217\u8868\u6765\u5c55\u793a\u591a\u4e2a\u8ba2\u5355\u7684\u72b6\u6001\uff0c\u4f46\u662f\u670d\u52a1\u7aef\u5e76\u4e0d\u80fd\u76f4\u63a5\u7ef4\u62a4\u8fd9\u6837\u4e00\u4e2a\u5217\u8868\uff0c\u53ea\u80fd\u9488\u5bf9\u5355\u4e2a\u8ba2\u5355\u8fdb\u884c\u72b6\u6001\u63a8\u9001\uff0c\u8fd9\u4e2a\u65f6\u5019\u53ef\u4ee5\u5728 Live Activity \u8fd9\u4e00\u4fa7\u6765\u505a\u4e00\u4e9b\u4e8b\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5728 Activity \u751f\u6210\u7684\u8fc7\u7a0b\uff0c\u5c06 ContentState \u8fdb\u884c\u4fdd\u5b58\uff0c\u5e76\u4e14\u901a\u8fc7\u552f\u4e00\u7684 Key \u6765\u8fdb\u884c\u53bb\u91cd\uff0c\u4ece\u800c\u83b7\u5f97\u4e00\u4e2a\u5386\u53f2\u5217\u8868\uff1a<\/p>\n<pre><code class=\"language-swift\">var orders = [String]()\nif let oldOrders = UserDefaults.standard.array(forKey: &quot;current_orders&quot;) as? [String] {\n    orders = oldOrders.filter { $0 != context.state.courierName }\n}\norders.append(context.state.courierName)\nUserDefaults.standard.set(orders, forKey: &quot;current_orders&quot;)<\/code><\/pre>\n<p>\u8fd9\u91cc\u901a\u8fc7 <code>UserDefaults<\/code> \u6765\u4fdd\u6301\u5386\u53f2\u6536\u5230\u8fc7\u7684 ContentState\uff0c\u5e76\u4e14\u901a\u8fc7\u552f\u4e00\u5c5e\u6027\u8fdb\u884c\u53bb\u91cd\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u540e\u7eed\u751f\u6210\u754c\u9762\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53bb\u5c55\u793a\u4e00\u4e0b\u5217\u8868\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E7%BB%93%E8%AE%BA\"><\/span>\u7ed3\u8bba<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u603b\u7684\u6765\u8bf4\uff0c\u5f00\u53d1\u548c\u63a8\u9001\u66f4\u65b0 Live Activity \u548c Dynamic Island \u5e76\u4e0d\u662f\u5f88\u590d\u6742\uff0c\u4f46\u662f\u7531\u4e8e\u4e00\u822c\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4f1a\u4f7f\u7528\u7b2c\u4e09\u65b9\u7684\u63a8\u9001\u670d\u52a1\u6765\u8fdb\u884c\u53d1\u9001\u63a8\u9001\u901a\u77e5\uff0c\u800c\u5728\u76ee\u524d\u8fd9\u4e2a\u65f6\u95f4\u70b9\u4e0a\uff0c\u7b2c\u4e09\u65b9\u670d\u52a1\u90fd\u8fd8\u6ca1\u6709\u9002\u914d Live Activity \u8fd9\u4e2a\u7c7b\u578b\u7684\u63a8\u9001\u901a\u77e5\uff0c\u56e0\u6b64\u5982\u679c\u9700\u8981\u540e\u53f0\u66f4\u65b0 Lvie Activity\uff0c\u4f46\u662f App \u81ea\u8eab\u53c8\u6ca1\u6709\u6743\u9650\u4fdd\u6301\u5728\u540e\u53f0\u8fd0\u884c\u7684\u60c5\u51b5\uff0c\u5c31\u9700\u8981\u81ea\u884c\u67b6\u8bbe\u76f8\u5173\u63a8\u9001\u670d\u52a1\u5e76\u8fdb\u884c\u5bf9\u63a5\u4e86\uff0c\u8fd9\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u5f00\u53d1\u5de5\u4f5c\u548c\u6210\u672c\u3002<\/p>\n<p>\u5e0c\u671b\u6b64\u6587\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002 <\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99\"><\/span>\u53c2\u8003\u8d44\u6599<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><a href=\"https:\/\/github.com\/batikansosun\/iOS-16-Live-Activities-Dynamic-Island\">https:\/\/github.com\/batikansosun\/iOS-16-Live-Activities-Dynamic-Island<\/a><\/li>\n<li><a href=\"https:\/\/betterprogramming.pub\/create-live-activities-with-activitykit-on-ios-16-beta-4766a347035b\">https:\/\/betterprogramming.pub\/create-live-activities-with-activitykit-on-ios-16-beta-4766a347035b<\/a><\/li>\n<li><a href=\"https:\/\/developer.apple.com\/documentation\/activitykit\/update-and-end-your-live-activity-with-remote-push-notifications\">https:\/\/developer.apple.com\/documentation\/activitykit\/update-and-end-your-live-activity-with-remote-push-notifications<\/a><\/li>\n<li><a href=\"https:\/\/developer.apple.com\/documentation\/usernotifications\/setting_up_a_remote_notification_server\/establishing_a_token-based_connection_to_apns\">https:\/\/developer.apple.com\/documentation\/usernotifications\/setting_up_a_remote_notification_server\/establishing_a_token-based_connection_to_apns<\/a><\/li>\n<li><a href=\"https:\/\/jwt.io\/\">https:\/\/jwt.io\/<\/a><\/li>\n<li><a href=\"https:\/\/medium.com\/@stefanhermanus\/ios-live-activities-updating-remotely-using-push-notification-34911a1bcc5e\">https:\/\/medium.com\/@stefanhermanus\/ios-live-activities-updating-remotely-using-push-notification-34911a1bcc5e<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740 iOS 16 \u63a8\u51fa\u7684 Live Activity \u4ee5\u53ca iOS 16.1  &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/xujiwei.com\/blog\/2022\/10\/update-dynamic-island-and-live-activity-with-push-notification\/\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":1039,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[168],"tags":[309,332,154,333,334],"_links":{"self":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/1038"}],"collection":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/comments?post=1038"}],"version-history":[{"count":0,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/1038\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/media\/1039"}],"wp:attachment":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/media?parent=1038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/categories?post=1038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/tags?post=1038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}