发送推送通知 Ionic PHP 返回 401 未经授权。

3
我一直在尝试使用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
你是否将服务器IP加入白名单?对于浏览器密钥,默认情况下不需要,但对于服务器密钥则需要。
你可以在这里检查: https://code.google.com/apis/console/#project:[YOUR PROJECT NUMBER]:access
或者将以下内容添加到你的curl php init中:
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

源代码这里


我已经检查了你在上面链接中给我的Google APIs访问权限。并且我专注于已经激活的GCM。我也已经在我的PHP代码中添加了curl。但结果仍然相同。 - Agustino Whickey
@AgustinoWhickey,你有没有查看我发布的源链接?我相信你的答案在那里。 - Kevin Dias

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接