zoukankan      html  css  js  c++  java
  • Android 云端推送C2DM php实现向终端推送消息

    Android云端推送C2DM PHP端实现向终端发送消息

    $registration = '===Registration ID generated for the app===';
    $auth = '===Google auth===';

    //curl_setopt参数
    $options = array(
            CURLOPT_TIMEOUT        =>    60,
            CURLOPT_CONNECTTIMEOUT    =>     10,
            CURLOPT_URL        =>    "https://android.apis.google.com/c2dm/send",
            CURLOPT_POST        =>    TRUE,
            CURLOPT_RETURNTRANSFER    =>    TRUE
    );
    //post参数
    $params = array(
                    "registration_id"    =>    $registration,
                    "collapse_key"        =>    "===a string===",
                    "data.message"        =>    "xxxx" 这个可以有多对
    );
    $postdata = http_build_query($params,null,"&");    
    $options[CURLOPT_POSTFIELDS] = $postdata;

    //http header
    $headers = array();
    $headers[] = "Content-Length: " . strlen($postdata); #这个地方可费了劲了content-length一定要放在第一个,不然会报那个411.That an error的错误
    $headers[] = "Authorization: GoogleLogin auth=" . $auth;

    $options[CURLOPT_HTTPHEADER] = $headers;

    //debug
    $options[CURLOPT_HEADER] = TRUE;
    $options[CURLOPT_VERBOSE] = TRUE;
    $ch = curl_init();
    if(!function_exists('curl_setopt_array')){
        foreach((array)$options as $key=>$value){
            curl_setopt($ch, $key, $value);
        }
    } else {
        curl_setopt_array( $ch,$options);    
    }
    $result = curl_exec($ch);
    curl_close( $ch);

    成功的话会返回id=0:1329988332025998%36eaed9800000031 类似的串
    如果失败则会返回Error=错误消息
    具体参考地址请看:http://code.google.com/intl/zh-CN/android/c2dm/#example

  • 相关阅读:
    SQl语句学习笔记(二)
    Adaboost 算法
    降维PCA技术
    scanf 格式化字符串详解
    大小端模式和位域详解(转载)
    推荐系统开源软件列表汇总和点评(转载)
    遗传算法入门(转载)
    大白话解析模拟退火算法(转载)
    机器学习相关——协同过滤(转载)
    python面向对象之单例模式
  • 原文地址:https://www.cnblogs.com/sblack/p/2365238.html
Copyright © 2011-2022 走看看