【发布时间】:2016-09-12 12:57:28
【问题描述】:
我在使用 Azure 通知中心时遇到问题。我的 iOS 设备最初已正确注册并接收推送。然而,偶尔一两个设备会莫名其妙地停止接收推送通知。调试此问题似乎是由于 APNS 使设备注册的通道过期:
设备注册和推送停止之间的时间不超过 10 分钟。我可以 100% 保证此设备没有通过通知中心手动取消注册。我正常使用该应用程序,并收到了一些推送,然后它停止接收任何推送。
作为参考,应用通过 WebAPI 后端注册到 Azure 通知中心。作为参考,这里是每当用户登录或应用程序返回前台时注册设备以进行推送的代码。如果我在这个逻辑中做错了什么,请告诉我:
private async Task<string> RegisterDevice(string token)
{
string newRegistrationId = null;
if (!string.IsNullOrWhiteSpace(token))
{
var registrations = await _notificationHub.GetRegistrationsByChannelAsync(token, 100);
foreach (var registration in registrations)
{
if (newRegistrationId == null)
{
newRegistrationId = registration.RegistrationId;
}
else
{
await _notificationHub.DeleteRegistrationAsync(registration);
}
}
}
return newRegistrationId ?? await _notificationHub.CreateRegistrationIdAsync();
}
public async Task<string> CreateOrUpdateRegistration(string userId, string token, string platform, string registrationId = null)
{
var userDetail = await _userDetailRepo.GetAsync(userId);
if (userDetail == null)
throw new ApiException(HttpStatusCode.BadRequest, "User details could not be found");
if (string.IsNullOrWhiteSpace(registrationId))
registrationId = await RegisterDevice(token);
RegistrationDescription registration;
switch (platform)
{
case Settings.Platforms.Android:
var gcmTemplate = "[REDACTED]"
registration = new GcmTemplateRegistrationDescription(token, gcmTemplate);
break;
case Settings.Platforms.Ios:
var apnsTemplate = "[REDACTED]";
registration = new AppleTemplateRegistrationDescription(token, apnsTemplate);
break;
default:
throw new ApiException(HttpStatusCode.BadRequest, "Platform not recognised");
}
registration.RegistrationId = registrationId;
SetDeviceRegistrationTags(registration, userId, userDetail.TwingleAcceptedNotificationEnabled, userDetail.TwingleDeclinedNotificationEnabled, userDetail.MessageReceivedNotificationEnabled, userDetail.ConnectionDeletedNotificationEnabled);
var registrationStale = false;
try
{
await _notificationHub.CreateOrUpdateRegistrationAsync(registration);
}
catch (MessagingException e)
{
var webEx = e.InnerException as WebException;
if (webEx != null && webEx.Status == WebExceptionStatus.ProtocolError)
{
var response = (HttpWebResponse)webEx.Response;
if (response.StatusCode == HttpStatusCode.Gone)
{
registrationStale = true;
}
}
}
// if the registration is stale and/or removed then it needs to be re-created with a new registrationId
if (registrationStale)
registrationId = await CreateOrUpdateRegistration(userId, token, platform);
return registrationId;
}
如果没有对设备(最初成功的)注册进行任何更改,谁能告诉我是什么导致设备在 APNS 中过期?
【问题讨论】:
-
我确实做到了,尽管我在这个 NH 中没有沙盒帐户。我有一个单独的实例设置。谢谢。
标签: c# azure notifications apple-push-notifications azure-notificationhub