如何使用php从客户端获取注册ID?
谁能告诉我如何获得下面代码中提到的“$registrationIDs=array(“reg id1”、“reg id2”);”,发布在这里如何使用php从客户端获取注册ID?,php,android,google-cloud-messaging,Php,Android,Google Cloud Messaging,谁能告诉我如何获得下面代码中提到的“$registrationIDs=array(“reg id1”、“reg id2”);”,发布在这里 您需要构建一个网页,一旦应用程序收到RegID,应用程序就可以使用该网页与RegID进行通信。您必须引入某种服务器端存储(数据库、文件),RegID将存储在其中 根据您的业务,您可能需要接受一些附加参数。投递表格是一个很好的地方 然后在应用程序中,一旦获得RegID,就执行HTTP请求,并传递RegID。警告:不要从主线程调用HttpClient。使用异步任
您需要构建一个网页,一旦应用程序收到RegID,应用程序就可以使用该网页与RegID进行通信。您必须引入某种服务器端存储(数据库、文件),RegID将存储在其中 根据您的业务,您可能需要接受一些附加参数。投递表格是一个很好的地方
然后在应用程序中,一旦获得RegID,就执行HTTP请求,并传递RegID。警告:不要从主线程调用
HttpClient
。使用异步任务
,或旋转新的线程
非常感谢!我已经解决了这个问题。这是我第一次在这里发帖,也是我编码生活的开始。
<?php
// Replace with real server API key from Google APIs
$apiKey = "your api key";
// Replace with real client registration IDs
$registrationIDs = array( "reg id1","reg id2");
// Message to be sent
$message = "hi Shailesh";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
//print_r($result);
//var_dump($result);
?>