0

我尝试通知我的 android 设备,但返回错误。

我正在使用 curl 将数据从我的服务器发布到 Google API。

<?php

$api_key = "AIza*******tdv2g";
$urlPost = "https://android.googleapis.com/gcm/send";

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: key=$api_key","Content-Type:application/x-www-form-urlencoded;charset=UTF-8"));
curl_setopt($ch, CURLOPT_POSTFIELDS, "registration_ids=APA9**o-LEuKN********dJa**Wjz4Juujv9FEiMJajt4");
curl_setopt($ch, CURLOPT_URL, $urlPost);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);

// exécution de la session
curl_exec($ch);

?>

我的浏览器上返回的消息:“Error=MissingRegistration”

任何想法 ?谢谢 !

4

1 回答 1

1

问题解决了!

我找到了另一种方法,也使用 curl。现在它开始工作了!

$regId = "AP********4Juujv9*****MJajt4";

$registration_ids = array($regId);
$message = array(
    'hangMessage' => "bonjour !",
    'userId' => "12"
);

$result = sendNotification($registration_ids, $message);

function sendNotification($registration_ids, $message)
{

    $api_key = "AIzaS*****v2g";
    $urlPost = "https://android.googleapis.com/gcm/send";


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

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

    //Open connection
    $ch = curl_init();

    //Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $urlPost);
    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 psot
    $result = curl_exec($ch);
    if ($result == false)
    {
        die('Curl failed: ' . Curl_error($ch));
    }

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

?>
于 2013-11-13T09:27:59.953 回答