如何将 Azure 通知中心中 iOS 设备注册的到期日期设置为 31-12-9999 23:59:59?

How to set the expiration date of an iOS device registration in Azure Notification Hub to 31-12-9999 23:59:59?

我有一个 Xamarin.Forms 应用程序,它使用 Azure 通知中心注册和发送推送通知。当我在服务器资源管理器 (Visual Studio) 中查找注册时,我注意到所有 iOS 注册的到期日期均为 3 个月。但期望的行为是在注册发生后,该注册不再过期(换句话说,过期日期:31-12-9999 23:59:59)

有没有办法为所有新注册实现这一点?但最好也适用于所有现有注册?

当有人登录应用程序时,将执行以下代码以在通知中心注册标签(会员 ID):

 public async void RegisterForNotifications(string tag)
 {
     // Set the Message listener
     MSNotificationHub.SetDelegate(new AzureNotificationHubListener());
     // Start the SDK
     MSNotificationHub.Start(AppConstants.ListenConnectionString, AppConstants.NotificationHubName);
     MSNotificationHub.AddTag(tag);
     var template = new MSInstallationTemplate();
     template.Body = AppConstants.APNTemplateBody;
     MSNotificationHub.SetTemplate(template, key: "template1");
 }

我正在使用 MSNotificationHub SDK 与 Azure 中的通知中心通信。我尝试使用 Whosebug 上建议的 来调整到期日期。此外,我制作了一个自定义 MSInstallationEnrichmentDelegate 以将到期日期设置为“NSDate.DistantFuture”。然而,当我尝试这个时,它立即崩溃,没有显示异常或日志记录。

提前致谢!

为此,您需要使用 MSInstallationEnrichmentDelegate 实现,然后将到期日期设置为您想要的任何值。默认情况下,安装设置为 90 天到期,但是,可以使用上述委托进行修改,例如:

public class InstallationEnrichmentAdapter : MSInstallationEnrichmentDelegate
{
    public override void WillEnrichInstallation(MSNotificationHub notificationHub, MSInstallation installation)
    {
         installation.ExpirationTime = NSDate.DistantFuture;
    }
}

然后可以在 AppDelegateFinishedLaunching 中将其设置为应用程序初始化的一部分。

const string ConnectionString = "<Connection-String>";
const string HubName = "<Hub-Name>";
private MSInstallationEnrichmentDelegate _installationEnrichmentDelegate;
private MSNotificationHubDelegate _notificationHubDelegate;

[Export("application:didFinishLaunchingWithOptions:")]
public bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
    _installationEnrichmentDelegate = new InstallationEnrichmentAdapter();
    _notificationHubDelegate = new NotificationMessageAdapter();
    MSNotificationHub.SetDelegate(_notificationHubDelegate);
            
    MSNotificationHub.SetEnrichmentDelegate(_installationEnrichmentDelegate);
    MSNotificationHub.Start(ConnectionString, HubName);

    AddTags();

     // Override point for customization after application launch.
     // If not required for your application you can safely delete this method
    return true;
}

public void AddTags()
{
    var language = NSBundle.MainBundle.PreferredLocalizations[0];
    var countryCode = NSLocale.CurrentLocale.CountryCode;
    var version = UIDevice.CurrentDevice.SystemVersion;

    var languageTag = $"language_{language}";
    var countryCodeTag = $"country_{countryCode}";
    var versionTag = $"version_{version}";

    MSNotificationHub.AddTag(languageTag);
    MSNotificationHub.AddTag(countryCodeTag);
    MSNotificationHub.AddTag(versionTag);
}

这已使用 iOS 15.1 和最新的 Xamarin.iOS 进行了全面测试,不会崩溃。