C# 为什么发送Apple推送通知和Android使用TcpClient会导致应用程序挂起?
我在网上搜索了一下,找不到解决问题的办法 我有一个针对.NETFramework4.0的项目 我有两种方法。一个用于将通知推送到ios设备,另一个用于将通知推送到android设备 这是我的第一个方法:C# 为什么发送Apple推送通知和Android使用TcpClient会导致应用程序挂起?,c#,push-notification,tcpclient,C#,Push Notification,Tcpclient,我在网上搜索了一下,找不到解决问题的办法 我有一个针对.NETFramework4.0的项目 我有两种方法。一个用于将通知推送到ios设备,另一个用于将通知推送到android设备 这是我的第一个方法: public void SendAplePushNotificationMessage(string body, int Participant_ID, int Trainer_ID) { var componentSetup = new ComponentSetup()
public void SendAplePushNotificationMessage(string body, int Participant_ID, int Trainer_ID)
{
var componentSetup = new ComponentSetup();
var odsDevicesToken = componentSetup.getDevicesToken(Participant_ID, Trainer_ID, 1);
if (odsDevicesToken.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in odsDevicesToken.Tables[0].Rows)
{
try
{
var deviceToken = HexStringToByteArray(dr["DeviceToken"].ToString());
var memoryStream = new MemoryStream();
var writer = new BinaryWriter(memoryStream);
writer.Write((byte)0); //The command
writer.Write((byte)0); //The first byte of the deviceId length (big-endian first byte)
writer.Write((byte)32); //The deviceId length (big-endian second byte)
writer.Write(deviceToken);
writer.Write((byte)0); //First byte of payload length; (big-endian first byte)
var payload = "{\"aps\":{\"alert\":\"" + body + "\",\"badge\":1,\"sound\":\"default\"}}";
var b1 = Encoding.UTF8.GetBytes(payload);
if (b1.Length > 256)
{
body = body.Substring(0, 106) + "...";
payload = "{\"aps\":{\"alert\":\"" + body + "\",\"badge\":1,\"sound\":\"default\"}}";
b1 = Encoding.UTF8.GetBytes(payload);
}
writer.Write((byte)b1.Length);
writer.Write(b1);
writer.Flush();
var array = memoryStream.ToArray();
memoryStream.Write(Encoding.ASCII.GetBytes(body), 0, body.Length);
var port = Convert.ToInt32(ConfigurationManager.AppSettings["APNPortNo"]);
var hostname = ConfigurationManager.AppSettings["APNHostName"];
var certificatePath = Server.MapPath("~") + ConfigurationManager.AppSettings["APNCertificatePath"].Replace("/", "\\");
Logger.LogInfoMessage("certificatePath: " + certificatePath);
var clientCertificate = new X509Certificate2(File.ReadAllBytes(certificatePath), "");
var certificatesCollection = new X509Certificate2Collection(clientCertificate);
var client = new TcpClient(hostname, port);
var sslStream = new SslStream(client.GetStream(), false,
new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
try
{
sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, false);
sslStream.Write(array);
sslStream.Flush();
client.Close();
}
catch (AuthenticationException ex)
{
client.Close();
var setupObj = new ComponentSetup();
setupObj.AddErrorLog(ex.Message, CamelRaceContext.UserSettings.User.USER_NAME, null,
Participant_ID, dr["DeviceToken"].ToString());
if (ex.InnerException != null)
Logger.LogErrorMessage(ex.InnerException, ex.InnerException.Message);
throw new CamelRaceApplicationException<MobileAdmin_ManageNotifications>(ex.Message, ex);
}
catch (CamelRaceApplicationException<MobileAdmin_ManageNotifications> ex)
{
throw new CamelRaceApplicationException<MobileAdmin_ManageNotifications>(ex.Message,ex);
}
catch (Exception e)
{
client.Close();
var setupObj = new ComponentSetup();
setupObj.AddErrorLog(e.Message, CamelRaceContext.UserSettings.User.USER_NAME, null, Participant_ID, dr["DeviceToken"].ToString());
}
}
catch (CamelRaceApplicationException<MobileAdmin_ManageNotifications> ex)
{
throw new CamelRaceApplicationException<MobileAdmin_ManageNotifications>(ex.Message, ex);
}
catch (Exception exMain)
{
var setupObj = new ComponentSetup();
setupObj.AddErrorLog(exMain.Message, CamelRaceContext.UserSettings.User.USER_NAME, null, Participant_ID, dr["DeviceToken"].ToString());
if (exMain.InnerException != null)
Logger.LogErrorMessage(exMain.InnerException, exMain.InnerException.Message);
}
}
}
}
现在,当设备数为3或4时,此代码工作正常
当这个数字增加时。比如8423
它会导致应用程序挂起
目前我正在点击按钮调用此代码
我想知道使用task类来编写代码会不会让它正常工作
var task1 = Task.Factory.StartNew(() =>SendAplePushNotificationMessage(txtNotification.Text, Participant_ID, Trainer_ID) , TaskCreationOptions.LongRunning);
var task2 = Task.Factory.StartNew(() => SendGCMPushNotificationMessage(txtNotification.Text, Participant_ID, Trainer_ID), TaskCreationOptions.LongRunning);
Task.WaitAll(task1, task2);
我知道你们中的一些人会建议使用pushsharp
我正在考虑这样做
然而,推动尖锐的目标4.5
我的项目目标是4.0
升级不是一个选项
我的解决方案有效吗
如果没有,我该如何解决我的问题呢
谢谢 伙计们,我需要帮助。背景线程不起作用。伙计们,我需要帮助。背景线程不工作。
var task1 = Task.Factory.StartNew(() =>SendAplePushNotificationMessage(txtNotification.Text, Participant_ID, Trainer_ID) , TaskCreationOptions.LongRunning);
var task2 = Task.Factory.StartNew(() => SendGCMPushNotificationMessage(txtNotification.Text, Participant_ID, Trainer_ID), TaskCreationOptions.LongRunning);
Task.WaitAll(task1, task2);