0
votes

I am using Azure Notification Hub with Xamarin Android. It works fine in normal scenario and I am able to get push notifications on my registered tags but on update of tag or reregistering the hub it creates duplicate registrations. Also the tags which were removed post registration still gets the notification. Below is the sample snippet for the same

try
{
  Hub.UnregisterAll(registrationId);
}
catch (Exception ex)
{

}

var tags = getting active tags 
try
{
    var hubregistration = Hub.Register(registrationId, tags);
}
catch (Exception ex)
{

}
2

2 Answers

0
votes

AFAIK, the Registration Token (registrationId) issued by GCM is used to identity the client app, and it may be the same when re-register from GCM without unregistering from GCM. Based on your code, you are using the Registrations model. Hub.UnregisterAll(registrationId) would try to un-register the registrations with the same Registration Token (pnsHandle) from your azure notification hub.

I would recommend you capturing the exception when you call UnregisterAll. Also, you could leverage Server Explorer from Visual Studio, choose your notification hub, then view and manage all the registrations in your hub as follows to narrow this issue:

Note: You could check with your device registrations and try to find whether you could retrieve the duplicated registrations (same PNS Identifier (Registration Token), different tags / Azure Registration ID or different PNS Identifier (Registration Token) for the same client app, etc.).

If you find different PNS Identifier (Registration Token) for the same client app, I assume that your client app need to store the previous Registration Token and compare with the latest Registration Token, UnregisterAll the old Registration Token if not match firstly, then register the new Registration Token with your notification hub.

Additionally, the Installations model could avoid the duplicate registrations. For more details, you could refer to Registration management.

0
votes

This is my working methods for Register and UnRegister from azure hub

    void unregister ()
    {
        try {
            NotificationHub hub = new NotificationHub (Constants.NotificationHubName, Constants.ListenConnectionString, this);
            hub.UnregisterAll (FirebaseInstanceId.Instance.Token);
        } catch (Exception ex) {

        }
    }


    void register ()
    {
        try {

            NotificationHub hub = new NotificationHub (Constants.NotificationHubName, Constants.ListenConnectionString, this);

            var tags = new List<string> () { ... };
            hub.Register (FirebaseInstanceId.Instance.Token, tags.ToArray ());
        } catch (Exception ex) {

        }
    }

based on this documentation https://docs.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm