Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中实现C2DM_Android_Android C2dm - Fatal编程技术网

如何在android中实现C2DM

如何在android中实现C2DM,android,android-c2dm,Android,Android C2dm,我正在尝试在Android中实现C2DM。我能够在C2DM服务器上注册后获得注册码。但当我从第三方服务器发送消息时,它会在日志中显示message message sent和错误代码200,这意味着第三方服务器已成功发送消息。但我的问题是,我无法在设备中接收消息 这个博客非常适合安装和运行android c2dm 查看此帖子 function googleAuthenticate($username, $password, $source="Company-AppName-Version", $

我正在尝试在Android中实现C2DM。我能够在C2DM服务器上注册后获得注册码。但当我从第三方服务器发送消息时,它会在日志中显示message message sent和错误代码200,这意味着第三方服务器已成功发送消息。但我的问题是,我无法在设备中接收消息

这个博客非常适合安装和运行android c2dm

查看此帖子

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

       $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);
        $response = curl_exec($ch);
        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;
        }
        return $matches[2];
   }

   // send message to android,  The message size limit is 1024 bytes in android

  function c2dmSendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText,$extraArr) {
            $headers = array('Authorization: GoogleLogin auth=' . $authCode);
            $data1 = array(
                    'registration_id' => $deviceRegistrationId,
                    'collapse_key' => $msgType,
                    'data.msg' => $messageText         
                    );
            $data2 = array();
            // append 'data' string in key of the array
            if(!empty($extraArr)){
                    foreach($extraArr as $k => $v){
                        $data2['data.'.$k] = $v;
                        unset($extraArr[$k]);
                    }
            }

            $data = array_merge($data1,$data2);
            $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件编码前的事情;2.客户端;3服务器端:etc;4注意

每一个关注这个话题的人都应该意识到谷歌的变化:

C2DM已弃用,已被替换为。 谷歌的声明:

Important: C2DM has been officially deprecated as of June 26, 2012.
This means that C2DM has stopped accepting new users and quota requests. 
No new features will be added to C2DM. However, apps using C2DM will continue to work. 
Existing C2DM developers are encouraged to migrate to the new version of C2DM, 
called Google Cloud Messaging for Android (GCM).
See the C2DM-to-GCM Migration document for more information.
Developers must use GCM for new development.
有一个和也用于来自C2DM