Create default.aspx page
|
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class OneMessagetoMultipleDevices : System.Web.UI.Page
{
List<Noti_Tabs> Regids = new List<Noti_Tabs>();
string[] stringregIds;
protected void Page_Load(object sender, EventArgs e)
{
lbl1.Text = "";
lbl_regIds.Text = "";
}
protected void btn_Send_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Database Connection
from Sql Server");
SqlCommand cmd = new SqlCommand(" select RegIds from
Table", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Regids.Add(new Noti_Tabs { RegId =
ds.Tables[0].Rows[i]["RegIds"].ToString() });
}
foreach (Noti_Tabs reg in Regids)
{
lbl_regIds.Text += reg.RegId + "\",\"";
}
lbl_regIds.Text =
lbl_regIds.Text.TrimEnd('"', ',', '"');
var applicationID = "AIzaS--------------------------uH0itc";
// applicationID means google Api key
// applicationID means google Api key
var SENDER_ID = "123-----006";
// SENDER_ID is nothing but your ProjectID (from API Console- google code)
// SENDER_ID is nothing but your ProjectID (from API Console- google code)
var value = txtBox_Msg.Text.Replace("\t",string.Empty) ;
// Text box message
// Text box message
WebRequest tRequest;
tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
tRequest.Method = "post";
tRequest.ContentType = " application/json";
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
tRequest.Headers.Add(string.Format("Sender: id={0}",
SENDER_ID));
string postData = "{\"collapse_key\":\"score_update\",\"time_to_live\":108,\"delay_while_idle\":true,\"data\":
{ \"message\" : " + "\"" + value
+ "\",\"time\": " + "\"" + System.DateTime.Now.ToString() + "\"},\"registration_ids\":[\"" + lbl_regIds.Text + "\"]}";
Console.WriteLine(postData);
Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength =
byteArray.Length;
Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray, 0,
byteArray.Length);
dataStream.Close();
WebResponse tResponse = tRequest.GetResponse();
dataStream = tResponse.GetResponseStream();
StreamReader tReader = new StreamReader(dataStream);
String sResponseFromServer = tReader.ReadToEnd(); //Get
response from GCM server.
lbl_Response.Text =
sResponseFromServer; //Assigning GCM response to Label text
tReader.Close();
dataStream.Close();
tResponse.Close();
}
}
}
Add
Class
public class Noti_Tabs
{
public string RegId { get; set; }
}
No comments:
Post a Comment