0
最近我想實現推送消息給所有應用用戶cilent。所以我找到了一個Google服務gcm。由於有多種方法來實施和使用GCM。實際的實現:如何使用GCM將消息推送到應用程序?
Develop by:
HTTP mode/php + mysql
Usage:
send message (notification) to all app users
下面是從網上教程中的PHP代碼
<?php
define("GOOGLE_API_KEY", "AIzaSyCJiVkatisdQ44rEM353PFGbia29mBVscA");
define("GOOGLE_GCM_URL", "https://android.googleapis.com/gcm/send");
function send_gcm_notify($reg_id, $message) {
$fields = array(
'registration_ids' => array($reg_id),
'data' => array("message" => $message),
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, GOOGLE_GCM_URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Problem occurred: ' . curl_error($ch));
}
curl_close($ch);
echo $result;
}
$reg_id = "APA91bHuSGES.....nn5pWrrSz0dV63pg";
$msg = "Google Cloud Messaging working well";
send_gcm_notify($reg_id, $msg);
有沒有適合我的情況,而不是不贊成任何教程?謝謝。我已經獲得了API密鑰,並且Google服務帳戶已準備就緒。
是否有幫助:http://www.androidhive.info/2012/ 10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ – user2450263
gcm.jar已貶值,但無論如何感謝您的幫助 – user782104
我的意思是服務器端代碼,但我的錯誤沒有檢查過最新的變化 – user2450263