【发布时间】:2021-11-30 03:55:09
【问题描述】:
我尝试使用此代码在 iOS 中发送推送通知,它可以工作但仅持续一段时间,然后它没有从 var response = apns.SendAsync(push).Result; 响应,如果我需要再次发送推送通知,那么我需要重新启动服务器然后它正在重新开始工作,但只是持续一段时间。
我正在使用 dotAPNS v4.0.30319 NuGet 包发送推送通知。 我正在使用亚马逊服务器。
为了更清楚,我在这里添加了一个代码示例。
public bool SendIOSNotification(Dictionary<string, Dictionary<string, string>> iosDictionary, ILog log = null)
{
string p8File= HttpContext.Current.Server.MapPath("*******");
var options = new ApnsJwtOptions()
{
BundleId = "*****",
CertFilePath = p8File, // use either CertContent or CertFilePath, not both
KeyId = "******",
TeamId = "*****"
};
var apns = ApnsClient.CreateUsingJwt(new HttpClient(new WinHttpHandler()), options);
var push = new ApplePush(ApplePushType.Alert)
.AddBadge(1)
.AddSound("sound.caf");
foreach (var itemKey in iosDictionary)
{
_log.DebugFormat("========== IOS ==========");
_log.DebugFormat("Device Item {0}", itemKey);
string payLoadCatgory = string.Empty;
push.AddCustomProperty("content-available", "1", true)
.AddToken(itemKey.Key.ToString());
foreach (var itemValue in itemKey.Value)
{
try
{
if (!itemValue.Key.ToLower().Contains("id"))
{
push.AddCustomProperty(itemValue.Key, itemValue.Value, true);
}
else
{
push.AddCustomProperty(itemValue.Key, Convert.ToInt64(itemValue.Value), true);
}
if (itemValue.Key.ToLower().Contains("category"))
{
payLoadCatgory = itemValue.Value;
}
}
catch (Exception ex)
{
log.Error(string.Format("Error in send Notification to IOS :: DeviceToken is {0}", itemKey.Key));
}
}
try
{
_log.Debug("Start send notification");
var response = apns.SendAsync(push).Result;
_log.Debug("Sent notification");
if (response.IsSuccessful)
{
log.Info(string.Format("An alert push has been successfully sent!"));
return true;
}
else
{
switch (response.Reason)
{
case ApnsResponseReason.BadCertificateEnvironment:
break;
default:
throw new ArgumentOutOfRangeException(nameof(response.Reason), response.Reason, null);
}
log.Error(string.Format("Failed to send a push, APNs reported an error: " + response.ReasonString));
}
}
catch (TaskCanceledException)
{
log.Error(string.Format("Failed to send a push: HTTP request timed out."));
}
catch (HttpRequestException ex)
{
log.Error(string.Format("Failed to send a push. HTTP request failed: " + ex));
}
catch (Exception ex)
{
log.Error(string.Format("Failed to send a push. HTTP request failed: " + ex));
return false;
}
}
return false;
}
【问题讨论】:
标签: c# asp.net-mvc asp.net-web-api model-view-controller apple-push-notifications