0
votes

Some background... I am building a Xamarin.Forms mobile application to allow monitoring of energy data from a device located at a user's home (which is pushed to a backend and stored every minute). From my (Azure Worker Role) backend I want to be able to send push notifications via Azure Notification Hub. Sometimes I'll want to push to all devices, sometimes, to groups, and sometimes to individual devices (I believe 'tags' are the way to do this). I currently have an Azure Notification Hub configured and I can send test messages (from the Azure portal) to all the iOS devices I am using for testing.

What I'm having trouble doing is figuring out how to register each device with the Notification Hub. What I want to happen is that when the user logs in to the mobile app, the device 'handle' (or device token) is passed back to the server. This can then be saved on the backend, and used to register the device using code based on the example provided at https://msdn.microsoft.com/en-us/library/azure/dn743807.aspx and Registering device on Azure Notification Hub from ASP.NET, as well as make changes to the associated 'tags' on the backend. I can then grab the device handle from the Worker Role if I need to send a notification to an individual device, or group of devices.

I have found a number of examples that all seem to be missing that vital piece of information for me to get this working. Most examples I've found seem to use rely on the use of Azure Mobile Apps (and the MobileServiceClient class), but I'm using a 'regular' MVC5 ASP.NET Web App. I've noticed with both Mobile Apps and Web Apps the Azure portal allows me to configure "Push", so presumably I can support push from either. I tried adding a RegisterController to my Web App, but this didn't seem to help when calling:

var push = PushNotificationsManager.DefaultManager.CurrentClient.GetPush();
push.RegisterAsync(deviceToken, templates); 

based on the code sample provide at: https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-xamarin-forms-get-started-push#create-hub. Where the push client is:

client = new MobileServiceClient("http://mywebapp.azurewebsites.net");

What endpoint is the RegisterAsync attempting to call? I get no error.

Registering device on Azure Notification Hub from ASP.NET seems to be suggesting that I need to add a RegisterController to my Web App, but it doesn't show the Xamarin.Forms code to call this.

So, how do I register a mobile device from a Xamarin.Forms app with an Azure Notification Hub, via an MVC5 Web App?

2
Do you want the answer to be absolutely about Azure Notification Hub?Kevin Le - Khnle
Yes, please - that is the Azure service I am using. ThnxProfNimrod

2 Answers

2
votes

what you are looking for is something like this.

Get the token of users Ios Device in your web api.

   var _hubClient=NotificationHubClient.CreateClientFromConnectionString(YourConnectionstring here)
   //this is notification hub client  
   var registrationId= await _hubClient.CreateRegistrationIdAsync();

Above code will create a registrationId for you .After this

   var registration = new GcmRegistrationDescription(devicetoken)
        {
            RegistrationId = RegistrationId ,  //one we got in previous 
                                                call.
            Tags = new HashSet<string>
            {
                "YourDesiredTag"
            }
        };

     //this completes registration of your device with desired tags.   
    _hubClient.CreateOrUpdateRegistrationAsync(registration);

now that your registration is complete. you can simply send notifications from your worker role targetting a tag , and the notifications should reach the desired device.

 _hubClient.SendGcmNativeNotificationAsync(notifPayload, tags)

you should get more info here https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-push-notification-registration-management

above code is possibly valid for gcm/android but is standard that can be followed.

1
votes

According to your description, I assumed that you are using the App Service Push configuration which is a feature provided by Azure Portal. You just need to log into Azure portal, choose your App Service, then click "Settings > Push" to configure your Notification Hub.

For your Xamarin.Form client, you need to install the package Microsoft.Azure.Mobile.Client, then leverage the extension method GetPush for IMobileServiceClient under MobileServiceClientExtensions.cs (Note: this is for UWP) and the PushHttpClient.cs to send registration requests against the build-in endpoint push/installations provided by App Service Push.

Based on your code, you did it in the right way. I would recommend you leverage fiddler to capture the network traces when register the device. Also, you could leverage the Server Explorer under VS to check your existing registrations, details you could follow here.

Additionally, you could also create your custom Web API endpoints for registering the notification. For more details, you could follow Azure Notification Hubs Notify Users with .NET backend for creating your backend and the relevant code for the client project. Moreover, I would recommend you follow Registration management for better understanding of the Registration and Installation model to achieve your purpose.