2016-05-02 15 views
1

私はasp.net c#を使用してGCMを通じてユーザーにプッシュ通知を送信しています。これは通知を送信する私のコードです。GCMを通じて1000人以上のユーザーにプッシュ通知を送信する方法

string RegArr = string.Empty; 
RegArr = string.Join("\",\"", RegistrationID);  // (RegistrationID) is Array of endpoints 

string message = "some test message"; 
string tickerText = "example test GCM"; 
string contentTitle = "content title GCM"; 
postData = 
"{ \"registration_ids\": [ \"" + RegArr + "\" ], " + 
"\"data\": {\"tickerText\":\"" + tickerText + "\", " + 
"\"contentTitle\":\"" + contentTitle + "\", " + 
"\"message\": \"" + message + "\"}}"; 

string response = SendGCMNotification("Api key", postData); 

SendGCMNotification機能: - 今、私はもっとして1000人のユーザを持っていると私はそれらすべてに通知を送信する必要がありますが、中に

private string SendGCMNotification(string apiKey, string postData, string postDataContentType = "application/json") 
    { 

     byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

     // CREATE REQUEST 
     HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send"); 
     Request.Method = "POST"; 
     Request.KeepAlive = false; 
     Request.ContentType = postDataContentType; 
     Request.Headers.Add(string.Format("Authorization: key={0}", apiKey)); 
     Request.ContentLength = byteArray.Length; 

     Stream dataStream = Request.GetRequestStream(); 
     dataStream.Write(byteArray, 0, byteArray.Length); 
     dataStream.Close(); 
     // 
     // SEND MESSAGE 
     try 
     { 
      WebResponse Response = Request.GetResponse(); 
      HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode; 
      if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden)) 
      { 
       var text = "Unauthorized - need new token"; 
      } 
      else if (!ResponseCode.Equals(HttpStatusCode.OK)) 
      { 
       var text = "Response from web service isn't OK"; 
      } 
      StreamReader Reader = new StreamReader(Response.GetResponseStream()); 
      string responseLine = Reader.ReadToEnd(); 
      Reader.Close(); 

      return responseLine; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     return "error"; 
    } 

それは正常に動作しているとの通知は、1000年users.Butに適切に配信します我々は1000の登録IDを渡すことができ、指定されたGCMのドキュメントが1 time.whatで、私はすべてのユーザーに通知を送信するために行うことができますか?すべてのヘルプは

enter image description here

にappriciatedされます
+1

を呼び出すことができますか? –

+0

@VolodymyrBilyachatあなたは配列を分割し、ループを使ってこれを行うと言っていますか? –

+2

正確です。もしAPIの限界だとすれば、何もできないと思いますので、ループでそれをやります。 –

答えて

1

コンテンツタイプを設定していないため、おそらく例外が発生します。私はarticle how to send dataを見つけ、あなたの必要に応じて書き換えました。あなたはまた、

public class NotificationManager 
    { 
     private readonly string AppId; 
     private readonly string SenderId; 

     public NotificationManager(string appId, string senderId) 
     { 
      AppId = appId; 
      SenderId = senderId; 
      // 
      // TODO: Add constructor logic here 
      // 
     } 

     public void SendNotification(List<string> deviceRegIds, string tickerText, string contentTitle, string message) 
     { 
      var skip = 0; 
      const int batchSize = 1000; 
      while (skip < deviceRegIds.Count) 
      { 
       try 
       { 
        var regIds = deviceRegIds.Skip(skip).Take(batchSize); 

        skip += batchSize; 

        WebRequest wRequest; 
        wRequest = WebRequest.Create("https://android.googleapis.com/gcm/send"); 
        wRequest.Method = "POST"; 
        wRequest.ContentType = " application/json;charset=UTF-8"; 
        wRequest.Headers.Add(string.Format("Authorization: key={0}", AppId)); 
        wRequest.Headers.Add(string.Format("Sender: id={0}", SenderId)); 

        var postData = JsonConvert.SerializeObject(new 
        { 
         collapse_key = "score_update", 
         time_to_live = 108, 
         delay_while_idle = true, 
         data = new 
         { 
          tickerText, 
          contentTitle, 
          message 
         }, 
         registration_ids = regIds 
        }); 

        var bytes = Encoding.UTF8.GetBytes(postData); 
        wRequest.ContentLength = bytes.Length; 

        var stream = wRequest.GetRequestStream(); 
        stream.Write(bytes, 0, bytes.Length); 
        stream.Close(); 

        var wResponse = wRequest.GetResponse(); 

        stream = wResponse.GetResponseStream(); 

        var reader = new StreamReader(stream); 

        var response = reader.ReadToEnd(); 

        var httpResponse = (HttpWebResponse) wResponse; 
        var status = httpResponse.StatusCode.ToString(); 

        reader.Close(); 
        stream.Close(); 
        wResponse.Close(); 

        //TODO check status 
       } 
       catch(Exception ex) 
       { 
        Console.WriteLine(ex.Message); 
       } 
      } 
     } 

Json.Netパッケージをインストールする必要があります次に、あなたは、なぜ2つの要求を分割して行うにはない方法

var notificationManager = new NotificationManager("AppId", "SenderId"); 
notificationManager.SendNotification(Enumerable.Repeat("test", 1010).ToList(), "tickerText", "contentTitle", "message"); 
+0

のコード例を教えてください。私はすでにcontent-type "Request.ContentType = application/json;"を渡しています。私の問題はループを使って解決されます。私はこのソリューションを試してみましたが、正しく動作しています。 –

関連する問題