谷歌GCM服务器返回404错误 [英] Google GCM server returns 404 error

查看:145
本文介绍了谷歌GCM服务器返回404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要发送的邮件从我的服务器到手机,用PHP。 这是我的code:

I want to sent message from my server to phone, by PHP. Here is my code:

    $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变量):

Where $c2dmId is just registrationId which I send to server from phone. As a result I get (in $result variable):

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

和我不知道为什么。任何人都可以帮忙吗?文档忽略了最低位说一下404 code什么,所以我真的不知道是怎么回事。

And I don't know why. Can anyone help? Documentation dosen't say anything about 404 code, so I really don't know what is going on.

推荐答案

呵呵,我完全忘了这个问题,后悔的。我已经发现出了什么问题。 早些时候,我用code我帐前通过C2DM发送消息。我仅取得了一些改进,将其调整到GCM。和变量($ msgExtra)人们可以在某些情况下,等于空。它的目的的行为,并与C2DM它的工作就好了。 不幸的是,当您试图通过GCM传递空的JSON你会得到404错误,虽然GCM文件没有提到这... 因此code,我贴好,只要你不尝试发送空。 我的问题的解决方案是用类似替换为空值。 并再次 - 对不起,我上传它只是现在,我完全忘了这个问题。

Oh, I completely forgot about this question, sorry for that. I already find out what was the problem. Earlier I used that code which I posted before to send messages via C2DM. I made only a few improvements to adjust it to GCM. And one of variables ($msgExtra) could equal null in some case. It was intended behaviour and with C2DM it worked just fine. Unfortunately, when you try to pass null in JSON via GCM you get 404 error, although GCM documentation says nothing about that... So code which I posted is good as far as you don't try to send null. Solution of my problem is to replace null value with something like "". And once again - sorry that I post it just now, I completely forgot about this question.

这篇关于谷歌GCM服务器返回404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆