Search code examples
xamarinxamarin.formsfirebase-cloud-messagingapple-push-notificationsazure-notificationhub

How to Register a device through Azure Notification Hub App Service SDK or manually for a Xamarin Forms App?


So i'm using Azure Notification Hub, and in that i followed their tutorial where they had mentioned to use FCM for Android , configure it and use their API key, and creating a certificate for iOS, which is working flawless

But the problem is i'm working on Xamarin forms, and i'd like to know if i could do the registration manually through API, and i've already written a method to do that in my API Service

 public async Task<string> RegisterDevice([FromBody] string handle = null)
    {
        string newRegistrationId = null;
        //newRegistrationId = await hub.CreateRegistrationIdAsync();
        //// make sure there are no existing registrations for this push handle (used for iOS and Android)
        //if (handle != null)
        //{
        //    var registrations = await hub.GetRegistrationsByChannelAsync(handle, 100);

        //    foreach (var registration in registrations)
        //    {
        //        if (newRegistrationId == null)
        //        {
        //            newRegistrationId = registration.RegistrationId;
        //        }
        //        else
        //        {
        //            await hub.DeleteRegistrationAsync(registration);
        //        }
        //    }
        //}
        newRegistrationId = await hub.CreateRegistrationIdAsync();
        return newRegistrationId;
    }

But i'm not able to understand how the device would be linked to this registration ID and/or what is a pns handle, i know the abbreviation but i dont know how to use it in this case or if at all is it necessary? Any help would be deeply appreciated


Solution

  • While registering Azure Notification Hub, If you want to ask for Push permissions after login, you have to call RegisterForRemoteNotifications(); (iOS) & CreateNotificationChannel(); (Android) after Login. What you're asking would require a few steps- You would have to created a DependencyService like this, which would require creating an Interface like IPushRegistrationService with a RegisterForPush() function that would basically be called after login:

    var pushService = DependencyService.Get<IPushRegistrationService>();
    pushService.RegisterForPush();