C2DM实现的PHP代码

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

服务器是否需要从Android设备获取任何信息来发送消息?

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

1
会话使用 cookies,不是吗?那对于服务器脚本如何工作呢? - Thomas Ahle
是的,一般情况下会话使用 cookies。在这里,仅当已知身份验证令牌时,才使用会话以省略不必要的身份验证请求。就是这样。如果您从另一个脚本调用此脚本,则不需要此机制,因为您的脚本知道它是否已经拥有身份验证令牌,对吧? - Yar
如果我从cron运行脚本,我需要一种存储授权令牌的方法。我猜那不会是会话。 - Thomas Ahle
@MrZorn:你应该定期获取认证令牌,因为我认为它会过期,所以每次获取新的认证令牌时,你可以更新数据库中的值。 - Houcine
嘿,你说sendMessageToPhone()函数用于向手机发送消息。那么,如何向多个手机发送消息? - Kannika
显示剩余2条评论

7

3

我想问一个新问题,是从安卓设备还是应用服务器生成令牌。在这里,安卓设备仅用于接收应用服务器发送的消息,我不想从安卓设备发送消息。 - Amit Thaper
请告诉我是谁发送身份验证令牌请求,是来自安卓应用程序还是终端服务器。 - Amit Thaper

1

0

我尝试使用被接受为正确答案的 PHP 代码,但它不起作用。 我得到的响应 HTTP 状态码是 "0"。

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

需要专家们在这里帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接