0

I have a Xamarin.Forms app that uses Azure Notification Hub to register and send push notifications. When I looked up the registrations in Server Explorer (Visual Studio), I noticed that there is an Expiration Date of 3 months on all iOS registrations. But the desired behavior is that after a registration has taken place, this registration no longer expires (in other words, expiration date: 31-12-9999 23:59:59)

Is there a way to achieve this for all new registrations? But preferably also on all existing registrations?

The moment someone logs into the app, the following code is executed to register the tag (member id) in the notification hub:

 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");
 }

I'm using the MSNotificationHub SDK to communicate with the Notification Hub in Azure. I tried to adjust the expiration date using the suggested solution on Stackoverflow. In addition, I made a custom MSInstallationEnrichmentDelegate to set the expiration date to "NSDate.DistantFuture". However, when I try this out, it crashes immediately without showing an exception or logging.

Thanks in advance!

R. Cats
  • 89
  • 1
  • 7
  • You are ignoring the statement made [here](https://learn.microsoft.com/en-us/answers/questions/595774/how-to-set-the-expiration-date-of-an-ios-device-re.html), which says: "This is intended behavior to keep registrations/installations living forever long past when the device has the app." Did you try to set it just 1 year in the future ? – Luuk Nov 05 '21 at 16:19
  • BTW, from [Improved Per Message Telemetry and device expiry for Notification Hubs](https://azure.microsoft.com/en-us/blog/push-notification-hub-telemetry-expiry-update/), I follow the link to [Registration](https://learn.microsoft.com/en-us/azure/notification-hubs/notification-hubs-push-notification-registration-management), and do read: "By default, registrations and installations do not expire.". – Luuk Nov 05 '21 at 16:32
  • This link(https://stackoverflow.com/questions/39413953/how-to-update-expiration-time-in-azure-notification-hub-registration) may help you. – Wen xu Li Nov 18 '21 at 09:44

1 Answers1

1

To do that, you will need to use an MSInstallationEnrichmentDelegate implementation which will then set the expiration date to whatever you want. By default, the installations set to 90 day expirations, however, this can be modified using the aforementioned delegate such as the following:

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

This can then be set as part of the initialization of your application in the FinishedLaunching of your AppDelegate.

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);
}

This has been thoroughly tested using iOS 15.1 and the latest Xamarin.iOS and does not crash.