Azure Notification Hub Xamarin reconnecting

Tom Willemin 1 Reputation point
2022-04-08T09:39:21.707+00:00

Hello,

I have implemented in my Xamarin (IOS/Android) / Asp.Net Core project a notification system using Azure Notification Hub.
On the client side, the application registers to the Azure hub via this code (android side, on MainActivity.cs -> OnCreate()):

// Listen for push notifications
NotificationHub.SetListener(new NotificationListener());

NotificationHub.SetInstallationSavedListener(new NotificationInstallationListener());
NotificationHub.SetInstallationSaveFailureListener(new NotificationInstallationFailedListener());

// Start the SDK
NotificationHub.Start(Application, Constants.PushNotification.HubName, Constants.PushNotification.ConnectionString)

And on the server side, we'll send the notifications to the Azure hub so that it can forward them to the devices.

The problem is that when I delete the registrations from the server, the devices do not reconnect to the Azure Hub at launch. They do it only when you reinstall the application completely.

1 - How to make the application register to the Azure Notification Hub at launch?

I have another question, in the Azure portal -> Azure Notification Hub, there is a Time To Live that is set to infinite time by default. When I retrieve all the device registrations, I have an expiration date (in each registration item) that is set to 3 months.
It is updated every time the device is used in the notifications.

2 - Why are there two different expiration dates? And what happens if the expiration date of 3 months in the registrations is expired?

Thanks for your future answers, I remain available for any missing information

Azure Notification Hubs
Azure Notification Hubs
An Azure service that is used to send push notifications to all major platforms from the cloud or on-premises environments.
383 questions
Developer technologies .NET Xamarin
Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. ajkuma 28,036 Reputation points Microsoft Employee Moderator
    2022-04-11T18:56:05.267+00:00

    TomWillemin-6431, Firstly, apologies for the delay in responding from over the weekend. And, sorry for the long post, I'm trying to provide more insights for your 2 questions:

    1. Installations and Registrations are tied directly to a device with the push channel being the way Notification Hub (NH) communicate with the PNS.
      Just to highlight the difference on installation vs registration: The installation or registration (depending on the API you are using), is tied uniquely to a single device through its Platform Notification Service (PNS) unique ID.

    If you have two devices, this means that you will have two different installations/registrations.

    -Please do share more details about your requirement/scenario --Maybe your requirement is similar to this discussion thread --
    Can Azure push the same notification to multiple apps (different bundle Id's) from one notification hub”.

     “If this is the case -Unfortunately, this is not possible to do with Notification Hubs. You would need to set up multiple hubs for each bundle ID and each app would then register with the specific hub they belong to. Also, this is even recommended just between sandbox/production usage for an app developer – Example: have a myhub-sandbox and myhub-production. The sandbox hub would hold the sandbox credential and the production hub the production credentials. Then the client app itself should know which variant it is and register with the correct hub. Then push the identical notification to each of the hubs you want to target.”   
    

    2.It looks like you’re referring to the device registrations (there are two patterns for registering devices -registering from the device directly to the notification hub, and registering through the application backend) – yes, these registrations are transient, just similar to the PNS handles that they contain, registrations expire. You can set the time to live for a registration on the Notification Hub, up to a maximum of 90 days.

    --This limit means that they must be periodically refreshed, so after 3 months (90 days) - this automatic expiration also simplifies cleanup when your mobile application is uninstalled.


  2. Tom Willemin 1 Reputation point
    2022-04-12T08:48:15.013+00:00

    Thank you for your answer.

    For the registration of a device, I use the registration system via the piece of code I sent you above (client side).
    The registration of the device is done when launching the Xamarin application.
    If I want to delete (server side) all registrations (with the NotificationHubClient package) :

                var registrations = await _azureNotificationHub.GetAllRegistrationsAsync(100);  
                  
                foreach(var registration in registrations)  
                {  
                    await _azureNotificationHub.DeleteRegistrationAsync(registration);  
                }  
    
    1. I would like when I relaunch the application on a device, that it registers again to the Azure Notification Hub (it does this only if you reinstall the Xamarin application).
    2. If the 90 days time to live expires, the registration is deleted automatically?
      If yes, then what is the purpose of the Time to Live in the Azure portal:
      192225-image.png

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.