2014-11-22 29 views
2

我在android中實現推送通知。我已經在android中正確實現了客戶端編碼。它的工作正常,已經創建了Google API密鑰。 問題:但是,當我從服務器發送推送通知(此腳本使用php編寫)時,我收到錯誤消息。錯誤是這樣的 -獲取InvalidRegistration實施GCM PHP腳本

  {"multicast_id":8690314483687932029,"success":0,"failure":1,"canonical_ids":0,"results": [{"error":"InvalidRegistration"}]} 

而我的服務器端腳本是這樣的。

   <?php 
     //Generic php function to send GCM push notification 
     function sendMessageThroughGCM($registatoin_ids, $message) { 
      //Google cloud messaging GCM-API url 
     $url = 'https://android.googleapis.com/gcm/send'; 
     $fields = array(
     'registration_ids' => $registatoin_ids, 
     'data' => $message, 
     ); 
     // Update your Google Cloud Messaging API Key 
     define("GOOGLE_API_KEY", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");   
     $headers = array(
     `` 'Authorization: key=' . GOOGLE_API_KEY, 
      'Content-Type: application/json' 
     ); 
     $ch = curl_init(); 
     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_SSL_VERIFYHOST, 0); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 
     $result = curl_exec($ch);    
     if ($result === FALSE) { 
     die('Curl failed: ' . curl_error($ch)); 
     } 
     curl_close($ch); 
     return $result; 
     } 
     ?> 
    <?php 

     //Post message to GCM when submitted 
    $pushStatus = "GCM Status Message will appear here"; 
    if(!empty($_GET["push"])) {  
     $gcmRegID = file_get_contents("GCMRegId.txt"); 
     $pushMessage = $_POST["message"]; 
     if (isset($gcmRegID) && isset($pushMessage)) {  
      $gcmRegIds = array($gcmRegID); 
      $message = array("m" => $pushMessage); 
      $pushStatus = sendMessageThroughGCM($gcmRegIds, $message); 
      }  
     } 

     //Get Reg ID sent from Android App and store it in text file 
     if(!empty($_GET["shareRegId"])) { 
     $gcmRegID = $_POST["regId"]; 
     file_put_contents("GCMRegId.txt",$gcmRegID); 
      echo "Done!"; 
     exit; 
     }  
     ?> 
     <html> 
     <head> 
     <title>Google Cloud Messaging (GCM) in PHP</title> 

     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
     <script> 
     $(function(){ 
      $("textarea").val(""); 
     }); 
      function checkTextAreaLen(){ 
      var msgLength = $.trim($("textarea").val()).length; 
      if(msgLength == 0){ 
      alert("Please enter message before hitting submit button"); 
      return false; 
       }else{ 
      return true; 
      } 
     } 
      </script> 
     </head> 
     <body> 
      <div id="formdiv"> 
     <h1>Google Cloud Messaging (GCM) in PHP</h1> 
      <form method="post" action="/gcm/gcm.php/?push=true" onsubmit="return  checkTextAreaLen()">                   
       <textarea rows="5" name="message" cols="45" placeholder="Message to send via GCM"> </textarea>  <br/> 
       <input type="submit" value="Send Push Notification through GCM" /> 
     </form> 
      </div> 
      <p id="status"> 
     <?php echo $pushStatus; ?> 
     </p>   
     </body> 
     </html> 

我是Android新手。我無法找到確切的問題。請提前幫助,幫助我。

回答

0

改變這在你的路徑

action="/gcm/gcm.php/?push=true" 

action="test/gcm/gcm.php/?push=true" 

這裏測試的是服務器上的文件夾和GCM是一個子文件夾。 gcm.php應放在子文件夾中。