Google GCM服务器返回404错误


Google GCM server returns 404 error

我想通过PHP将消息从服务器发送到手机。这是我的代码:

    $apiKey = "AIxxxxxxxxxxxxxxxxxxxx";
    $registrationIDs = array( $c2dmId );
    $url = "https://android.googleapis.com/gcm/send";
    $headers = array( 
                        'Authorization: key='.$apiKey,
                        'Content-Type: application/json'
                    );
    $fields = array(
                    'collapse_key'      => $collapseKey,
                    'data'              => array( 
                        "type"      => $msgType,
                        "extra"     => $msgExtra,
                        "uuid"      => $uuid,
                        "user_id"   => $userId),
                    'registration_ids'  => $registrationIDs,
                    );
    print (json_encode($fields));
    echo "<br/>";
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
    $result = curl_exec($ch);
    $resultInfo = curl_getinfo($ch);
    echo "resultinfo: $resultInfo <br>";
    foreach ($resultInfo as $key => $value) {
        echo "$key => $value <br>";
    }
    curl_close($ch);
    die ("Result: $result");

其中$c2dmId只是我从手机发送到服务器的registrationId。结果我得到(在$result变量中):

<HTML>
<HEAD>
<TITLE>Not Found</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Not Found</H1>
<H2>Error 404</H2>
</BODY>
</HTML>

我不知道为什么。有人能帮忙吗?文档没有说明404代码,所以我真的不知道发生了什么。

哦,我完全忘记了这个问题,很抱歉。我已经发现问题出在哪里了。早些时候,我使用之前发布的代码通过C2DM发送消息。我只做了一些改进,将其调整为GCM。在某些情况下,其中一个变量($msgExtra)可能等于null。这是有意为之的行为,使用C2DM效果很好。不幸的是,当您试图通过GCM在JSON中传递null时,会出现404错误,尽管GCM文档对此只字未提。。。所以我发布的代码是好的,只要你不试图发送null。我的问题的解决方案是用类似"的东西替换null值。再说一遍——很抱歉我刚才发了,我完全忘记了这个问题。

从Google API控制台生成浏览器API密钥,并在"授权"标题中使用它而不是服务器密钥。一旦你这样做,这个错误就会消失。

这是由GCM文档中的一个严重错误引起的,该错误指出您应该在授权标头中使用服务器密钥(如本文所述)。

可能已经为您解决了它。但这是我在设备上测试的您的代码的工作版本。

<?php
$apiKey = "AI.....";
$registrationIDs = array( "You registration key"  );
$url = "https://android.googleapis.com/gcm/send";
$headers = array( 
                    'Authorization: key='.$apiKey,
                    'Content-Type: application/json'
                );
$msgType = "hello";
$msgExtra = "rock";
$uuid = '1234';
$userId = '5678';
/* data you need to define message you want to send in APP.For ex: here myssg in what i am fetching at client side, so in notification it will show hello. You can change accordingly. */ 
$fields = array(
                'collapse_key'      => $collapseKey,
                'data'              => array( 
                    "mymsg"      => $msgType,
                    "extra"     => $msgExtra,
                    "uuid"      => $uuid,
                    "user_id"   => $userId),
                'registration_ids'  => $registrationIDs,
                );
print (json_encode($fields));
echo "<br/>";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch);
$resultInfo = curl_getinfo($ch);
echo "resultinfo: $resultInfo <br>";
foreach ($resultInfo as $key => $value) {
    echo "$key => $value <br>";
}
curl_close($ch);
die ("Result: $result");
?>

这不会给您404错误。希望这将有助于您或某些人在PHP中寻找服务器端实现。