2012-07-13 104 views
2

我正在使用Google Cloud Messaging提供推送通知。我可能需要向大約10,000個用戶發送廣播通知。但是,我讀到Multicast消息可以包含一個包含1000個註冊ID的列表maximun。使用Google Cloud Messaging發送廣播通知

那麼,我需要發送十個多播消息嗎?有沒有辦法將廣播發送給所有的客戶端,而不用所有的id生成列表?

感謝advace。

回答

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; 
     } 
1

由於播放服務7.5,它現在也可以通過主題來實現這一目標:

https://developers.google.com/cloud-messaging/topic-messaging

註冊之後,你就必須發送GCM服務器的消息通過HTTP:

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