GCM推送服务器端,要使用的API密钥? [英] GCM Push Server side, which API Key to use?

查看:1522
本文介绍了GCM推送服务器端,要使用的API密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑。我有一个使用谷歌从开发者控制台中的关键一GCM PHP服务器端的应用服务器。我可以创建的Andr​​oid按键按在抵达该处开始手册中的说明,或者我可以创建一个浏览器键,服务器密钥或OAuth的关键。

I'm a bit confused. I have a GCM PHP server side app server that uses a key from Google Developer Console. I can create Android key as per the instructions given in getting started guide here, or I can create a browser key, server key or OAuth key.

有人可以告诉我应该通过GCM发送消息到Android设备时,在服务器端PHP使用哪个键?

Can somebody tell which key I should use on the server side PHP when sending messages via GCM to Android devices?

这是将消息发送到GCM功能

This is the function that sends the message to GCM

    public function send_notification($registatoin_ids, $message) {
    // include config
    include_once './config.php';

    // Set POST variables
    $url = 'https://android.googleapis.com/gcm/send';

    $fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
    );

    $headers = array(
        'Authorization: key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
        'Content-Type: application/json'
    );
    // Open connection
    $ch = curl_init();

    // Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    // Execute post
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    // Close connection
    curl_close($ch);
    echo $result;
}

显然XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX字符串由我在原来的code API_KEY替换。

Obviously the XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX string is replaced by my API_KEY in the original code.

PS:我的Andr​​oid是注册精细与GCM,我已经发送到服务器,以及注册ID,它是发送消息后请求总是返回401未经授权,我怕我使用了错误的键

p.s: My android is registering fine with the GCM, I have the registration id sent to server as well, it's the send message post request that always returns 401 Unauthorized, I'm afraid I'm using a wrong key?

p.s2:我已经尝试了所有3类API_KEYs没有成功

p.s2: I've tried all 3 sort of API_KEYs without success.

推荐答案

解决

我是专注于Android或服务器API_KEY,但实际上我不得不使用浏览器的关键。

I was concentrating on Android or Server API_KEY but I actually had to use browser key.

此外,最初测试我删除从白名单的所有IP地址,以确保不打它的作用因素。

Moreover, to test initially I removed all IP addresses from white listing to make sure that's not the factor playing it's role.

现在我有推送服务使用浏览器的API密钥和IP限制到位白名单上面code工作。

Now I have the push service working with above code using Browser API Key and IP restriction in place for white listing.

干杯:)

这篇关于GCM推送服务器端,要使用的API密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆