using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SendCloudMessageFirebase
{
public class Notification
{
public string body { get; set; }
public string title { get; set; }
public string click_action { get; set; }
public string android_channel_id { get; set; }
}
public class Data
{
public string body { get; set; }
public string title { get; set; }
public string android_channel_id { get; set; }
public string click_action { get; set; }
}
public class FirebaseRequestInfo
{
public Notification notification { get; set; }
public Data data { get; set; }
public string to { get; set; }
public string priority { get; set; }
public bool delay_while_idle { get; set; }
public bool content_available { get; set; }
public string android_channel_id { get; set; }
}
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SendCloudMessageFirebase
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSendmessge_Click(object sender, EventArgs e)
{
SendMessage(txtTitle.Text, txtBody.Text);
}
public static string SendMessage(string title, string body)
{
string serverKey = "xxxx";
try
{
string result;
var webAddr = "https://fcm.googleapis.com/fcm/send";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("Authorization", "Key=" + serverKey);
httpWebRequest.Method = "POST";
var firebaseRequestInfo = new FirebaseRequestInfo();
var notification = new Notification();
notification.body = body;
notification.title = title;
notification.click_action = "FLUTTER_NOTIFICATION_CLICK";
notification.android_channel_id = "high_importance_channel";
firebaseRequestInfo.notification = notification;
var data = new Data();
data.body = body;
data.title = title;
data.android_channel_id = "high_importance_channel";
data.click_action = "FLUTTER_NOTIFICATION_CLICK";
firebaseRequestInfo.data = data;
firebaseRequestInfo.to = "/topics/EMS";
firebaseRequestInfo.priority = "high";
firebaseRequestInfo.delay_while_idle = true;
firebaseRequestInfo.android_channel_id = "high_importance_channel";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var dataRequest = JsonConvert.SerializeObject(firebaseRequestInfo);
// string data = $@"
// {{
// ""notification"": {{
// ""body"": ""{body}"",
// ""title"": ""{title}"",
// ""click_action"": ""FLUTTER_NOTIFICATION_CLICK"",
// ""android_channel_id"": ""high_importance_channel""
// }},
//""data"": {{
// ""body"": ""{body}"",
// ""title"": ""{title}"",
// ""android_channel_id"": ""high_importance_channel"",
// ""click_action"": ""FLUTTER_NOTIFICATION_CLICK"",
// }},
// ""to"": ""/topics/EMS"",
// ""priority"" : ""high"",
// ""delay_while_idle"" : true,
// ""content_available"" : true,
// ""android_channel_id"": ""high_importance_channel"",
// }}
// ";
streamWriter.Write(dataRequest);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
return result;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return ex.ToString();
}
}
}
}