使用ASP.NET C#

时间:2016-09-27 08:41:08

标签: c# asp.net

任何人都可以帮助我,如何向Firefox浏览器(桌面和移动设备)发送网络推送通知?

要向Chrome浏览器发送推送通知,我使用以下步骤。 1)在FCM中创建应用程序(获取发件人ID和api密钥) 2)使用google docs中显示的javascript方法获取端点(注册ID)。 3)使用asp.net c#向所有注册ID发送推送通知。 以下是代码

protected void SubmitButton1_Click(object sender, EventArgs e) 
{
  AndroidPush(); // calling android push method
}

//Android push message to GCM server method
private void AndroidPush() 
{
  // your RegistrationID paste here which is received from GCM server.
  string regId = "APA91bG_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_V6hO2liMx-
  eIGAbG2cR4DiIgm5Q ";
    // applicationID means google Api key
  var applicationID = "AIzaSyDScBxxxxxxxxxxxxxxxxxxxpv66IfgA";
  // SENDER_ID is nothing but your ProjectID (from API Console- google code)//
  var SENDER_ID = "77xxxxx625";
  var value = Text1.Text; //message text box                                                                        
  WebRequest tRequest;
  tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
  tRequest.Method = "post";
  tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
  tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
  tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
  //Data post to server                                                                                                         
  string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + regId + "";
  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.
  Label3.Text = sResponseFromServer; //Assigning GCM response to Label text 
  tReader.Close();
  dataStream.Close();
  tResponse.Close();
}

0 个答案:

没有答案