同时向千位用户发送android推送通知,获得405错误

时间:2016-10-06 06:01:04

标签: php android push-notification google-cloud-messaging

请向我发送推送通知的同时帮助我解决此问题,因为我遇到了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;
                         }

                }
}

1 个答案:

答案 0 :(得分:0)

405错误指出方法不允许more

尝试添加curl_setopt($ch, CURLOPT_CUSTOMREQUEST, POST);

因为我认为gcm代码需要发布请求..

相关问题