Azure / azure-notificationhubs-dotnet

.NET SDK for Azure Notification Hubs
MIT License
70 stars 119 forks source link

The Push Notification System handle for the registration is invalid #300

Open vijaypratap845 opened 7 months ago

vijaypratap845 commented 7 months ago

Query/Question The Push Notification System handle for the registration is invalid

Why is this not a Bug or a Feature Request? 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 the result as '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 attaching the code below. 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"); } } }

endregion

`