使用Google Cloud Messaging发送广播通知

时间:2012-07-13 10:13:22

标签: notifications cloud messaging broadcast

我正在使用Google Cloud Messaging提供推送通知。我可能需要向大约10,000个用户发送广播通知。但是,我读到多播消息可以包含一个包含1000个注册ID的列表,最大化。

那么,我需要发送十条多播消息吗?有没有办法向所有客户发送广播而不生成包含所有ID的列表?

先谢谢。

2 个答案:

答案 0 :(得分:1)

从Play Services 7.5开始,现在也可以通过以下主题实现这一目标:

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());

您的客户应订阅/ topics / foo-bar:

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.
}

答案 1 :(得分:0)

你别无选择,只能将你的广播分成最多1000个分区。

然后,您可以在单独的线程中发送多播消息。

        //regIdList max size is 1000
        MulticastResult multicastResult;
        try {
            multicastResult = sender.send(message, regIdList, retryTimes);
        } catch (IOException e) {
            logger.error("Error posting messages", e);
            return;
        }