发送推送通知离子PHP返回401未授权

时间:2018-10-10 03:20:05

标签: php firebase ionic-framework

我一直在尝试使用php将通知发送给fcm google,但未经授权返回401。

我已经检查了令牌,api密钥和脚本,但是结果仍然相同。

有人可以帮助我吗?

这是我的php代码

  public function pushNotif($data){
            $api_key = 'myapikey';
            $registrationIds = $data;
            // prep the bundle
            $msg = array(
                'body'=> 'here is a message. message',
                'title'=> 'This is a title. title',
                'subtitle'=> 'This is a subtitle. subtitle',
                'tickerText'=> 'Ticker text here...Ticker text here...Ticker text here',
                'vibrate'=>1,
                'sound'=>1,
                'largeIcon'=>'large_icon',
                'smallIcon'=>'small_icon'
            );
            $fields = array(
                'to'=>$data,
                'notification'=>$msg
            );

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

        $ch = curl_init();
        curl_setopt( $ch,CURLOPT_URL, 'https://gcm-http.googleapis.com/gcm/send' );
        //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, true );
        curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
        $result = curl_exec($ch );
        curl_close( $ch );
        echo $result;
    }

1 个答案:

答案 0 :(得分:0)

您是否将服务器IP列入白名单?默认情况下,浏览器密钥不是必需的,但服务器密钥是必需的。

您可以在此处检查它:

https://code.google.com/apis/console/#project:[YOUR项目编号]:访问权限

或将其添加到您的curl php init:

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

来源here