【问题标题】:How to implement C2DM in android如何在android中实现C2DM
【发布时间】:2011-09-18 13:33:17
【问题描述】:

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

【问题讨论】:

    标签: android android-c2dm


    【解决方案1】:

    关注此主题的每个人都应该知道 Google 的变化:

    C2DM 已弃用,并已被 Google Cloud Messaging for Android (GCM) 取代。 谷歌声明:

    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 的 demo app tutorial for GCMmigration

    【讨论】:

      【解决方案2】:

      查看此帖子C2DM Implementation Checklist Step by Step

      在编码之前包括 1 件事; 2 Android App(客户端); 3 服务器端:get ClientLogin等; 4 条注释

      【讨论】:

        【解决方案3】:
        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; 
            }  
        

        【讨论】:

          【解决方案4】:

          此博客http://www.tomasmalmsten.com/tag/c2dm/ 非常适合启动和运行 android c2dm。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-05-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-12-28
            • 2018-03-02
            • 2012-07-29
            相关资源
            最近更新 更多