【问题标题】:Send push notification ionic php return 401 unauthorized发送推送通知 ionic php 返回 401 未授权
【发布时间】:2019-03-14 21:17:20
【问题描述】:

我一直在尝试使用 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;
    }

【问题讨论】:

    标签: php firebase ionic-framework


    【解决方案1】:

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

    你可以在这里查看:

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

    或者将这个添加到你的 curl php init 中:

    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
    

    来源here

    【讨论】:

    • 我已经签入了您在上面的链接中提供给我的 google apis 访问权限。我专注于已经活跃的 GCM。我也已经在我的 php 代码中添加了 curl。但结果还是一样。
    • @AgustinoWhickey 你检查过我发布的源链接吗?我确定你的答案在那里
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 2016-06-03
    • 2020-06-25
    • 2014-07-18
    相关资源
    最近更新 更多