-
Notifications
You must be signed in to change notification settings - Fork 128
Description
I am currently facing an issue where a device token (APNs) works perfectly when used with Twilio (We're migrating to Azure), but fails when I register it with Azure Notification Hubs. Here's tghe code that shows the registration and notification sending in my .NET application using Azure Notification Hubs. The goal is to send a direct notification to a device using its APNs token. My device token looks something like this: "1e135b31225f2e03f4489d1bb90e3ee09bc19bac34aaeb01936fa8a0b9d34f73"
using Microsoft.Azure.NotificationHubs;
using System;
using System.Reflection;
using System.Threading.Tasks;
class Program
{
private static string connectionString = "my_connection_string";
private static string hubName = "my_hub_name";
static async Task Main(string[] args)
{
string deviceToken = "my_device_token"; // PNS handle
await RegisterDeviceToken(deviceToken);
await SendDirectNotificationToPnsHandle(deviceToken);
}
public static async Task RegisterDeviceToken(string deviceToken)
{
var hub = NotificationHubClient.CreateClientFromConnectionString(connectionString, hubName);
try
{
var registrationId = await hub.CreateRegistrationIdAsync();
var jsonBody = "{\"aps\": {\"alert\": \"Hello from Azure\"}}";
var registrationDescription = new AppleTemplateRegistrationDescription(deviceToken, jsonBody)
{
RegistrationId = registrationId
};
var upsertedRegistration = await hub.CreateOrUpdateRegistrationAsync(registrationDescription);
Console.WriteLine($"Registration successful. Registration ID: {upsertedRegistration.RegistrationId}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to register device token: {ex.Message}");
}
}
public static async Task SendDirectNotificationToPnsHandle(string deviceToken)
{
var hub = NotificationHubClient.CreateClientFromConnectionString(connectionString, hubName);
string notificationPayload = "{\"aps\":{\"alert\":\"Hello, this is a test notification!\"}}";
try
{
var outcome = await hub.SendDirectNotificationAsync(new AppleNotification(notificationPayload), deviceToken);
Console.WriteLine("Notification sent");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to send notification: {ex.Message}");
}
}
When I attempt to send a notification through Azure Notification Hubs using the above code, the notifications do not reach the device, even though the same token works when sending notifications via Twilio. The error I get from Azure Notification Hubs indicates an invalid handle, which suggests something might be wrong with the token or the way Azure handles it:
As you can see I get a The Push Notification System handle for the registration is invalid error when using the Test Send tool in the Azure Portal.
However when I run my code i get the following result from the method GetNotificationOutcomeDetailsAsync():
Questions for the Community:
- Is there a known issue with Azure Notification Hubs handling APNs tokens differently than Twilio?
- Could there be a configuration issue on the Azure side that I might be overlooking?
- Are there additional diagnostics or logs that I can enable in Azure to get more detailed error information?
Any suggestions on how to troubleshoot or resolve this issue would be greatly appreciated.