1-) C# RMOS - Firebase FCM(bildirim mesaj) gönderme
1. BildirimReq.cs
public class BildirimReq { public string to { get; set; } public Notification notification { get; set; } public class Notification { public string body { get; set; } public string title { get; set; } public bool content_available { get; set; } public string priority { get; set; } } } |
2. KULLANIMI
private async void btnBildirimGonder_Click(object sender, EventArgs e) { if (txtBildirimAciklama.Text=="" || txtBildirimBaslik.Text=="") { MessageBox.Show("Tüm alanları giriniz..."); return; }
await bildirimGonderAsync(txtBildirimAciklama.Text,txtBildirimBaslik.Text);
MessageBox.Show("Gönderildi"); }
public async Task bildirimGonderAsync(string body,string title) { try { BildirimReq req = new BildirimReq(); req.to = "/topics/all";
BildirimReq.Notification not= new BildirimReq.Notification(); not.body =body; not.title =title; not.content_available = true; not.priority = "high";
req.notification=not;
string jsonText = JsonConvert.SerializeObject(req);
var client = new HttpClient(); var request = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/fcm/send"); client.DefaultRequestHeaders.Authorization= new AuthenticationHeaderValue("Bearer", "AAAATUKDWHI:APA91bEs4jy197Zy39aaaaaaaaancR44YcwxPInuMaaaaaaauUhGpqZ2nGGIMbbCMR9c8V0ZZGlC4icXalkqN3uv7X6iHnJZCbfErxSCarYwabtaj9o3i"); var content = new StringContent(jsonText, Encoding.UTF8, "application/json"); request.Content = content; var response = await client.SendAsync(request); response.EnsureSuccessStatusCode(); Console.WriteLine(await response.Content.ReadAsStringAsync()); } catch (Exception ex) { MessageBox.Show(ex.Message); }
} |
3. DİĞER
var webAddr = "https://fcm.googleapis.com/fcm/send"; var regID = "PushToken"; string sunucuAnahtari ="vs.."; string icerik ="mesaj içeriği"; var result = "-1"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr); httpWebRequest.ContentType = "application/json"; httpWebRequest.Headers.Add("Authorization:key=" + sunucuAnahtari); httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { string json = "{\"to\": \"" + regID + "\"," + "\"notification\": " + "{" + "\"title\": \"" + otelAdi + "\"," + "\"sound\": \"noti.mp3\"," + "\"body\": \"" + icerik + "\"" + "}," + "\"priority\":10}"; //registration_ids, array of strings - to, single recipient streamWriter.Write(json); streamWriter.Flush(); }
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) {
result = streamReader.ReadToEnd(); }
ÖRNEK POSTMAN VERİSİ POST https://fcm.googleapis.com/fcm/send header key : Authorization
body-> raw -> json "to": "fB1kkyVnQDyicSASLHL2Ar:AP<<<<AAAi5kB6ustu6kCT6ialpQ50_WuDojEHYo433PjwEmaaaaaaz4Bc0qw0bDkBJW-0D90-N9wg-uqR5W2YH8wK4fArEqqudAOrJhHfT-bvbSASP-aaaaaaUDDPTDiq",
"notification": {
"body": "Hello", "title": "This is test message." }
|