dongpao5658 2012-10-13 21:25
浏览 47
已采纳

GCM丢弃消息

I have a demo of gcm working here: http://leobee.com/android/push/login/gcm/updateusers.php

If you refresh the page, you will see a new random number. Only 1 out of 4 page refreshes delivers a message to gcm. Is this normal or is there some tweeks to the code I can use?

The page is not being cached.

code:

<?php


require_once '../include/DB_Functions.php';


// get database access
$db = new DB_Functions();

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

//api key 
$apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$result;

//array for phones connected to this service
$registrationIDs = array();


$randomNum=rand(10,100);


echo "this is updateusers".$randomNum;
$_POST['message']="updateusers".$randomNum;
if (isset($_POST['message']) && $_POST['message'] != ''){   

echo "<br>if (isset) updateusers".$randomNum;
    // Message to be sent
    $id= ''.mysql_real_escape_string(htmlentities($_POST['server_id'])).'';
    $new_message= ''.mysql_real_escape_string(htmlentities($_POST['message'])).'';


    // get client registration IDs
    $query ="SELECT * FROM GoogleCloudMsg";

    $queryresult=mysql_query($query);

    while($row=mysql_fetch_assoc($queryresult)){
    echo "<br>While loop updateusers".$randomNum;
        $regId=$row['GCMPhoneRegisteredId'];

        array_push($registrationIDs,$regId);

}



// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields;

if(!$id || $id==""){

    $fields = 
array(
'registration_ids' => $registrationIDs, 
'data' => array("message" => $new_message), 
'delay_while_idle'=> false,
'collapse_key'=>"".$randomNum.""
);

    echo "<br> id is blank updateusers".$randomNum;

}else{

$fields = 
array(
'registration_ids' => $registrationIDs, 
'data' => array("message" =>$new_message,"server_id"=>$id), 
 'delay_while_idle' => 'false',
'collapse_key'=>"".$randomNum.""
);

echo "<br>id exists updateusers".$randomNum;

}
$headers = array('Authorization: key=' . $apiKey, 'Content-Type: application/json');

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//     curl_setopt($ch, CURLOPT_POST, true);
//     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);
echo $result;

}

echo "<br>mysql close updateusers".$randomNum;
 mysql_close();


?>
  • 写回答

1条回答 默认 最新

  • duanbi8529 2012-10-15 01:20
    关注

    There were two ways to fix this.

    1. generate and install an CA certificate.

    2. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    not verifying the host.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿