, 그것은 주제를 통해이를 달성하기 위해 지금 수도 있습니다 등록 후
https://developers.google.com/cloud-messaging/topic-messaging
, 당신은 HTTP를 통해 GCM 서버에게 메시지를 보낼 것이다 :
https://gcm-http.googleapis.com/gcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"to": "/topics/foo-bar",
"data": {
"message": "This is a GCM Topic Message!",
}
}
예 :
JSONObject jGcmData = new JSONObject();
JSONObject jData = new JSONObject();
jData.put("message", "This is a GCM Topic Message!");
// Where to send GCM message.
jGcmData.put("to", "/topics/foo-bar");
// What to send in GCM message.
jGcmData.put("data", jData);
// Create connection to send GCM Message request.
URL url = new URL("https://android.googleapis.com/gcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "key=" + API_KEY);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
// Send GCM message content.
OutputStream outputStream = conn.getOutputStream();
outputStream.write(jGcmData.toString().getBytes());
,451,515,
그리고 당신의 클라이언트/주제/foo는 바에 가입해야합니다 :
public void subscribe() {
GcmPubSub pubSub = GcmPubSub.getInstance(this);
pubSub.subscribe(token, "/topics/foo-bar", null);
}
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
// Handle received message here.
}