The Backend code to send messages using Azure Notificaion Hub to a specific userId:
var hubClient = NotificationHubClient.CreateClientFromConnectionString(connectionString, hubName);
var notification = new Dictionary<string, string> {
{ "message", "New Message" }
};
var userIdTag = "userId:12345";
await hubClient.SendTemplateNotificationAsync(notification, userTag);
Register the userId using an api that is called by the app:
var hubClient = NotificationHubClient.CreateClientFromConnectionString(connectionString, hubName);
var installation = new Installation
{
InstallationId = Guid.NewGuid().ToString(), //or use userId?
PushChannel = deviceToken, //sent by app
Tags = new List<string> { "userId:12345" }
};
await hubClient.CreateOrUpdateInstallationAsync(installation);
How is a message then sent to a specific userId, what work needs to be done on the Android app side in order for the message to be sent to a specific userId?
Workflow:
onNewTokenmethod inMyFirebaseMessagingServiceit will trigger FCM by this token will obtain < That token is sent to the backend server for registration with Azure Notification Hub < backend server receives the FCM token from the Android app < backend server uses the Azure Notification Hub SDK to register the FCM token with Azure Notification Hub < FCM token is associated with the appropriate user ID using tags < based on our Configuration we will get the success or failure of the registration processFirebase Messaging Service :
AndroidManifest.xml :
Enable your android API for public messaging.
Copy this server key to connect with the azure notification hub.
Save the key as shown below.
Backend Code :
We can also send a test notification from portal to our required UserID as shown in below.