{"id":16689,"date":"2023-12-12T11:46:40","date_gmt":"2023-12-12T03:46:40","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=16689"},"modified":"2023-12-12T11:46:40","modified_gmt":"2023-12-12T03:46:40","slug":"php%e5%bc%80%e5%8f%91%e5%ae%89%e5%8d%93app%e6%8e%a8%e9%80%81%e6%b6%88%e6%81%af%e6%80%8e%e4%b9%88%e5%bc%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/16689\/","title":{"rendered":"php\u5f00\u53d1\u5b89\u5353app\u63a8\u9001\u6d88\u606f\u600e\u4e48\u5f04\uff1f"},"content":{"rendered":"

Android\u7cfb\u7edf\u63d0\u4f9b\u4e86Google Cloud Messaging (GCM)\u7528\u4e8e\u5411\u79fb\u52a8\u8bbe\u5907\uff08Android \u8bbe\u5907\uff09\u53d1\u9001\u63a8\u9001\u901a\u77e5\u3002GCM \u670d\u52a1\u672c\u8eab\u652f\u6301 HTTP\/JSON\u534f\u8bae\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528 PHP \u5f00\u53d1\u63a8\u9001\u901a\u77e5\u3002<\/p>\n

1. \u83b7\u53d6 Google Cloud Messaging API Key<\/p>\n

\u5728 Google \u63a7\u5236\u53f0\u7533\u8bf7\u751f\u6210\u4e00\u4e2a Google Cloud Messaging API Key\u3002\u751f\u6210\u5b8c\u6210\u540e\uff0c\u5c06 API Key \u5907\u4efd\u4e0b\u6765\u5907\u7528\u3002<\/p>\n

2. \u83b7\u53d6\u8bbe\u5907\u7684 Registration ID<\/p>\n

\u5f53\u5e94\u7528\u7b2c\u4e00\u6b21\u542f\u52a8\u65f6\uff0c\u5e94\u8be5\u901a\u8fc7 GCM \u5411\u670d\u52a1\u5668\u53d1\u9001\u4e00\u4e2a\u6ce8\u518c\u8bf7\u6c42\uff0c\u4ee5\u4fbf\u5728\u670d\u52a1\u5668\u4fdd\u5b58\u8bbe\u5907\u7684 Registration ID\u3002<\/p>\n

3. \u7f16\u5199 PHP \u4ee3\u7801\u8fdb\u884c\u6d88\u606f\u63a8\u9001<\/p>\n

“`php<\/p>\n<\/p>\n

\/\/ Replace with real client registration IDs<\/p>\n

$registrationIds = array( ‘registration_id_0’, ‘registration_id_1’ );<\/p>\n

\/\/ Message to be sent<\/p>\n

$message = “Your message here.”;<\/p>\n

\/\/ Set POST variables<\/p>\n

$url = ‘https:\/\/gcm-http.googleapis.com\/gcm\/send’;<\/p>\n

$fields = array(<\/p>\n

‘registration_ids’ => $registrationIds,<\/p>\n

‘data’ => array( “message” => $m\u5b89\u5353APP\u5f00\u53d1<\/a>essage ),<\/p>\n

);<\/p>\n

$headers = array(<\/p>\n

‘Authorization: key=’ . API_KEY,<\/p>\n

‘Content-Type: application\/json’<\/p>\n

);<\/p>\n

\/\/ Open connection<\/p>\n

$ch = curl_init();<\/p>\n

\/\/ Set the URL, number of POST vars, POST data<\/p>\n

curl_setopt( $ch, CURLOPT_URL, $url );<\/p>\n

curl_setopt( $ch, CURLOPT_POST, true );<\/p>\n

curl_setopt( $ch, CURLOPT_\u5b89\u5353app\u5236\u4f5c<\/a>HTTPHEADER, $headers);<\/p>\n

curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );<\/p>\n

curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );<\/p>\n

\/\/ Execute post<\/p>\n

$result = curl_exec($ch);<\/p>\n

\/\/ Close con<\/p>\n

<\/figure>\n<\/p>\n

nection<\/p>\n

curl_close($ch);<\/p>\n

echo $result;<\/p>\n

?><\/p>\n

“`<\/p>\n

4. \u5c06 PHP \u4ee3\u7801\u96c6\u6210\u5230\u5e94\u7528<\/p>\n

\u5728\u5e94\u7528\u4e2d\u6dfb\u52a0\u4ee3\u7801\uff0c\u5f53\u9700\u8981\u5c06\u63a8\u9001\u901a\u77e5\u53d1\u9001\u5230\u5ba2\u6237\u7aef\u65f6\uff0c\u8c03\u7528 PHP \u63a5\u53e3\u5373\u53ef\u3002<\/p>\n

\u4ee5\u4e0a\u662f\u4f7f\u7528 PHP \u5f00\u53d1 Android \u63a8\u9001\u901a\u77e5\u7684\u57fa\u672c\u539f\u7406\uff0c\u5177\u4f53\u5b9e\u73b0\u8fd8\u9700\u8981\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u8fdb\u884c\u5177\u4f53\u7684\u5f00\u53d1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Android\u7cfb\u7edf\u63d0\u4f9b\u4e86Google Cloud Messaging (GCM)\u7528\u4e8e\u5411\u79fb\u52a8\u8bbe\u5907\uff08Android \u8bbe\u5907\uff09\u53d1\u9001\u63a8\u9001\u901a\u77e5\u3002GCM \u670d\u52a1\u672c\u8eab\u652f\u6301 HTTP\/JSON\u534f\u8bae\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528 PHP<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[23263,81,15831,331,7801],"class_list":["post-16689","post","type-post","status-publish","format-standard","hentry","category-zuoapp","tag-eclipseapp","tag-81","tag-app","tag-331","tag-7801"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/16689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=16689"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/16689\/revisions"}],"predecessor-version":[{"id":16706,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/16689\/revisions\/16706"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=16689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=16689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=16689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}