[英]How to send notification from C# Console application
I want to create console application which is used to send notifications to different mobile devices via Goolge Firebase Notifications,我想创建控制台应用程序,用于通过 Goolge Firebase Notifications 向不同的移动设备发送通知,
I have seen the code from link Send push to Android by C# using FCM (Firebase Cloud Messaging)我已经看到使用 FCM (Firebase Cloud Messaging) 通过 C# 发送推送到 Android链接中的代码
I am getting internal server error with status code 500我收到状态码为 500 的内部服务器错误
try{
string url = @"https://fcm.googleapis.com/fcm/send";
WebRequest tRequest = WebRequest.Create(url);
tRequest.Method = "post";
tRequest.ContentType = "application/json";
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + "This is the message" + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + deviceId + "";
var data = new
{
to = deviceId,
notification = new
{
body = "This is the message",
title = "This is the title"
}
};
string jsonss = Newtonsoft.Json.JsonConvert.SerializeObject(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(jsonss);
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
tRequest.ContentLength = byteArray.Length;
tRequest.ContentType = "application/json";
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();
Console.Write(sResponseFromServer);
}
}
}
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
{
var sss = ex.Message;
if (ex.InnerException != null)
{
var ss = ex.InnerException;
}
}
}
Here is the code to send notification using C#, I have made it working这是使用 C# 发送通知的代码,我已经使它工作了
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var objNotification = new
{
to = notification.DeviceToken,
data = new
{
title = notification.NotificationTitle,
body = notification.NotificationBody
}
};
string jsonNotificationFormat = Newtonsoft.Json.JsonConvert.SerializeObject(objNotification);
Byte[] byteArray = Encoding.UTF8.GetBytes(jsonNotificationFormat);
tRequest.Headers.Add(string.Format("Authorization: key={0}", serverKey));
tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
tRequest.ContentLength = byteArray.Length;
tRequest.ContentType = "application/json";
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 responseFromFirebaseServer = tReader.ReadToEnd();
FCMResponse response = Newtonsoft.Json.JsonConvert.DeserializeObject<FCMResponse>(responseFromFirebaseServer);
if (response.success == 1)
{
new NotificationBLL().InsertNotificationLog(dayNumber, notification, true);
}
else if (response.failure == 1)
{
new NotificationBLL().InsertNotificationLog(dayNumber, notification, false);
sbLogger.AppendLine(string.Format("Error sent from FCM server, after sending request : {0} , for following device info: {1}", responseFromFirebaseServer, jsonNotificationFormat));
}
}
}
}
}
Here is class FCMResponse used in this code to store response sent from FMServer这是此代码中用于存储从 FMServer 发送的响应的 FCMResponse 类
public class FCMResponse
{
public long multicast_id { get; set; }
public int success { get; set; }
public int failure { get; set; }
public int canonical_ids { get; set; }
public List<FCMResult> results { get; set; }
}
public class FCMResult
{
public string message_id { get; set; }
}
Yes, Duston is right, I am using wrong value in variable ' applicationID '是的,达斯顿是对的,我在变量“ applicationID ”中使用了错误的值
Actually I am using mobilesdk_app_id value instead of api_key current key thats why it is giving 500 error实际上我使用的是 mobilesdk_app_id 值而不是 api_key 当前密钥,这就是它给出 500 错误的原因
Now it is working fine :)现在它工作正常:)
Thanks Json, Francis Lord for your efforts :)感谢 Json,Francis Lord 的努力 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.