【问题标题】:C2DM implementation PHP codeC2DM实现PHP代码
【发布时间】:2011-05-06 12:14:59
【问题描述】:

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

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

【问题讨论】:

    标签: php android push-notification android-c2dm


    【解决方案1】:

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

    【讨论】:

    • 我想问 1 个新事物,即从 android 设备或应用程序服务器生成令牌的位置。这里 android 设备仅用于接收应用服务器发送的消息。我不想从安卓设备发送消息
    • 嗨,请告诉我谁会从 android 应用程序或终端服务器发送 authtoken 请求。
    【解决方案2】:

    我在我的博客中创建了一个使用 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/

    问候,

    迈克

    【讨论】:

      【解决方案3】:

      注册您自己的服务器系统并获得授权令牌(这是 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;
              }
      

      【讨论】:

      • 会话使用cookies,不是吗?这对服务器脚本有什么作用?
      • 是的,会话通常使用 cookie。如果身份验证令牌已知,这里会话仅用于省略不必要的身份验证请求。就这些。如果您从另一个脚本调用此脚本,则不需要此机制,因为您的脚本知道它是否已经拥有身份验证令牌,不是吗?
      • 如果我从 cron 运行脚本,我需要一种方法来存储身份验证令牌。我想那不会是会话。
      • 您是否应该每次都获得一个新的身份验证令牌,或者这是可以存储的东西吗? (数据库/文件系统/等)
      • @MrZorn : 你应该不时获得授权令牌,因为我认为它已经过期了,所以每次你获得新的授权令牌时,你都可以更新数据库中的值
      【解决方案4】:

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

      我在following link找到了相同的代码

      需要专家的帮助。

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-09-18
          • 1970-01-01
          • 1970-01-01
          • 2011-11-23
          • 1970-01-01
          • 2015-01-09
          • 2021-04-22
          • 2020-06-28
          相关资源
          最近更新 更多