PHP發送ANDROID GCM推播訊息程式碼

PHP發送ANDROID GCM推播訊息程式碼

PHP發送ANDROID GCM推播訊息程式碼


資料來源: http://king39461.pixnet.net/blog/post/269212135-php%E7%99%BC%E9%80%81gcm%E6%8E%A8%E6%92%AD%E8%A8%8A%E6%81%AF%E7%A8%8B%E5%BC%8F%E7%A2%BC

<?php

header("Content-Type:text/html; charset=utf-8");
// Message to be sent
$message = $_POST['message'];

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

$fields = array(
                'registration_ids'  => array($_POST['registrationIDs']),
                'data'              => array( "message" => $message ),
                );

$headers = array( 
                    'Authorization: key=' . $_POST['apiKey'],
                    '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 );

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

// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);

echo $result;

?>

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *