【问题标题】:Firebase Cloud Messaging and C# server side codeFirebase 云消息传递和 C# 服务器端代码
【发布时间】:2016-07-08 00:11:48
【问题描述】:

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

C# 服务器端代码

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

使用我的服务器端代码在 iPhone 上无法使用通知,但我从 Firebase 获得了良好的响应。

    {
    "multicast_id": 479608 XXXXXX529964,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [{
        "message_id": "0:1467935842135743%a13567c6a13567c6"
    }]
}

任何帮助或建议将不胜感激。

【问题讨论】:

  • 当消息直接来自 firebase 时,它​​们看起来会有所不同。 gcm.message_id: 0:1467938951987907%a13567c6XXXX67c6] %@ [aps: { alert = { body = "这是消息"; title = "这是标题"; }; }, gcm.message_id: 0:14679XXXX87907%a13567c6a1XXXXc6] 收到消息 消息 ID: 0:1467939XXXXX5%a13567c6a13567c6 %@ [google.c.a.c_l: why2, google.c.a.e: 1, aps: { alert = why1; }, gcm.n.e: 1, google.c.a.c_id: 50607XXXXXX957125, google.c.a.udt: 0, gcm.message_id: 0:14679XXXX18215%a13567c6a1XXX7c6, google.c.a.ts: 146XXXXXX32]
  • 嗨,你能告诉我你是如何发送 deviceId 的吗?
  • @Paul_D 嘿,我知道你的问题已经过时了,但我怎样才能获得 deviceId?您是使用端点发送的还是如何发送的?感谢您的帮助

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


【解决方案1】:

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

例如:

var data = new
{
    to = deviceId,
    notification = new
    {
        body = "This is the message",
        title = "This is the title",
        icon = "myicon"
    },
    priority = "high"
};

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

【讨论】:

  • 我将答案标记为正确。在我的情况下,我必须将优先级设置为 10(优先级 = 10),然后一切都很好。提供的答案非常有帮助。谢谢亚瑟
【解决方案2】:

我在 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 我正在添加网络配置。要在 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 "/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2016-11-05
    • 2022-06-11
    • 2014-01-05
    • 2023-03-20
    相关资源
    最近更新 更多