class GCM {
//put your code here
// constructor
function __construct() {
}
/**
* 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;
}
}
2013年1月1日 星期二
Google Cloud Messaging PHP
Google Cloud Messaging for Android
Cloud-to-device-messaging (C2DM) is coming out of beta and
getting a new name: Google Cloud Messaging for Android. GCM for Android
incorporates the lessons we learned in the C2DM beta, many of which take
the form of new features. This session will cover the new service
end-to-end and in detail.
Google Cloud Messaging for Android (GCM)已推出,将取代C2DM框架
Google已經發佈了Google Cloud Messaging for Android,該服務對已被廢棄的雲到端訊息方塊架(C2DM)做出改進,取而代之的服務無配額限制、無需註冊,並提供了一套更豐富的全新介面。
GCM提供了在應用伺服器和Android設備之間引入代理的能力,以保障兩者之間可伸縮的雲通信。GCM定義了契約,應用伺服器和Android應用都註冊GCM服務,Google
GCM伺服器維護兩者之間的通信。GCM伺服器負責處理消息佇列和分發至運行在Android設備上的目標應用的各個方面。
GCM還提供另一組重要特性。例如,Android設備接收消息不再需要輪詢。相反,當接收到應用伺服器發送的資料時,GCM伺服器將推送資料至已註冊過的設備。避免輪詢延長了移動設備電池的使用時間。此外,Android應用即使不處於運行狀態也能收到來至GCM的消息,當消息被推送至設備時,如果符合必要的規定,就可以通過廣播喚醒應用。
GCM服務如今成為Google其他眾多介面的一部分(例如,地圖,雲SQL),它們各自都由一個基於Google API控制臺的專案所管理。與Google其他介面不同,GCM服務沒有配額限制,所以無論有多少消息、多少設備使用這項服務,都是完全免費的。
對於現有C2DM框架的用戶來說,該服務已於2012年6月26日被正式棄用,所以在此日期後的任何時間點,該服務都可能被關閉,且不會另行通知。儘管C2DM和GCM無法互通,但是兩個平臺間的遷移很簡單。已有C2DM到GCM遷移文檔可以讓你使用GCM的新特性,以及關於如何用GCM替換C2DM和從C2DM遷移到GCM的開發者的博客文章。
除了完善的GCM技術文檔以外, 網上還有許多其他的GCM資源。Stack Overflow上有關於GCM內部工作方式的一系列問答。如果你正尋求從更高的角度看GCM,你可以看看Francesco Nerieri(GCM專案經理)在三藩市舉行的2012 Google I/0開發者大會上名為GCM平臺為時一小時的演講。
訂閱:
文章 (Atom)