Vijay Pratap Singh

Vijay Pratap Singh

  • 327
  • 5.1k
  • 483.8k

The push notification system handle for the registration is invalid

Nov 27 2023 9:59 AM

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");
        }
    }
}

 


Answers (1)