C2DM执行PHP codeC2DM、PHP、code

2023-09-11 20:29:23 作者:别等时光非礼了梦想

我创造那里C2DM推送通知使用的Andr​​oid应用程序。但我在创建PHP code使用C2DM发送邮件的问题。请指导我如何使用PHP的code发送消息。其实,有关于这一点,如何让客户端身份验证令牌的一个问题。我已经看到了http://$c$c.google.com/android/c2dm/index.html#server URL但根据这一点,我已经创建的Andr​​oid应用程序,然后拿到了注册ID也和我也发送给用户,但如何服务器使用该发送的应用程序。

I am creating the Android application where C2DM push notification is used. But i have a problem in creating the php code to use c2dm for sending messages. please guide me how to use the php code to send the messages. Actually there is a problem regarding this that how to get client auth token. I have seen the http://code.google.com/android/c2dm/index.html#server url but according to this i have created the android application and i got the registration id also and i also send to the user but how server uses this to send the application.

有需要的服务器从Android设备的任何发送消息?

is there anything needed for the server from the android device to send the messages?.

推荐答案

要注册自己的服务器系统,并得到是否授权令牌(这是CPT奥朗德提出的。):

To register your own server system and obtain the Authorise Tokens (this is what Cpt. Ohlund proposed):

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;
        }