You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Azure通知中心iOS设备注册有效期设为9999年12月31日23:59:59

解决方案

一、新注册有效期永久配置(解决崩溃问题)

你之前运行崩溃大概率是两个原因:1. 将iOS专属的MSInstallationEnrichmentDelegate实现放在了共享项目中调用,触发平台API不兼容;2. 直接使用NSDate.DistantFuture时,和Azure Notification Hub SDK的序列化逻辑存在兼容问题。按照以下步骤修改即可:

  1. 在iOS平台项目中单独实现自定义委托,手动构造明确的有效期时间,避免序列化异常:
// 仅放在iOS平台项目中实现,不要放到共享项目
public class CustomInstallationEnrichmentDelegate : MSInstallationEnrichmentDelegate
{
    public override void EnrichInstallation(MSNotificationHub notificationHub, MSInstallation installation, Action<MSInstallation, NSError> completionHandler)
    {
        // 该时间戳对应UTC时间9999-12-31 23:59:59,替代NSDate.DistantFuture
        var expirationDate = NSDate.FromTimeIntervalSince1970(253402300799); 
        installation.ExpirationTime = expirationDate;
        completionHandler(installation, null);
    }
}
  1. 在iOS项目的AppDelegate.FinishedLaunching方法中,启动SDK之前先设置该委托:
// iOS AppDelegate中添加
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    // 先设置Enrichment委托,再启动通知中心SDK
    MSNotificationHub.SetEnrichmentDelegate(new CustomInstallationEnrichmentDelegate());
    // 原有初始化逻辑保持不变
    global::Xamarin.Forms.Forms.Init();
    LoadApplication(new App());
    return base.FinishedLaunching(app, options);
}
  1. 共享项目中的RegisterForNotifications注册逻辑不需要修改,保持现有代码即可。

二、现有注册批量更新有效期

客户端使用的Listen权限连接字符串没有批量修改注册的权限,需要在后端使用通知中心的管理权限连接字符串操作,示例代码(C#为例)如下:

// 后端代码,需安装NuGet包 Microsoft.Azure.NotificationHubs
var hubClient = NotificationHubClient.CreateClientFromConnectionString("你的通知中心管理连接字符串", "你的通知中心名称");
var continuationToken = string.Empty;
do
{
    // 筛选所有iOS平台的注册
    var registrations = await hubClient.GetRegistrationsByChannelAsync("apns", 100, continuationToken);
    continuationToken = registrations.ContinuationToken;
    
    foreach (var reg in registrations)
    {
        if (reg.ExpirationTime < new DateTime(9999, 12, 31, 23, 59, 59, DateTimeKind.Utc))
        {
            reg.ExpirationTime = new DateTime(9999, 12, 31, 23, 59, 59, DateTimeKind.Utc);
            await hubClient.UpdateRegistrationAsync(reg);
        }
    }
} while (!string.IsNullOrEmpty(continuationToken));

注意事项

  • 构造9999-12-31的时间时统一使用UTC时间,避免时区偏移导致有效期设置不符合预期
  • 所有iOS专属的API调用必须放在iOS平台项目中,禁止在共享项目中直接调用,否则会触发运行时崩溃

内容的提问来源于stack exchange,提问作者R. Cats

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 03:06:04