本文介绍了关于 APNSBroker 队列通知推送夏普中的添加,该集合已被标记为完整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pushsharp 4.0 版

Pushsharp version 4.0

我收到一个异常System.InvalidOperationException:集合已被标记为关于添加的完整".搜索了几个小时后,我才知道这是因为队列通知在第二次调用时不接受相同的 Id.我的问题是,如果此服务正在运行,我会向 User-token-1 发送通知,几秒钟后,如果我将通知发送给具有相同 User-token-1 的同一用户,则会收到异常.

I am getting an exception "System.InvalidOperationException: The collection has been marked as complete with regards to additions". After searching for hours I came to know this is because the queue notification do not accepts the same Id when its called 2nd time. My problem is let say if this service is running I send a notification to User-token-1 and after few seconds if I send the notification to same user with same User-token-1 then I get the exception.

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

To produce this issue just send the notification two times to same user but calling this code function two times not in single custom message. I am sending it two times because I have a requirement to send multiple notification to same user but not in single push message but a different push message. To give an idea how this error can be reproduces please understand the following situation and please ignore my custom variables. Even though if I ignore this like calling SendPushMessageToEmployees only one time then later if I need to send notification to same user after few minutes on same running service then I will get same exception.

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

异常:System.InvalidOperationException:集合已被标记为关于添加的完整.堆栈跟踪:System.Collections.Concurrent.BlockingCollection1.TryAddWithNoTimeValidation(T item, Int32 millisecondsTimeout, CancellationToken cancelationToken) at PushSharp.Core.ServiceBroker1.QueueNotification(TNotification notification)

Exception : System.InvalidOperationException: The collection has been marked as complete with regards to additions.StackTrace:System.Collections.Concurrent.BlockingCollection1.TryAddWithNoTimeValidation(T item, Int32 millisecondsTimeout, CancellationToken cancellationToken) at PushSharp.Core.ServiceBroker1.QueueNotification(TNotification notification)

我项目中的课程

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.我也尝试联系推锐作者.至今没有任何回应.有同样问题的朋友请留言.

I have seen this answer but not help. As I am using Push Sharp Nuget Package. I also tried to contact push sharp author. No response so far. Anyone having same problem please comment.

问候阿玛德

推荐答案

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

The exception magically disappeared. I manually downloaded the source code of push sharp. Instead of targeting Nuget package. I reference the pushsharp.core and pushsharp.Apple from the solution and I was unable to reproduce the same issue. The solution was working fine without issues with local project reference in solution. I thought may be its problem with pushsharp Nuget package.

然后我从解决方案中删除了 pushsharp.core 和 pushsharp.Apple 项目,然后再次下载并引用了 PushSharp nuget 包.这次它运行良好,没有出现上述异常System.InvalidOperationException:集合已被标记为关于添加的完整".

Then I delete the pushsharp.core and pushsharp.Apple projects from solution and then again downloaded and referenced the PushSharp nuget package. This time it worked fine without the above exception "System.InvalidOperationException: The collection has been marked as complete with regards to additions".

这篇关于关于 APNSBroker 队列通知推送夏普中的添加,该集合已被标记为完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:07