C2DM 实现 PHP 代码

我正在创建使用C2DM推送通知的Android应用程序。但是我在创建php代码以使用c2dm发送消息时遇到了问题。请指导我如何使用php代码发送消息。实际上,有一个问题,即如何获取客户端身份验证令牌。我已经看到了 http://code.google.com/android/c2dm/index.html#server 网址,但根据这个我已经创建了Android应用程序,我也得到了注册ID,我也发送给用户,但服务器如何使用它来发送应用程序。

服务器从Android设备发送消息是否需要任何东西?


答案 1

要注册您自己的服务器系统并获得授权令牌(这是Cpt. Ohlund建议的):

function googleAuthenticate($username, $password, $source="Company-AppName-Version", $service="ac2dm") {    


        session_start();
        if( isset($_SESSION['google_auth_id']) && $_SESSION['google_auth_id'] != null)
            return $_SESSION['google_auth_id'];

        // get an authorization token
        $ch = curl_init();
        if(!ch){
            return false;
        }

        curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
        $post_fields = "accountType=" . urlencode('HOSTED_OR_GOOGLE')
            . "&Email=" . urlencode($username)
            . "&Passwd=" . urlencode($password)
            . "&source=" . urlencode($source)
            . "&service=" . urlencode($service);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
        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 the request

        $response = curl_exec($ch);

        //var_dump(curl_getinfo($ch)); //for debugging the request
        //var_dump($response);

        curl_close($ch);

        if (strpos($response, '200 OK') === false) {
            return false;
        }

        // find the auth code
        preg_match("/(Auth=)([\w|-]+)/", $response, $matches);

        if (!$matches[2]) {
            return false;
        }

        $_SESSION['google_auth_id'] = $matches[2];
        return $matches[2];
    }

要向手机发送消息:

// $msgType: all messages with same type may be "collapsed": if multiple are sent,
// only the last will be received by phone. 
function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText) {

            $headers = array('Authorization: GoogleLogin auth=' . $authCode);
            $data = array(
                'registration_id' => $deviceRegistrationId,
                'collapse_key' => $msgType,
                'data.message' => $messageText //TODO Add more params with just simple data instead           
            );

            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
            if ($headers)
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);


            $response = curl_exec($ch);

            curl_close($ch);

            return $response;
        }

答案 2

我在我的博客中创建了一个使用Android C2DM的示例。我使用Zend框架和我写的自定义组件。它应该为您提供在PHP中处理Android C2DM实现所需的基本信息。

Android C2DM PHP w/ Zend Framework: http://blog.digitalstruct.com/2010/11/21/android-c2dm-with-php-and-zend-framework/

问候

话筒


推荐