【问题标题】:Android push notification PHP code not working in 3rd party server, working in local serverAndroid推送通知PHP代码在第三方服务器中不起作用,在本地服务器中工作
【发布时间】:2011-11-06 12:24:48
【问题描述】:

我只是想测试一个带有 android 推送通知 (C2DM) 的应用程序,我确信 android 代码没有任何问题。我正在使用 PHP 代码连接到推送服务器并发送消息,它在我的本地服务器上工作正常。

但如果我在第三方服务器上运行相同的程序,它就不会运行。它没有给我任何错误消息。它显示“HTTP 状态 200 OK”作为响应。

我正在添加代码供您参考,我是从某个网站获得的。

<?php    
    $username ="push_enabled_email_id@gmail.com";  
    $password = "password"; 
    $source="My-Server-Event-Alerter"; //anything that says about ur app  
    $service="ac2dm";  

    // local server
    $message =$_GET ['message']; 

    if ($_GET ['message'] != '' )  
    {  
        $message =$_GET ['message']; 
        echo 'Message sent to server '.$message;  

        $post_params = array( 
                        "Email" => $username, 
                        "Passwd" => $password, 
                        "accountType"=>"HOSTED_OR_GOOGLE", 
                        "source" => $source,
                        "service"=>$service 
                        );   

        $first = true;  
        $data_msg = "";  

        foreach ($post_params as $key => $value)
        {  

            if ($first)  
                $first = false;  
            else  
                $data_msg .= "&";  

            $data_msg .= urlencode($key) ."=". urlencode($value);  
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");  

        $data = array(
                    'accountType' => HOSTED_OR_GOOGLE,  
                    'Email' => $username,
                    'Passwd' => $password,
                    'source'=> $source, 
                    'service'=>$service
                    ); 

        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // for debugging the request
        //curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging
        $response = curl_exec($ch);

        curl_close($ch);   
        echo $response;   
        $pos = strpos($response, "Auth=");  
        $authKey = trim(substr($response, 5+$pos));  
        echo $authKey;  

        echo 'Device Token: '. $deviceToken . '';  
        $data = array(  
        'registration_id' => $deviceToken,  
        'collapse_key' => 'ck_' . 'col_key',  
        'data.message' => $message,  
        'data.title' =>'Request Push Demo'
        );  

        //$data = (is_array($data)) ? http_build_query($data) : $data;   

        $ch = curl_init();  

        curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");  
        echo 'Content-Length:'.strlen($data);  
        $headers = array('Authorization: GoogleLogin auth=' . $authKey,'Content-Length:'.strlen($data));  
        if($headers)
        {  
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
        }

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  
        curl_setopt($ch, CURLOPT_POST, true);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        $messagedata = curl_exec($ch);
        //var_dump(curl_getinfo($ch)); //for debugging the request
        //var_dump($messagedata); 
        // echo $messagedata;  
        curl_close($ch);  
    }  
?>  



<html>
<form action ="./c2dmtest2.php" method="get" enctype="application/x-www-form-urlencoded" name="Send Notification" target="_self">  
<label>Push Message  
<input name="message" type="text" size="50" maxlength="50"></label>  
<input name="Send Message" type="submit" value="Send Message">  
</form>
</html>

【问题讨论】:

  • 您是否确认两台服务器都安装了相同的扩展?特别是卷曲?无效或不受信任的 SSL 连接是否存在问题?您可能必须将 CURL 设置为接受任何 SSL 证书而不对其进行验证。
  • 您是否从 curl_error() 收到任何 CURL 消息?
  • try GCM.time 已更改。
  • 您能否将示例代码中注释的 var_dump 的输出排除在外?

标签: php android push-notification


【解决方案1】:

您能否在代码顶部添加以下行,以便查看潜在错误:

error_reporting(E_ALL);
ini_set('display_errors', '1');

【讨论】:

    猜你喜欢
    • 2019-02-20
    • 2012-02-17
    • 2020-09-30
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    相关资源
    最近更新 更多