如何将Azure通知中心中iOS设备注册的截止日期设置为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中的NotificationHub进行通信。我尝试使用Stackoverflow上的建议解决方案来调整到期日期。此外,我制作了一个自定义MSIInstallationEnrichmentDelegate,将到期日期设置为";NSDate.DancetFuture";。然而,当我尝试这个时,它会立即崩溃,而不会显示异常或日志记录。

提前感谢!

要做到这一点,您需要使用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进行了彻底测试,不会崩溃。

最新更新