Gagri Global IT services is having a team of executives who have good experience in developing applications on various platforms like SharePoint 2013/2010, Silverlight, net Framework 4.5 and Mobile tools.
Posted on December 15, 2023 by Amruthavalli,Deeksha
public string SendFCMNotification(string DeviceId, string Text, string SenderName, string path,string Img)
{
try
{
// Server key
var applicationID = "AAAAL5ULBwQ:APA91bGqwKGCWGgDehUEFxahofGCoZBlYAqAWX6dtnd3Ql2vypwU-Idn7rID087xX8HWlA4ETfnuSklCZvF_1lIa_xpR2HLQ-EC0YGS2ZPOP6JQLy58CYkAoanIPzVakjNaGr2VUAGy0";
// Sender Id
var senderId = "204363990788";
string deviceId = DeviceId;
var request = (HttpWebRequest)WebRequest.Create("https://fcm.googleapis.com/fcm/send");
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add(string.Format("Authorization: key={0}", applicationID));
request.Headers.Add(string.Format("Sender: id={0}", senderId));
var message = new
{
to = deviceId,
notification = new
{
body = Text,
title = SenderName,
//image = "https://www.kasandbox.org/programming-images/avatars/spunky-sam.png" image = Img
},
data = new
{
sound = "null"
channel_id = "null"
clickAction = path
}
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(message);
var byteArray = Encoding.UTF8.GetBytes(json);
request.ContentLength = byteArray.Length;
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var dataStreamResponse = response.GetResponseStream())
{
using (var reader = new StreamReader(dataStreamResponse))
{
string sResponseFromServer = reader.ReadToEnd();
return sResponseFromServer;
}
}
}
}
catch (Exception ex)
{
exp.ExceptionHandler(ex);
return "";
}
}
FrontEnd:
submit(value: any)
{
var obj = [{
BlogTitle: value.BlogTitle,
BlogImage: this.Photo,
ProductsInfo: value.ProductsInfo,
Benefits: value.Benefits,
Headers: value.Headers,
Discription: value.Discription
}]
this.Blogservice.AddBlog(obj).subscribe((data: any) => {
if (data === 'EXISTS')
{
this.general.presentAlert("Alert", "Your mobile number is already exists.")
// Handle the "EXISTS" response here
}
else if (data === 'SUCCESS')
{
var path = "blogdetails";
const massege = value.Discription+ ".";
var UploadFile = new FormData();
//const imageFile = new File([""], "ceo.webp", { type: "image/webp" });
UploadFile.append("message", massege);
UploadFile.append("senderName", value.BlogTitle);
UploadFile.append("Img", this.HomeURL+this.Photo);
UploadFile.append("Path", path);
var url = "api/Farmer/sendNotificationtoallFarmers";
this.general.PostData(url, UploadFile).subscribe((data: any) => {
debugger
});
this.NavCtrl.navigateForward('/blogdetails');
this.general.presentAlert("SUCCESS", "Your blog has been added successfully!.")
// Handle the "SUCCESS" response here
}
//this['NavCtrl'].navigateForward('/add-blog');
},
(err: any) => {
this.general.presentToast('Something went wrong. Please try again later.')
});
}
All design and content Copyright © 2012-2018 Gagri Global IT Services Pvt.Ltd. All rights reserved