Using Registration ID to update tags [droid / ios]

Johny B 21 Reputation points
2020-11-24T13:37:55.057+00:00

Hi,

We are migrating from Appcenter Push to Azure Notification Hub.

We have this interface that is implemented for both platforms [ios and droid]

public interface INotifications
{
        void OpenSettings();
        Task<NotificationState> GetStatus();
        Task<bool> RequestPermission();
        Task<string> GetPushId();
        Task CancelAll();
        Task Send(string title, string message);
}

I'm trying to retrofit Azure Notification Hub to avoid making many changes (if possible)

Implementation of Task<string> GetPushId(); using AppCenter used to be as simple as below:

public async Task&lt;string&gt; GetPushId()
{
    var installId = await AppCenter.GetInstallIdAsync();
    return installId?.ToString();
}

This method worked for both ios and droid. We have backend APIs that take care of storing this PushId in SQL DB along with some user information so we can target specific users/devices for notifications.

Now with Azure Notification Hub, I understand that we do not need to maintain a DB to store such information as we can assign tags per registration.
We've changed our backend APIs to now use Microsoft.Azure.NotificationHubs; We expose a register method which now takes a registration Id and updates tags.

public async Task&lt;int&gt; Register(RegisterArgs args, int refNumber)
{
    var registry = await notificationHubClient.GetRegistrationAsync&lt;RegistrationDescription&gt;(args.RegistrationId);
    registry.Tags.Add(args.Email);
    registry.Tags.Add(refNumber.ToString());

    await notificationHubClient.UpdateRegistrationAsync(registry);
    return 1;
}

Now my question is how can I access Registration ID in client app so I can pass that to the backend?

PS: For droid, I can only access the registration id while registering which is done via a service class GoogleFirebaseMessagingService : FirebaseMessagingService - override void OnNewToken(string token). I can access the reg id here using this.hub.Register(token, tags.ToArray()).RegistrationId;

But this does not seems to be an option for iOS.

Any help will be greatly appreciated.

Summary: I want to register for push notifications as soon as the app opens - which I've done via MainActivity.cs in Droid and AppDelegate in ios. Registration is successful and I'm also able to target notifications to each or all registrations. When users actually log in to their account in the app - I want to update device specific registration tags with user information so our backend push notification job can send notifications using tags.

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.
325 questions
{count} votes

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.