我正在使用谷歌云消息提供推送通知。我可能需要向大约10000名用户发送广播通知。然而,我读到一条多播消息可以包含一个最多有1000个注册ID的列表。
那么,我需要发送十条多播消息吗?有没有任何方法可以在不生成带有所有id的列表的情况下向所有客户端发送广播?
提前谢谢。
自从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());
你的客户应该订阅/ttopic/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.
}
您别无选择,只能将广播拆分为最多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;
}