如何撰写Firebase请求,以便我可以在单个请求中向移动设备广播100通知?

时间:2017-02-14 05:15:37

标签: c# android firebase notifications firebase-cloud-messaging

我想在单个FCM请求中向多个设备发送通知。我的通知文本对所有设备都相同。

我想按照每个请求批量向移动设备发送通知。我正在使用c#asmx服务。

以下是我的代码。

            string regid="c_Z5yRoj4TY:APA91bGry2g_CIA1xaRy_LscxOvFX6YHqasKA96TjpG6yi1yytNyM5rtGL6DgxjGMSE5c74d7VdSL6W8zxO1ixVMlpVMwdgcrsGUWV0VfdbddC2XD","c_Z5yRoj4TY:APA91bGry2g_CIA1xaRy_LscxOvFX6YHqasKA96TjpG6yi1yytNyM5rtGL6DgxjGMSE5c74d7";

        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://fcm.googleapis.com/fcm/send");

        httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";

        httpWebRequest.Method = "POST";

        String collaps_key = "Score_update";

        string json = "collapse_key=abcd" + "&data.header=cricket&registration_id=" + regId + "&data.notificationId=" + notificationId + "&data.message=" + msg;

        httpWebRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
        httpWebRequest.Headers.Add(string.Format("Sender: key={0}", SENDER_ID));

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            //Console.WriteLine(json);
            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
            using (HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse())
            {
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    Console.WriteLine(result);
                    retmsgid = result.ToString();
                    if (retmsgid.Trim() != "")
                    {
                        ResponceString = result.ToString();
                        string[] msgsplits = retmsgid.Split(',');
                        string[] msg1 = msgsplits[0].ToString().Split(':');
                        ReturnMessageId = msg1[1].ToString();
                    }
                    else
                    {
                        ReturnMessageId = "0";
                    }
                }
                httpResponse.Close();
                httpResponse.Dispose();
                httpWebRequest = null;
            }
        } 

1 个答案:

答案 0 :(得分:1)

要发送到特定的多个注册设备,您必须使用registration_ids参数:

  

此参数指定多播消息的收件人,即发送到多个注册令牌的消息。

     

该值应该是要向其发送多播消息的注册令牌数组。该数组必须包含至少1个且最多1000个注册令牌。要将消息发送到单个设备,请使用to参数。

在您的代码中,您使用的是我认为无效的registration_id。它应该是registration_ids

string json = "collapse_key=abcd" + "&data.header=cricket&registration_ids=" + regIds + "&data.notificationId=" + notificationId + "&data.message=" + msg;

- regIds这是一个包含注册令牌的字符串数组。

之前的一些帖子也可能有所帮助(请参阅OP代码段):