3

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

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

提前感谢。

4

2 回答 2

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.
}
于 2015-09-15T15:26:44.003 回答
0

您别无选择,只能将广播分成最多 1000 个 regId 的块。

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

        //regIdList max size is 1000
        MulticastResult multicastResult;
        try {
            multicastResult = sender.send(message, regIdList, retryTimes);
        } catch (IOException e) {
            logger.error("Error posting messages", e);
            return;
        }
于 2012-10-11T15:47:38.350 回答