我遇到Azure通知中心问题。我的iOS设备最初正在正确注册并接受推送。然而偶尔会有一两个设备莫名其妙地停止接收推送通知。调试此问题似乎问题是由于APNS使设备注册的频道失效:
设备注册和推送停止之间经过的时间不超过10分钟。我可以100%保证此设备未通过通知中心手动取消注册。我正常使用该应用程序,并接受了一些推动,然后它停止接收任何。
作为参考,该应用程序通过WebAPI后端注册到Azure Notification Hub。作为参考,这里是用户登录或应用程序返回前台时注册设备以进行推送的代码。如果我在这个逻辑中做错了什么,请告诉我:
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;
}
当没有对其(首次成功)注册进行任何更改时,有人能告诉我导致设备过期的原因是什么?
答案 0 :(得分:1)
设备必须定期向APNS重新注册,以确保他们拥有有效的令牌。如果他们没有,那么他们将无法获得推送(一旦失败回调触发其移除,您将看到设备注册在通知中心消失)。这是设计的:https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html
答案 1 :(得分:-1)
问题是因为默认情况下AppleTemplateRegistrationDescriptions
将0
指定为超时值。这意味着只要APNS无法向设备发送一条消息,它就会被立即删除;没有回旋余地。为了解决这个问题,我指定了类的Expiry
属性,以便在发送通知和设备确认之间允许延迟。这意味着APNS从未过度注册任何设备。
AppleTemplateRegistrationDescription registration = new AppleTemplateRegistrationDescription(DeviceToken)
{
BodyTemplate = new CDataMember(ApnsBodyTemplate)
};
registration.Expiry = DateTime.UtcNow.AddHours(2);
await client.CreateRegistrationAsync(registration);