从C#服务器向android发送FCM数据有效负载通知?
我尝试使用FCM从C#服务器向Android设备发送通知。我使用下面提到的代码发送通知,它工作得非常好,但我也需要发送数据负载,但我不知道如何实现从C#服务器向android发送FCM数据有效负载通知?,c#,android,asp.net,firebase-cloud-messaging,C#,Android,Asp.net,Firebase Cloud Messaging,我尝试使用FCM从C#服务器向Android设备发送通知。我使用下面提到的代码发送通知,它工作得非常好,但我也需要发送数据负载,但我不知道如何实现 public static void SendPushNotification(String user_id,string not_title) { try { string applicationID = "AAAAjpeM.......";
public static void SendPushNotification(String user_id,string not_title)
{
try
{
string applicationID = "AAAAjpeM.......";
string senderId = ".......";
string deviceId = user_id;
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 = not_title,
title = "ABC",
sound = "Enabled"
}
};
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();
string str = sResponseFromServer;
}
}
}
}
}
catch (Exception ex)
{
string str = ex.Message;
}
}
我试过了,但不管用
var data = new
{
to = deviceId,
data = new
{
body = not_title,
title = "Avicenna",
payload="1",
sound = "Enabled"
}
};
试试这个:
var data = new
{
to = deviceId,
notification = new
{
body = not_title,
title = "ABC",
sound = "Enabled"
},
data = new
{
payload="1"
}
};
试试这个:
var data = new
{
to = deviceId,
notification = new
{
body = not_title,
title = "ABC",
sound = "Enabled"
},
data = new
{
payload="1"
}
};
您是否考虑到,当Android设备接收到纯通知消息和数据有效负载消息时,它们的处理方式不同。还要注意,Android和iOS处理推送通知的方式不同。请查看FCM文档。您是否考虑到Android设备接收纯通知消息和数据有效负载消息时的处理方式不同。还要注意,Android和iOS处理推送通知的方式不同。请检查FCM文档。