在实现GCM PHP脚本时获取InvalidRegistration

时间:2014-11-22 13:38:52

标签: php android push-notification google-cloud-messaging

我正在android中实现推送通知。我在android中正确实现了客户端编码。它工作正常,已经创建了Google API密钥。 问题:但是当我从服务器发送推送通知时(这个脚本是用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新手。我无法找到确切的问题。请帮助我,提前谢谢。

1 个答案:

答案 0 :(得分:0)

在路径中更改此内容

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

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

这里test是服务器上的文件夹,gcm是子文件夹。 gcm.php应该放在子文件夹中。