Firebase云消息传递和C#服务器端代码

时间:2016-07-08 00:11:48

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

我在Android和iOS应用中使用FCM。客户端代码工作正常,因为从Firebase控制台我可以向两个平台发送通知而不会出现任何问题。使用我的C#代码,我可以成功向Android设备发送通知,但除非直接来自Firebase通知控制台,否则通知永远不会出现在iPhone上。我不知道是什么给了。

C#服务器端代码

"connected to %s:#, port

使用我的服务器端代码通知iPhone无效,但我从Firebase得到了很好的回复。

try
{
    var applicationID = "application_id";
    var senderId = "sender_id";
    string deviceId = "device_id_of_reciever";
    WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
    tRequest.Method = "post";
    tRequest.ContentType = "application/json";
    var data = new
    {
        to = deviceId,
        notification = new
        {
            body = "This is the message",
            title = "This is the title",
            icon = "myicon"
        }
    };

    var serializer = new JavaScriptSerializer();
    var json = serializer.Serialize(data);
    Byte[] byteArray = Encoding.UTF8.GetBytes(json);
    tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
    tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
    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())
            using (StreamReader tReader = new StreamReader(dataStreamResponse))
            {
                String sResponseFromServer = tReader.ReadToEnd();
                Response.Write(sResponseFromServer);
            }
        }
    }
}
catch (Exception ex)
{
    Response.Write(ex.Message);
}

任何帮助或建议都会非常感激。

2 个答案:

答案 0 :(得分:4)

尝试在FCM请求中将优先级字段设置为“高”。

例如:

Thread

请注意,虽然在开发中使用高优先级很好但在生产中它应该仅在用户需要采取行动时使用,例如回复聊天消息。

答案 1 :(得分:1)

我正在Android和IOS推送通知中使用FCM。我正在使用Visual Studio 2015.要创建一个Web api项目并添加一个控制器。编写代码。代码如下

using System;
using System.Net;
using System.Web.Http;
using System.Web.Script.Serialization;
using System.Configuration;
using System.IO;



namespace pushios.Controllers
{
    public class HomeController : ApiController
    {
        [HttpGet]
        [Route("sendmessage")]

        public IHttpActionResult SendMessage()
        {
            var data = new {
                to = "device Tokens", // iphone 6s test token
                data = new
                {
                    body = "test",
                    title = "test",
                    pushtype="events",



                },
                notification = new {
                        body = "test",
                        content_available = true,
                         priority= "high",
                        title = "C#"
               }



        } ;
            SendNotification(data);
          return Ok();
        }
        public void SendNotification(object data)
        {
            var Serializer = new JavaScriptSerializer();
            var json = Serializer.Serialize(data);
            Byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(json);

            SendNotification(byteArray);
        }
        public void SendNotification(Byte[] byteArray)
        {

            try
            {
                String server_api_key = ConfigurationManager.AppSettings["SERVER_API_KEY"];
                String senderid = ConfigurationManager.AppSettings["SENDER_ID"];

                WebRequest type = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                type.Method = "post";
                type.ContentType = "application/json";
                type.Headers.Add($"Authorization: key={server_api_key}");
                type.Headers.Add($"Sender: id={senderid}");

                type.ContentLength = byteArray.Length;
                Stream datastream = type.GetRequestStream();
                datastream.Write(byteArray, 0, byteArray.Length);
                datastream.Close();

                WebResponse respones = type.GetResponse();
                datastream = respones.GetResponseStream();
                StreamReader reader = new StreamReader(datastream);

                String sresponessrever = reader.ReadToEnd();
                reader.Close();
                datastream.Close();
                respones.Close();

            }
            catch (Exception)
            {
                throw;
            }

        }
    }
}

在下面给出android json的情况下

var data = new {
                    to = "device Tokens", // iphone 6s test token
                    data = new
                    {
                        body = "test",
                        title = "test",
                        pushtype="events",



                    };

对于IOS json

var data = new {
                    to = "device Tokens", // iphone 6s test token
                    data = new
                    {
                        body = "test",
                        title = "test",
                        pushtype="events",



                    },
                    notification = new {
                            body = "test",
                            content_available = true,
                             priority= "high",
                            title = "C#"
                   }



            } ;

SERVER_API_KEY,SENDER_ID我正在添加Web配置。要在FCM中收集SERVER_API_KEY,SENDER_ID。

<add key="SERVER_API_KEY" value="ADD Key in FCM"/>
    <add key="SENDER_ID" value="Add key in fcm "/>