{"id":175,"date":"2023-09-28T07:49:41","date_gmt":"2023-09-27T23:49:41","guid":{"rendered":"https:\/\/www.nickyoo7.com\/?p=175"},"modified":"2023-09-28T09:41:50","modified_gmt":"2023-09-28T01:41:50","slug":"php-ios-push-notification","status":"publish","type":"post","link":"https:\/\/www.nickyoo7.com\/?p=175","title":{"rendered":"\u4f7f\u7528 PHP \u767c\u9001 iOS \u9060\u8ddd\u63a8\u64ad"},"content":{"rendered":"<p>2023\u7248\u4f7f\u7528 PHP \u767c\u9001 iOS \u9060\u8ddd\u63a8\u64ad \u7684\u7bc4\u4f8b.<br \/>\n\u91cd\u9ede :<br \/>\n1. php curl \u8981\u652f\u63f4 HTTP\/2<br \/>\n2.\u672c\u6587\u4f7f\u7528\u7684\u662f\u958b\u767c\u8005\u5e33\u865f\u7533\u8acb\u7684 p8\u683c\u5f0f\u7684\u91d1\u9470<br \/>\n<!--more-->\u57fa\u672c\u8a2d\u5b9a\u503c : config.php<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\n    $team_id=\"\u958b\u767c\u8005\u5e33\u865f\u7684TEAM ID\";\r\n    $key_id=\"p8\u91d1\u9470\u7684 Key ID\"; \r\n    $pkey_file=\"p8 \u683c\u5f0f\u7684\u91d1\u9470\u6a94\u540d\uff0c\u7d55\u5c0d\u8def\u5f91\";\r\n    $topic=\"App\u7684Bundle ID\";<\/pre>\n<p>\u4e3b\u7a0b\u5f0f :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\nrequire_once \"config.php\";\r\n    \r\nfunction Sent_Notification_iOS($team_id ,$key_id,$pkey_file,$topic,$device_token,$payload,$sandbox=false){\r\n\r\n    $payload = json_encode($payload);\r\n\r\n    if ($sandbox == true){\r\n        \/\/\u6c99\u76d2\u63a8\u9001\u5730\u5740\r\n        $url = \"https:\/\/api.sandbox.push.apple.com\/3\/device\/\".$device_token;\t\t\r\n    }else{\r\n        \/\/\u6b63\u5f0f\u74b0\u5883\u63a8\u9001\r\n        $url = 'https:\/\/api.push.apple.com\/3\/device\/' . $device_token;\r\n    }\r\n\r\n    \r\n    $jwt = get_JWT($team_id ,$key_id,$pkey_file,$topic);\r\n        \r\n    $headers = array(\r\n        'Authorization: Bearer '.$jwt,\r\n        'Content-Type: application\/json',\r\n       \t'apns-topic: '.$topic,\r\n    );\r\n    $ch = curl_init($url);\r\n    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);\r\n    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); \/\/\u9019\u91cc\u5f88\u95dc\u9375\uff0c\u4ee5http2\u767c\u9001\u8acb\u6c42\r\n    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\r\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n    $response = curl_exec($ch);\r\n    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);\r\n    curl_close($ch);\r\n\r\n    \/\/ Check the response\r\n    \/*\r\n    if ($http_code == 200) {\r\n        return 'Notification sent successfully.';\r\n    } else {\r\n        echo 'Notification failed with error code: ' . $http_code;\r\n    }\r\n    *\/\r\n    return $http_code;\r\n}\r\nfunction get_JWT($team_id ,$key_id,$pkey_file,$TOPIC){\r\n\r\n\/\/ token\u904e\u671f\u6642\u9593\r\n    $expires = time() + 3600;\r\n\r\n    \/\/ Generate the JWT header\r\n    $header = array(\r\n        'alg' =&gt; 'ES256',\r\n        'kid' =&gt; $key_id\r\n    );\r\n\r\n    $jwt_header = base64_encode(json_encode($header));\r\n\r\n    \/\/ Generate the JWT payload\r\n    $payload = array(\r\n        'iss' =&gt; $team_id,\r\n        'iat' =&gt; time()\r\n    );\r\n    $jwt_payload = base64_encode(json_encode($payload));\r\n\r\n    \/\/\u8f09\u5165\u8b49\u66f8\u6587\u4ef6\r\n    $pkey_contents = file_get_contents($pkey_file);\r\n    $pkey = openssl_get_privatekey($pkey_contents);\r\n\r\n    \/\/JWT \u7c3d\u540d\r\n    $signature = '';\r\n    openssl_sign($jwt_header . '.' . $jwt_payload, $signature, $pkey, 'sha256');\r\n    $jwt_signature = base64_encode($signature);\r\n    \r\n    \/\/ Generate the final JWT\r\n    $jwt = $jwt_header . '.' . $jwt_payload . '.' . $jwt_signature;\r\n    return $jwt;\r\n}<\/pre>\n<p>\u767c\u9001\u7bc4\u4f8b :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\nrequire_once \"iOS_Notification.php\";\r\n\r\n    $device_token=\"\u63a5\u6536\u7aef\u8a2d\u5099\u78bc\";\/\/\u63a5\u6536\u7aef\u8a2d\u5099\u78bc\r\n    \r\n$msg=array(\r\n        'aps' =&gt; array(\r\n                'alert' =&gt; array (\r\n                            \"title\" =&gt; \"\u975e\u6d32\u5927\u8349\u539f\",\r\n                            \"subtitle\" =&gt; \"\u80af\u4e9e\",\r\n                            \"body\" =&gt; \"\u5927\u8c61\u4e00\u96bb\"\r\n                           ),\r\n                 'badge' =&gt; 99,\r\n                 'sound' =&gt; \"default\"\r\n        )\r\n);\r\n    \r\n$r=Sent_Notification_iOS($team_id ,$key_id,$pkey_file,$topic,$device_token,$msg);<\/pre>\n<p>\u5728 App \u5167\u6e05\u9664\u6216\u8a2d\u5b9a Badge :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\">UIApplication.shared.applicationIconBadgeNumber = 0<\/pre>\n<p>&nbsp;<\/p>\n<p>Payload \u5404\u9805\u8a2d\u5b9a\u53c3\u8003\u8cc7\u6599 :<br \/>\n<a href=\"https:\/\/developer.apple.com\/documentation\/usernotifications\/setting_up_a_remote_notification_server\/generating_a_remote_notification\">https:\/\/developer.apple.com\/documentation\/usernotifications\/setting_up_a_remote_notification_server\/generating_a_remote_notification<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>2023\u7248\u4f7f\u7528 PHP \u767c\u9001 iOS \u9060\u8ddd\u63a8\u64ad \u7684\u7bc4\u4f8b. \u91cd\u9ede : 1. php curl \u8981\u652f\u63f4 HTTP\/ &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.nickyoo7.com\/?p=175\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008\u4f7f\u7528 PHP \u767c\u9001 iOS \u9060\u8ddd\u63a8\u64ad\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-175","post","type-post","status-publish","format-standard","hentry","category-ios","entry"],"_links":{"self":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=175"}],"version-history":[{"count":8,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":183,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/175\/revisions\/183"}],"wp:attachment":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}