I am attempting to register a device to Azure Notifications Hub from a .NET Core backend. Using the DefaultFullSharedAccessSignature
name and connection string to connect to the Azure NH. None of the methods are working and are throwing an error
The remote server returned an error: (401) Unauthorized. Reason: Unauthorized
Not sure what I am missing but tried scouring the internet for solutions and no luck so far.
Here is my backend code which connects with and calls the Azure Notification Hub resource:
private NotificationHubClient _hub;
private Dictionary<string, NotificationPlatform> _installationPlatform;
private TelemetryClient _telemetryClient;
public PushNotificationsHelper(TelemetryClient telemetryClient)
{
_telemetryClient = telemetryClient;
_hub = NotificationHubClient.CreateClientFromConnectionString(NotificationHubOptions.ConnectionString, NotificationHubOptions.Name);
_installationPlatform = new Dictionary<string, NotificationPlatform>
{
{ nameof(NotificationPlatform.Apns).ToLower(), NotificationPlatform.Apns },
{ nameof(NotificationPlatform.Fcm).ToLower(), NotificationPlatform.Fcm },
};
}
public async Task<bool> CreateOrUpdateInstallationAsync(DeviceInstallation deviceInstallation, CancellationToken token)
{
if (string.IsNullOrWhiteSpace(deviceInstallation?.InstallationId) ||
string.IsNullOrWhiteSpace(deviceInstallation?.Platform) ||
string.IsNullOrWhiteSpace(deviceInstallation?.PushChannel))
return false;
var installation = new Installation()
{
InstallationId = deviceInstallation.InstallationId,
PushChannel = deviceInstallation.PushChannel,
Tags = deviceInstallation.Tags
};
if (_installationPlatform.TryGetValue(deviceInstallation.Platform, out var platform))
installation.Platform = platform;
else
return false;
try
{
await _hub.CreateOrUpdateInstallationAsync(installation, token);
}
catch (Exception ex)
{
return false;
}
return true;
}
Even calling the CreateRegistrationIdAsync
method is throwing the same error.
user16716435 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.