我已经通过 GCM 使用 ASP.net C# 向 Android 手机发送了推送通知。
但是我尝试了不同类型的代码,但所有 arr 都返回 Missing Registration 错误,所以请帮助我。
我已经通过 GCM 使用 ASP.net C# 向 Android 手机发送了推送通知。
但是我尝试了不同类型的代码,但所有 arr 都返回 Missing Registration 错误,所以请帮助我。
尝试PushSharp。这很简单。并查看此说明 -如何使用 PushSharp 配置和发送 GCM Google 云消息推送通知
var push = new PushBroker();
//Registering the GCM Service and sending an Android Notification
push.RegisterGcmService(new GcmPushChannelSettings("theauthorizationtokenhere"));
//Fluent construction of an Android GCM Notification
//IMPORTANT: For Android you MUST use your own RegistrationId here that gets generated within your Android app itself!
push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("DEVICE REGISTRATION ID HERE")
.WithJson("{\"alert\":\"Hello World!\",\"badge\":7,\"sound\":\"sound.caf\"}"));
您可以通过nuget安装它。
public class GCMSNS
{
public static string SendGCMNotification(string deviceId, string message)
{
string GoogleAppID = "XYxxxxxxxxxxxxxxxxxxxx";//This is API Key Server
var SENDER_ID = "11111111111111"; //This is Google Project Id
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}", GoogleAppID));
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() + "®istration_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;
}
}