2013-05-14 41 views
3

我已经搜索了很多,但找不到此问题的任何解决方案。尝试向GCM发送数据时MissingRegistraton错误

我正在使用PHP服务器,并试图将PushNotifications发送到我的Android应用程序。但是,当我在浏览器中试用我的代码时,出现此错误:“Error = MissingRegistration”。

这里是我运行代码:

registration_ids = array($regId); 
     $message = array(
      'hangMessage' => $message, 
      'userId' => $user_id 
      ); 

     $result = $gcm->send_notification($registration_ids, $message); 

这就是我所说的代码:

$url = "https://android.googleapis.com/gcm/send"; 

    $fields = array(
     'registration_ids' => $regisration_ids, 
     'data' => $message, 
     ); 

    $headers = array(
     'Authorization: key=' . GOOGLE_API_KEY, 
     '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_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    //Disabling SSL Certificate support temporarly 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

    //Execute psot 
    $result = curl_exec($ch); 
    if($result == false){ 
     die('Curl failed: ' . Curl_error($ch)); 
    } 

    //Close connection 
    curl_close($ch); 
    echo $result; 

请求不连一路根据前来服务器Google API控制台报告系统。

任何人都有什么想法可能是错的?

回答

3

你的要求应该是这样的:

Content-Type:application/json 
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA 

{ 
    "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."], 
    "data" : { 
    ... 
    }, 
} 

因此,我认为,问题可能出在这行:

'Content-Type= application/json' 

尝试改变=:。由于内容类型标头无效,GCM服务器可能会假定内容类型的默认值(可能为application/x-www-form-urlencoded;charset=UTF-8,在这种情况下注册ID需要不同的密钥,这将解释MissingRegistration错误)。

顺便说一句,你得到一个MissingRegistraton错误的事实意味着请求确实到达GCM服务器。 GCM请求未记录在Google API控制台报告系统中。

+0

UTF8需要什么不同的密钥? – mcmillab 2014-12-14 23:56:48

相关问题