I am sending the push notification to Android and Apple devices from ASP.NET Core Web API. But most of the time in between push failed and I am getting an error
The Push Notification System handle for the registration is invalid
I do not understand this.
I am using the NotificationHub nuget package in my project. Package version in 4.0.0.
I am showing my code here - please help me with this issue.
public async static Task SendPushNotificationToDevice(HubNotification hub notification)
{
if (hubNotification != null)
{
try
{
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString
(ConfigurationManager.AppSettings["HubConnectionString"],
ConfigurationManager.AppSettings["HubName"], true);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
if (!string.IsNullOrEmpty(hubNotification.DeviceId) &&
!string.IsNullOrEmpty(hubNotification.DeviceType))
{
// check if the already registered device
var registationDescriptions = hub.GetRegistrationsByTagAsync(hubNotification.ToUserEmail, 100)
.Result;
foreach (var registrationDescription in registationDescriptions)
{
// We shouldn't have any extra registrations; delete if we do.
hub.DeleteRegistrationAsync(registrationDescription);
}
// set message
hubNotification.Message = string.Format(hubNotification.Message);
if (!string.IsNullOrEmpty(hubNotification.Message))
{
string jsonPayLoad = string.Empty;
NotificationOutcome outcome;
if (hubNotification.DeviceType.ToLower().Contains("android"))
{
var registeredNewDevice = hub.CreateGcmNativeRegistrationAsync(
hubNotification.DeviceId.Replace("-", ""),
new[]{hubNotification.ToUserEmail})
.Result;
jsonPayLoad = "{ " data ": " +
"{ " message
": "
" + hubNotification.Message.Trim() + "
"," +
"" ListId
": "
" + hubNotification.ListId + "
"," +
"" notificationType
": "
" + hubNotification.NotificationType + "
"," +
"" IsOwner
": "
" + hubNotification.IsOwner + "
"," +
"" ListName
": "
" + hubNotification.ListName + "
"," +
"" ListType
": "
" + hubNotification.ListType + "
"," +
"" badge
": "
" + hubNotification.badge + "
"," +
"" inProgress
": "
" + hubNotification.InProgress + "
"," +
"" headercolor
": "
" + hubNotification.ListHeaderColor + "
"," +
"" titlecolor
": "
" + hubNotification.ListHeaderTitleColor + "
"," +
"" sound
":"
" + hubNotification.Sound + "
"," +
"" userProfileId
":"
" + hubNotification.ToUserProfileId.ToString().Trim() + "
"," +
"" type ":" sendfriend "}}";
outcome = hub.SendGcmNativeNotificationAsync(
jsonPayLoad, hubNotification.ToUserEmail)
.Result;
} else {
hubNotification.Sound = hubNotification.Sound + ".caf";
var registeredNewDevice =
hub.CreateAppleNativeRegistrationAsync(
hubNotification.DeviceId.Replace("-", ""),
new[]{hubNotification.ToUserEmail})
.Result;
new[] { hubNotification.ToUserEmail }).Result;
jsonPayLoad = "{ " aps ": " +
"{ " alert
": "
" + hubNotification.Message.Trim() + "
"," +
"" ListId
": "
" + hubNotification.ListId + "
"," +
"" notificationType
": "
" + hubNotification.NotificationType + "
"," +
"" IsOwner
": "
" + hubNotification.IsOwner + "
"," +
"" ListName
": "
" + hubNotification.ListName + "
"," +
"" ListType
": "
" + hubNotification.ListType + "
"," +
"" badge
": "
" + hubNotification.badge + "
"," +
"" InProgress
": "
" + hubNotification.InProgress + "
"," +
"" headercolor
": "
" + hubNotification.ListHeaderColor + "
"," +
"" titlecolor
": "
" + hubNotification.ListHeaderTitleColor + "
"," +
"" sound
":"
" + hubNotification.Sound + "
"," +
"" userProfileId
":"
" + hubNotification.ToUserProfileId.ToString().Trim() + "
"," +
"" type ":" sendfriend "}}";
outcome = hub.SendAppleNativeNotificationAsync(jsonPayLoad,
hubNotification.ToUserEmail)
.Result;
}
// check the response
if (outcome != null)
{
if (!((outcome.State == NotificationOutcomeState.Abandoned) ||
(outcome.State == NotificationOutcomeState.Unknown)))
{
// Message send successfully!!
}
}
}
//});
}
}
catch (Exception ex)
{
// LogError(ex.Message, "SendPushNotificationToDevice");
}
}
}
Chris LovePosted Nov 27, 2023, 2:56 PM
Hello,
The error message “The Push Notification System handle for the registration is invalid” indicates that the handle (also known as the device token or registration ID) used by the Notification Hub to send the push notification is not valid for the push notification service provider (such as APNS for Apple or FCM for Android devices).
Here are a few steps you can take to troubleshoot and resolve this issue:
1. Verify that the device registration IDs stored in your Notification Hub are up to date. Device registration IDs can expire or be refreshed by the device operating system. Your application should handle token refresh events and update the registration information in the Notification Hub correspondingly.
2. Ensure that the `hubNotification.DeviceId` you are using to register the device with the Notification Hub is in the correct format and is the correct token sent by the push notification service (APNS, FCM).
3. When creating the JSON payload for your push notification, ensure that it is properly formatted for the target platform. For example, the payload format for APNS is different from that of FCM. The JSON payload should also be properly escaped.
4. Check the platform-specific error codes provided by the push notification service (for APNS you can check the response headers for a `apns-id` and `apns-error-reason`, and for FCM the result will be within the response).
5. Review any registration deletion logic you have to make sure it is not deleting valid registrations. The logic in your `foreach` loop that deletes registrations can potentially remove the correct one if multiple devices are using the same tag.
6. Ensure that you handle asynchronous calls correctly. In your code, there are multiple `.Result` calls which can lead to deadlocks in ASP.NET environments. Consider using `await` instead of `.Result` to handle asynchronous calls properly.
7. Double-check the Notification Hub configuration on both the Azure portal and your app settings. The `HubConnectionString` and `HubName` must match what's configured in the Azure portal.
8. Look at exception details in your catch block to gather more information when the registration or notification sending fails. It's important to log the exception message and stack trace.
For any sensitive data or operations, always follow security best practices relevant to your application and infrastructure.
Best of luck with your debugging efforts, and let us know if you make any progress or if there's more we can help to troubleshoot.
Best regards.