关于APNSBroker队列通知推送尖锐的添加,该集合已被标记为完整

本文关键字:集合 记为 添加 通知 队列 APNSBroker 关于 | 更新日期: 2023-09-27 18:14:43

Pushsharp 4.0版本

我得到一个异常"System. "InvalidOperationException:该集合已被标记为"关于添加的完整"。搜索了几个小时后,我知道这是因为队列通知在第二次调用时不接受相同的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. log。InvalidOperationException:关于添加,集合已被标记为完成。异常堆栈: System.Collections.Concurrent.BlockingCollection 1.TryAddWithNoTimeValidation(T item, Int32 millisecondsTimeout, CancellationToken cancellationToken) at PushSharp.Core.ServiceBrokerQueueNotification (TNotification通知)

我的项目中的类

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

我看到了这个答案,但没有帮助。因为我使用的是Push Sharp Nuget Package。我也试着联系了push sharp的作者。到目前为止还没有回应。谁有同样的问题,请评论。

的问候Ammad

关于APNSBroker队列通知推送尖锐的添加,该集合已被标记为完整

异常神奇地消失了。我手动下载了push sharp的源代码。而不是针对Nuget包。我指的是尖刀。核心和推尖。苹果从解决方案,我无法重现同样的问题。解决方案工作良好,没有当地项目参考解决方案的问题。我想可能是它的问题与pushsharp Nuget包。

然后我删除pushsharp。核心和推尖。苹果项目的解决方案,然后再次下载并引用PushSharp的核包。这一次它工作得很好,没有上面的异常"系统"。InvalidOperationException:该集合已被标记为完成,并添加了".