当我的网络托管公司不允许使用GCM端口时,我的Android设计遇到了问题。所以我在考虑建立自己的"有点GCM"为我服务但是,我需要一个构建接收器的起点。那么有可能构建一个类似于GCM和Android应用程序的服务吗?以及从哪里开始研究?
这是我的php端与GCM连接
<?php
/*
* Our "config.inc.php" file connects to database every time we include or require it within a php script. Since we want this script to add a new user to our db, we will be talking with our database, and therefore, let's require the connection to happen:
*/
require ("config.inc.php");
// Replace with the real server API key from Google APIs
$api_Key = "SERVER-API";
// Replace with the real client registration IDs
$registrationIDs = array ();
$data = array (
'message' => 'Hello World!'
);
// ------------------------------
// The recipient registration IDs
// that will receive the push
// (Should be stored in your DB)
//
// Read about it here:
// http://developer.android.com/google/gcm/
// ------------------------------
$ids ="ANDROID-DEVICE-GCM-ID";
// ------------------------------
// Call our custom GCM function
// ------------------------------
sendGoogleCloudMessage ( $data, $ids );
// ------------------------------
// Define custom GCM function
// ------------------------------
function sendGoogleCloudMessage($data, $ids) {
// ------------------------------
// Replace with real GCM API
// key from Google APIs Console
//
// https://code.google.com/apis/console/
// ------------------------------
$api_Key = "AIzaSyCd88kVYixxkcOKbTShw4HHeYjMRbEWTWU";
// ------------------------------
// Define URL to GCM endpoint
// ------------------------------
$url = 'https://android.googleapis.com/gcm/send';
// ------------------------------
// Set GCM post variables
// (Device IDs and push payload)
// ------------------------------
$post = array (
'registration_ids' => $ids,
'data' => $data
);
// ------------------------------
// Set CURL request headers
// (Authentication and type)
// ------------------------------
$headers = array (
'Authorization: key=' . $api_Key,
'Content-Type: application/json'
);
// ------------------------------
// Initialize curl handle
// ------------------------------
$ch = curl_init ();
// ------------------------------
// Set URL to GCM endpoint
// ------------------------------
curl_setopt ( $ch, CURLOPT_URL, $url );
// ------------------------------
// Set request method to POST
// ------------------------------
curl_setopt ( $ch, CURLOPT_POST, true );
// ------------------------------
// Set our custom headers
// ------------------------------
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
// ------------------------------
// Get the response back as
// string instead of printing it
// ------------------------------
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
// ------------------------------
// Set post data as JSON
// ------------------------------
curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode ( $post ) );
// ------------------------------
// Actually send the push!
// ------------------------------
$result = curl_exec ( $ch );
// ------------------------------
// Error? Display it!
// ------------------------------
if (curl_errno ( $ch )) {
echo 'GCM error: ' . curl_error ( $ch );
}
// ------------------------------
// Close curl handle
// ------------------------------
curl_close ( $ch );
// ------------------------------
// Debug GCM response
// ------------------------------
echo $result;
}
?>