![](/img/trans.png)
[英]how to send push notification to the android device using GCM by java server?
[英]Android GCM Push Notification server side in java
關於GCM,我有幾個問題。
Decide which Google-provided GCM connection server you want to use— HTTP or XMPP (CCS).
現在我了解了實踐中這兩者之間的區別,但是當他說決定選擇哪一個時,我不確定他對我有什么期望? 我需要為這兩個做不同的事情嗎? https://android.googleapis.com/gcm/send
nd傳遞參數發出POST請求嗎? 我可以為此使用HTTPClient,對嗎? 有什么特別的嗎? 2-基本上就是這樣,這是我的服務器代碼,這是我服務器中的PHP文件
public function send_push(){
$message = "your message";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => array("GCM_REGISTRATION"),
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=YOUR_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 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
}
GCM_REGISTRATION是您首次運行應用以在GCM中注冊后所獲得的哈希值。 如果一切正常,您可以在logcat中看到
api_key是創建應用程序時獲得的哈希(您可能已經在文檔中看到了)
3-我認為它不必是服務器,因為它實際上是將消息發送到Google服務器,然后再將其傳遞到您的應用程序中
是的,http和ccs服務器之間有很大的區別。 前者涉及簡單的http請求。 后者需要xmpp協議實現。
是的,如果您采用的是gcm http方法,則任何提交http請求的代碼都應該起作用。
出於測試目的,您不需要服務器。 任何提交http請求的代碼都可以。 但是,您必須將設備注冊ID傳遞到服務器代碼,才能發送GCM消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.