Thursday, 7 September 2017

Message Broadcast using google Api. in asp.net. (firebase Push Notification on Mobile)

   public void Test(string tokenid,string messagebody,string messgaetitle)
    {
        try
        {
//This applicationid and senderid comes from mobile developer. this comes from www.console.firebase.google.com
            var applicationID = "AAAAuSEmjuQ:APA91bFewTUFbEwbHOH7-W2Cvl5a6LXDMjHp7grlv2aBBzlCS0gwgRFir3a29hNdkmqMPTjVfk8FI_px973BPs_XfO6WjsLmEDKR2FgFjQUcF6Q75uX4oYvqIBgZAT-zX7rBqD9jVO2F";
            var senderId = "795125124836";

//This device id from Database when A app install automettically his device id has post ourdatabase.
            string deviceId = tokenid;
            WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = "application/json";
            var data = new
            {
                to = deviceId,
                notification = new
                {
                    body = messgaetitle,
                    title = messagebody,
                    icon = "myicon"
                },
                priority = "high"

            };

            var serializer = new JavaScriptSerializer();
            var json = serializer.Serialize(data);
            Byte[] byteArray = Encoding.UTF8.GetBytes(json);
            tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
            tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
            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();
                            Response.Write(sResponseFromServer);
                            txtdepartmentname.Text = sResponseFromServer;
                        }
                    }
                }
            }
        }

        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
=========================================
 protected void BtnSave_Click(object sender, EventArgs e)
    {
if (e.CommandName == "btnsend")
        {
            for (int i = 0; i < Griddepartment.Items.Count; i++)
            {
                int ID = Convert.ToInt32(((RadLabel)Griddepartment.Items[i].FindControl("lblattributeid")).Text);
                string msgbody = ((RadLabel)Griddepartment.Items[i].FindControl("lblattributename")).Text;
                string msgtitle = ((RadLabel)Griddepartment.Items[i].FindControl("lblcontrolname")).Text;
                RadCheckBox chksend = (RadCheckBox)Griddepartment.Items[i].FindControl("chksend");
                if (chksend.Checked == true)
                {
                    var tokens = GetallTokens().ToList();
                    foreach (var ss in tokens)
                    {
                        Test(ss.RegistartionToken_DeviceID, msgbody, msgtitle);
                        n1.Text = "Message Send Successfully";
                        n1.Show();
                    }
                }
            }
        }
}


No comments:

Post a Comment