在向数千名用户发送 android 推送通知时出现 405 错误
Posted
技术标签:
【中文标题】在向数千名用户发送 android 推送通知时出现 405 错误【英文标题】:while sending android push notification to thousand of user getting 405 error 【发布时间】:2016-10-06 06:01:03 【问题描述】:请在向我遇到 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;
【问题讨论】:
我不确定,但我想您可能使用的是旧端点?https://android.googleapis.com/gcm/send
。我认为当前的 GCM 端点是https://gcm-http.googleapis.com/gcm/send
。
【参考方案1】:
405 错误表明 Method Not Allowed more
尝试添加curl_setopt($ch, CURLOPT_CUSTOMREQUEST, POST);
因为我认为 gcm 代码需要发布请求..
【讨论】:
我担心的是,当我使用此功能时,用户会收到通知,但响应会出现 405 错误。以上是关于在向数千名用户发送 android 推送通知时出现 405 错误的主要内容,如果未能解决你的问题,请参考以下文章
我在向 ios 设备发送推送通知时收到 errorNum :8