关于 APNSBroker Queue Notification Push Sharp 中的添加,该集合已标记为完整

The collection has been marked as complete with regards to additions in APNSBroker Queue Notification Push Sharp

Pushsharp 版本 4.0

我遇到异常 "System.InvalidOperationException: The collection has been marked as complete with regards to additions"。搜索了几个小时后,我才知道这是因为队列通知在第二次调用时不接受相同的 ID。我的问题是,如果此服务是 运行,我会向 User-token-1 发送通知,几秒钟后,如果我将通知发送给具有相同 User-token-1 的同一用户,则会出现异常。

要产生此问题,只需向同一用户发送两次通知但两次调用此代码函数,而不是在单个自定义消息中。我发送了两次,因为我需要向同一用户发送多个通知,但不是在单个推送消息中而是在不同的推送消息中。要了解如何重现此错误,请了解以下情况并忽略我的自定义变量。即使我忽略了这一点,比如只调用 SendPushMessageToEmployees 一次,然后如果我需要在几分钟后在同一个 运行 服务上向同一个用户发送通知,那么我将得到同样的异常。

main()
{
          SendPushMessageToEmployees(MyCustomMessage);
          SendPushMessageToEmployees(MyCustomMessage);
}

 public void SendNotification(List<PushMessage> pushMessages)
        {
            try 
            {
                apnsBroker.Start();
                foreach (PushMessage message in pushMessages)
                {
                    if (!String.IsNullOrEmpty(message.DeviceId) )
                    {
                        apnsBroker.QueueNotification(new ApnsNotification
                        {
                            DeviceToken = message.DeviceId,
                            Payload = JObject.Parse("{\"aps\":{\"badge\":0,\"alert\":'" + message.Message + "',\"Workflow\":'" + message.Message + "'}}")
                        });
                    }
                }
                apnsBroker.Stop(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0}",ex.Message);
            }
        }

Exception : System.InvalidOperationException: 该集合已标记为已完成添加。 堆栈跟踪: System.Collections.Concurrent.BlockingCollection1.TryAddWithNoTimeValidation(T item, Int32 millisecondsTimeout, CancellationToken cancellationToken) at PushSharp.Core.ServiceBroker1.QueueNotification(TNotification 通知)

Class 在我的项目中

public  class  AppleServiceBroker : IServiceBroker 
{
    private ApnsServiceBroker apnsBroker;
    public AppleServiceBroker()
    {
        if (apnsBroker == null)
        {
           var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox,
             "PushNotification.p12", "MyPassword");

            apnsBroker = new ApnsServiceBroker(config);

            // Wire up events
            apnsBroker.OnNotificationFailed += (notification, aggregateEx) =>
            {
                aggregateEx.Handle(ex =>
                {
                    if (ex is ApnsNotificationException)
                    {
                        var notificationException = (ApnsNotificationException)ex;
                        var apnsNotification = notificationException.Notification;
                        var statusCode = notificationException.ErrorStatusCode;
                        Console.WriteLine("Apple Notification Failed:  ID= " + apnsNotification.Identifier + " Code= " + statusCode);
                    }
                    else
                    {
                        Console.WriteLine("Apple Notification Failed for some unknown reason : " + ex.InnerException);
                    }

                    return true;
                });
            };
        }
    }


    /// <summary>
    /// Sends notification to apple servers
    /// </summary>
    /// <param name="pushMessages">List of push messages</param>
    public void SendNotification(List<PushMessage> pushMessages)
    {
        try 
        {
            apnsBroker.OnNotificationSucceeded += (notification) =>
            {
                Console.WriteLine("Apple Notification Sent!");
            };
            apnsBroker.Start();

            foreach (PushMessage message in pushMessages)
            {
                if (!String.IsNullOrEmpty(message.DeviceId) )
                {
                    apnsBroker.QueueNotification(new ApnsNotification
                    {
                        DeviceToken = message.DeviceId,
                        Payload = JObject.Parse("{\"aps\":{\"badge\":0,\"alert\":'" + message.Message + "',\"Workflow\":'" + message.Message + "'}}")
                    });
                }

            }
            apnsBroker.Stop(true);

        }
        catch (Exception ex)
        {
            Console.WriteLine("{0}",ex.Message);
        }
    }
}

`

我看过这个 answer 但没有帮助。因为我正在使用 Push Sharp Nuget 包。我也试着联系了 push sharp 的作者。至今没有回应。有同样问题的朋友请评论。

问候 阿玛德

异常神奇地消失了。我手动下载了push sharp的源码。而不是针对 Nuget 包。我从解决方案中引用了 pushsharp.core 和 pushsharp.Apple,但无法重现相同的问题。该解决方案运行良好,解决方案中的本地项目参考没有问题。我想可能是 pushsharp Nuget 包的问题。

然后我从解决方案中删除了 pushsharp.core 和 pushsharp.Apple 项目,然后再次下载并引用了 PushSharp nuget 包。这次它工作正常,没有上述异常 "System.InvalidOperationException: The collection has been marked as complete with regards to additions".