<bdo id='nlCBv'></bdo><ul id='nlCBv'></ul>
  • <small id='nlCBv'></small><noframes id='nlCBv'>

    <legend id='nlCBv'><style id='nlCBv'><dir id='nlCBv'><q id='nlCBv'></q></dir></style></legend>
  • <i id='nlCBv'><tr id='nlCBv'><dt id='nlCBv'><q id='nlCBv'><span id='nlCBv'><b id='nlCBv'><form id='nlCBv'><ins id='nlCBv'></ins><ul id='nlCBv'></ul><sub id='nlCBv'></sub></form><legend id='nlCBv'></legend><bdo id='nlCBv'><pre id='nlCBv'><center id='nlCBv'></center></pre></bdo></b><th id='nlCBv'></th></span></q></dt></tr></i><div id='nlCBv'><tfoot id='nlCBv'></tfoot><dl id='nlCBv'><fieldset id='nlCBv'></fieldset></dl></div>
    <tfoot id='nlCBv'></tfoot>

        使用 FCM(Firebase 云消息传递)通过 C# 向 Android 发送推送

        时间:2023-10-24
        • <bdo id='DK4q3'></bdo><ul id='DK4q3'></ul>

          <legend id='DK4q3'><style id='DK4q3'><dir id='DK4q3'><q id='DK4q3'></q></dir></style></legend>
            <tbody id='DK4q3'></tbody>
              • <small id='DK4q3'></small><noframes id='DK4q3'>

                <tfoot id='DK4q3'></tfoot>

                  <i id='DK4q3'><tr id='DK4q3'><dt id='DK4q3'><q id='DK4q3'><span id='DK4q3'><b id='DK4q3'><form id='DK4q3'><ins id='DK4q3'></ins><ul id='DK4q3'></ul><sub id='DK4q3'></sub></form><legend id='DK4q3'></legend><bdo id='DK4q3'><pre id='DK4q3'><center id='DK4q3'></center></pre></bdo></b><th id='DK4q3'></th></span></q></dt></tr></i><div id='DK4q3'><tfoot id='DK4q3'></tfoot><dl id='DK4q3'><fieldset id='DK4q3'></fieldset></dl></div>

                  本文介绍了使用 FCM(Firebase 云消息传递)通过 C# 向 Android 发送推送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用此代码通过 C# 和 GCM 发送通知消息,使用 Winforms、Webforms 等.现在我想发送到 FCM(Firebase 云消息传递).我应该更新我的代码吗?:

                  I am using this code to send notification message by C# with GCM, using Winforms, Webforms, whatever. Now I want to send to FCM (Firebase Cloud Messaging). Should I update my code? :

                  public class AndroidGCMPushNotification
                  {
                      public AndroidGCMPushNotification()
                      {
                          //
                          // TODO: Add constructor logic here
                          //
                      }
                      public string SendNotification(string deviceId, string message)
                      {
                          string SERVER_API_KEY = "server api key";        
                          var SENDER_ID = "application number";
                          var value = message;
                          WebRequest tRequest;
                          tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
                          tRequest.Method = "post";
                          tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
                          tRequest.Headers.Add(string.Format("Authorization: key={0}", SERVER_API_KEY));
                  
                          tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
                  
                          string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + deviceId + "";
                          Console.WriteLine(postData);
                          Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                          tRequest.ContentLength = byteArray.Length;
                  
                          Stream dataStream = tRequest.GetRequestStream();
                          dataStream.Write(byteArray, 0, byteArray.Length);
                          dataStream.Close();
                  
                          WebResponse tResponse = tRequest.GetResponse();
                  
                          dataStream = tResponse.GetResponseStream();
                  
                          StreamReader tReader = new StreamReader(dataStream);
                  
                          String sResponseFromServer = tReader.ReadToEnd();
                  
                  
                          tReader.Close();
                          dataStream.Close();
                          tResponse.Close();
                          return sResponseFromServer;
                      }
                  }
                  

                  但 GCM 已更改为 FCM.发送通知的代码是否相同?我在哪里可以找到 SERVER_API_KEY?是一样的解决方案吗?

                  but the GCM was changed to FCM. Is this same code to send the notification? Where can I find the SERVER_API_KEY? Is the same solution?

                  推荐答案

                  使用 c# 的 firebase 云消息传递:适用于所有 .net 平台(asp.net、.netmvc、.netcore)

                  firebase cloud messaging with c#: working all .net platform (asp.net, .netmvc, .netcore)

                          WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                          tRequest.Method = "post";
                          //serverKey - Key from Firebase cloud messaging server  
                          tRequest.Headers.Add(string.Format("Authorization: key={0}", "AIXXXXXX...."));
                          //Sender Id - From firebase project setting  
                          tRequest.Headers.Add(string.Format("Sender: id={0}", "XXXXX.."));
                          tRequest.ContentType = "application/json";
                          var payload = new
                          {
                              to = "e8EHtMwqsZY:APA91bFUktufXdsDLdXXXXXX..........XXXXXXXXXXXXXX",
                              priority = "high",
                              content_available = true,
                              notification = new
                              {
                                  body = "Test",
                                  title = "Test",
                                  badge = 1
                              },
                              data = new 
                              {
                                  key1 = "value1",
                                  key2 = "value2"
                              }
                  
                          };
                  
                          string postbody = JsonConvert.SerializeObject(payload).ToString();
                          Byte[] byteArray = Encoding.UTF8.GetBytes(postbody);
                          tRequest.ContentLength = byteArray.Length;
                          using (Stream dataStream = tRequest.GetRequestStream())
                          {
                              dataStream.Write(byteArray, 0, byteArray.Length);
                              using (WebResponse tResponse = tRequest.GetResponse())
                              {
                                  using (Stream dataStreamResponse = tResponse.GetResponseStream())
                                  {
                                      if (dataStreamResponse != null) using (StreamReader tReader = new StreamReader(dataStreamResponse))
                                          {
                                              String sResponseFromServer = tReader.ReadToEnd();
                                              //result.Response = sResponseFromServer;
                                          }
                                  }
                              }
                          }
                  

                  这篇关于使用 FCM(Firebase 云消息传递)通过 C# 向 Android 发送推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:FCM(Firebase 云消息传递)发送到多个设备 下一篇:ApiHubFile Azure 函数绑定的动态输出文件名(一个驱动器,投递箱等)

                  相关文章

                1. <i id='gaYId'><tr id='gaYId'><dt id='gaYId'><q id='gaYId'><span id='gaYId'><b id='gaYId'><form id='gaYId'><ins id='gaYId'></ins><ul id='gaYId'></ul><sub id='gaYId'></sub></form><legend id='gaYId'></legend><bdo id='gaYId'><pre id='gaYId'><center id='gaYId'></center></pre></bdo></b><th id='gaYId'></th></span></q></dt></tr></i><div id='gaYId'><tfoot id='gaYId'></tfoot><dl id='gaYId'><fieldset id='gaYId'></fieldset></dl></div>

                  1. <small id='gaYId'></small><noframes id='gaYId'>

                    1. <tfoot id='gaYId'></tfoot>
                    2. <legend id='gaYId'><style id='gaYId'><dir id='gaYId'><q id='gaYId'></q></dir></style></legend>
                      • <bdo id='gaYId'></bdo><ul id='gaYId'></ul>