设备通过gcm google api和php服务器未收到推送通知

时间:2013-11-11 13:15:30

标签: php android push-notification

我已关注this tutorial, 我设法在cgm服务器中使用设备注册该应用程序 但我无法收到服务器到设备的推送通知 我在genymotion模拟器和真实设备上测试过但总是没有在设备上收到通知 我不知道错误和不知道如何解决这个问题

以下是发送推送通知的方法:

    /**
 * Sending Push Notification
 */
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=' . GOOGLE_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, $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;
}

我已经设置了api密钥和项目ID,一切都已完成。

1 个答案:

答案 0 :(得分:0)

试试这个:

$apiKey = "your api key goes here (use browser api)";
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);