[英]How to send FCM notification with an image to all devices using C#?
有没有办法使用C#向所有设备广播FCM通知 - 加上图像 ?
我想要包含图片并通过Firebase Notification服务发送到所有设备,而不是发送到一个特定的设备ID。
我使用此代码将数据发送到单个用户设备,但没有图像:
public string SendNotificationInstaTips(string firebaseID,
string notTitle
string notText,
string notContent)
{
try
{
string SERVER_API_KEY = "AIza..QXq5OQCaM";
string SENDER_ID = "162..09";
string REGISTERATION_ID = firebaseID;
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new
{
to = REGISTERATION_ID,
data = new
{
title = notTitle,
text = notText
content = notContent
}
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
tRequest.Headers.Add(string.Format("Authorization: key={0}", SERVER_API_KEY));
tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
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();
return sResponseFromServer;
}
}
}
}
}
catch (Exception ex)
{
return ex.Message;
}
}
我一直在用PHP工作,但它应该在C#中类似。 在代码中使用“注册ID”而不是“to”
var data = new
{
to = REGISTERATION_ID, //Change this line
data = new
{
title = notTitle,
text = notText
content = notContent
}
};
var data = new
{
registration_ids = {"AIza..QXq5OQCaM","An2i..QXq5OQCaM", .....},
data = new
{
title = notTitle,
text = notText
content = notContent
}
};
希望这可以帮助
这不是Push Notifications的常用用例。 对于FCM,强烈建议不要发送图像,主要是因为有效载荷大小限制 - notification
为2KB, data
为4KB。
我建议使用Firebase存储来存储图像,然后在需要时将其下载到设备中,只需在推送通知中发送下载URL作为解决方法。
有关如何发送到多个设备的问题,请在此处查看我的答案 。 我建议你的用例使用Topic Messaging 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.