Search code examples
xamarin.formsxamarin.androidazure-notificationhubvisual-studio-app-center

Azure Notification Hub device registration


Since AppCenter retiring at the end of this year, I have started migrating to Azure-Notification-Hub. But the documentation for notification-hub is not clear at all. Especially the documentation for Xamarin.Android. It does not match with their latest SDK.

In the latest (version 1.1.1) azure-notification-hub SDK for Android (or Xamarin.Android) there's no need to implement FirebaseMessagingService. NotificationHub.Start() method registers the device in the Notification-Hub. A device registered with this way gets notifications without any problem.

NotificationHub.Start(Application, <HubName>, <ConnectionString>);

Addition tags to existing NotificationHub instance are also straightforward with the new SDK.

NotificationHub.AddTag("username:user123");

But in Registration Management official doc states that devices can register with the notification-hub either from client-side or from server-side. Is it necessary to use one of those methods if my app registered with the notification-hub using the NotificationHub.Start() method? Or do I missing something?

Also, when I was using the AppCenter, I have used the AppCenter-InstallId to target a specific device. With that in mind is it possible to use the NotificationHub.InstallationId to use as a tag (eg: "handle:<devce's InstallationId>") to send device-specific notifications?


Solution

  • Is it necessary to use one of those methods if my app registered with the notification-hub using the NotificationHub.Start() method?

    When you invoke NotificationHub.start(Application, ...), the Android SDK will listen for changes like added tags, new FirebaseMessagingService tokens, etc. Anytime it detects a change, it will invoke an InstallationAdapter to inform a backend of the new details.

    The default InstallationAdapter will send an PUT request to the Azure Notification Hubs backend as documented here. This is what is created by NotificationHub.start(Application, string, string); for people who are not hosting their own backend, this is a sensible default.

    If you have your own backend where you track devices, or you're just looking to keep your credentials server-side, you can swap out the InstallationAdapter to be a class that invokes your API. All you need to do is implement the InstallationAdapter interface and initialize the SDK by calling NotificationHub.start(Application, InstallationAdapter).

    If you use the NotificationHub.start(...) methods as indicated above, there is no further registration action required.

    With that in mind is it possible to use the NotificationHub.InstallationId to use as a tag (eg: "handle:<devce's InstallationId>") to send device-specific notifications?

    Yes! This documentation walks you through how to use the special tag format $InstallationId:{YOUR_TAG_ID} to target a specific device.

    When you've used NotificationHub.start(), if you do not specify an InstallationId, it will generate one for you.