22

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

android设备的服务器是否需要任何东西来发送消息?

4

5 回答 5

45

注册您自己的服务器系统并获得授权令牌(这是 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;
        }
于 2011-03-08T13:01:10.013 回答
7

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

带有 Zend 框架的 Android C2DM PHP:http ://blog.digitalstruct.com/2010/11/21/android-c2dm-with-php-and-zend-framework/

问候,

麦克风

于 2010-11-21T21:27:06.403 回答
3

看看这个:http ://www.toppa.com/2010/google-clientlogin-php-example/ 否则我会回复你,因为我将在本周晚些时候尝试 C2DM。

于 2010-11-08T10:27:32.243 回答
2

由于 C2DM 已被正式弃用(google c2dm

我建议使用以下链接中描述的新 GCM API: GCM Php implementation

于 2013-07-09T15:44:17.060 回答
0

我尝试使用被接受为正确答案的 php 代码,但它不起作用。我得到的响应 http 代码为“0”。

我在以下链接中找到了相同的代码

在这里需要专家的帮助。

于 2012-07-05T17:58:04.527 回答