gpt4 book ai didi

c# - 如何在 C# .Net 上通过 GCM 发送 Android 推送通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:33 24 4
gpt4 key购买 nike

我是所有 Android GCM 推送通知的新手,我已阅读堆栈帖子但无法得到直接答案。我还阅读了 Create push notification in android更好地了解 GCM 的工作原理。我也使用了SDK提供的gcm-demo-server和gcm-demo-client。然而,这是我的疑虑和我到目前为止所做的尝试:

  1. 关于我放置的链接,安装了该应用程序的手机注册以获取注册码。对于使用相同应用程序的所有手机来说,这是唯一的 key 吗?
  2. 这个注册码在任何情况下都会过期吗? (例如在后台运行的应用程序)
  3. 假设我有注册码,我已尝试使用以下代码 fragment 通过 GCM 将通知推送到我的应用程序。这是写在 c# .net 上的。请让我知道我上面提到的是否可以使用以下代码 fragment 来实现:

         private string SendGCMNotification(string apiKey, string postData, string postDataContentType = "application/json")
    {
    ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);

    // MESSAGE CONTENT
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);

    // CREATE REQUEST
    HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
    Request.Method = "POST";
    Request.KeepAlive = false;
    Request.ContentType = postDataContentType;
    Request.Headers.Add(string.Format("Authorization: key={0}", apiKey));
    Request.ContentLength = byteArray.Length;

    Stream dataStream = Request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();

    // SEND MESSAGE
    try
    {
    WebResponse Response = Request.GetResponse();
    HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
    if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
    {
    var text = "Unauthorized - need new token";
    }
    else if (!ResponseCode.Equals(HttpStatusCode.OK))
    {
    var text = "Response from web service isn't OK";
    }

    StreamReader Reader = new StreamReader(Response.GetResponseStream());
    string responseLine = Reader.ReadToEnd();
    Reader.Close();

    return responseLine;
    }
    catch (Exception e)
    {
    }
    return "error";
    }
  4. 有没有一种无需先在我们的自定义服务器中注册手机就可以直接发送推送通知的方法?

最佳答案

引用代码:

public class AndroidGCMPushNotification
{
public AndroidGCMPushNotification()
{
//
// TODO: Add constructor logic here
//
}
public string SendNotification(string deviceId, string message)
{
string SERVER_API_KEY = "server api key";
var SENDER_ID = "application number";
var value = message;
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}", SERVER_API_KEY));

tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

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=" + deviceId + "";
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();


tReader.Close();
dataStream.Close();
tResponse.Close();
return sResponseFromServer;
}
}

引用链接:

http://www.codeproject.com/Tips/434338/Android-GCM-Push-Notification

关于c# - 如何在 C# .Net 上通过 GCM 发送 Android 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16248468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com