请向我发送推送通知的同时帮助我解决此问题,因为我遇到了405错误。下面是推送通知的代码
function androidPush($regid,$message,$title,$imageUrl='',$url)
{
if (!defined('API_ACCESS_KEY')) define('API_ACCESS_KEY', 'mykey');
$registrationIds = array_filter($regid);
$Regid = array();
foreach($registrationIds as $rid){
$Regid[] = $rid;
}
if(empty($registrationIds) || $title==""){
return false;
}else{
$msg = array
(
'message' => html_entity_decode(trim($message)),
'title' => $title,
'image' => $imageUrl,
'url' =>html_entity_decode(trim($url)),
'vibrate' => 1,
'sound' => 1
);
$fields = array();
$fields['data'] = $msg;
$Idcount = count($Regid);
if($Idcount > 1000)
{
$newId = array_chunk($Regid, 1000);
for($i = 0;$i < count($newId);$i++){
$fields['registration_ids'] = $newId[$i];
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
}
return $result;
} else{
$fields['registration_ids'] = $Regid;
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch);
curl_close( $ch );
return $result;
}
}
}
答案 0 :(得分:0)