简体   繁体   中英

Send push notification ionic php return 401 unauthorized

I've been trying to send notification with php to fcm google, but its return 401 unauthorized.

I already check my token, api key, my script, but the result is still same.

Anyone can help me?

Here my php code

  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;
    }

Did you whitelist the IP of your server? This is not necessary by default for the browser key, but it is for the server key.

You can check it here:

https://code.google.com/apis/console/#project:[YOUR PROJECT NUMBER]:access

Or add this to your curl php init:

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

source here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM