How to implement C2DM in android

馋奶兔 提交于 2019-12-12 05:25:55

问题


I am try implement C2DM in Android. I am able to get Registration code after registration with C2DM Server. But when I send message from 3rd party server, it displays message "message sent" and error code 200 in Log, which means 3rd party server have successfully send the message. BuT my problem is that, I am not able to receive message in device.


回答1:


This blog http://www.tomasmalmsten.com/tag/c2dm/ is excellent for getting android c2dm up and running.




回答2:


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



回答3:


check this post C2DM Implementation Checklist Step by Step

Including 1 Things before coding; 2 Android App(client side); 3 Server side: get ClientLogin etc; 4 Notes




回答4:


Everyone tuning in into this topic should be aware of a change at Google:

C2DM is deprecated and has been replaced by Google Cloud Messaging for Android (GCM). Google's statement:

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.

There is a demo app tutorial for GCM and also for migration from C2DM.



来源:https://stackoverflow.com/questions/6409488/how-to-implement-c2dm-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!