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

Posted

技术标签:

【中文标题】如何在 Azure Notification Hub 中将 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? 【发布时间】:2021-12-19 16:10:37 【问题描述】:

我有一个 Xamarin.Forms 应用程序,它使用 Azure 通知中心注册和发送推送通知。当我在 Server Explorer (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 中的通知中心进行通信。我尝试使用 *** 上建议的 solution 调整到期日期。另外,我做了一个自定义的 MSInstallationEnrichmentDelegate 将过期日期设置为“NSDate.DistantFuture”。但是,当我尝试这样做时,它会立即崩溃,而不会显示异常或日志记录。

提前致谢!

【问题讨论】:

您忽略了 here 的声明,该声明说:“这是为了让注册/安装在设备拥有应用程序后永远存在很久的行为。”您是否尝试将其设置为未来 1 年? 顺便说一句,来自Improved Per Message Telemetry and device expiry for Notification Hubs,我点击链接到Registration,并阅读:“默认情况下,注册和安装不会过期。”。 此链接(***.com/questions/39413953/…) 可能对您有所帮助。 【参考方案1】:

为此,您需要使用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 进行了全面测试,不会崩溃。

【讨论】:

以上是关于如何在 Azure Notification Hub 中将 iOS 设备注册的到期日期设置为 31-12-9999 23:59:59?的主要内容,如果未能解决你的问题,请参考以下文章

如何在不更新 IOS 应用程序的情况下使用 Azure 通知中心

阅读所有注册 azure notification hub python

Firebase Cloud 消息传递和 Azure Notification Hubs 设备注册相互实现

Xamarin Android 使用 Asp.Net core web api 连接到 Azure Notification Hub [关闭]

Azure 通知中心:如何查看每条消息遥测?

「SAP技术」SAP 如何看序列号被包在哪些HU里?