如何从使用php的客户端获取registrationID?

时间:2012-09-30 21:12:18

标签: php android google-cloud-messaging

谁能告诉我如何获得“$ registrationIDs = array(”reg id1“,”reg id2“);”,在下面的代码中提到,GCM with PHP (Google Cloud Messaging)

<?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);
       ?>

1 个答案:

答案 0 :(得分:0)

一旦应用收到RegID,您需要构建一个应用可以用来传达RegID的网页。您将不得不介绍将存储RegID的某种服务器端存储(数据库,文件)。

根据您的业务,您可能希望接受一些其他参数。 POST表格对那些人来说是一个好地方。

然后在应用程序中,一旦获得RegID,就会执行HTTP请求,并传递RegID。注意事项:不要从主线程中调用HttpClient。使用AsyncTask或旋转新的Thread