You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the OnNotificationFailed(ApnsNotification, AggregateException) event of the Apns broker, I often get this exception:
PushSharp.Apple.ApnsNotificationException: Apns notification error: 'ConnectionError'
---> System.NullReferenceException: Object reference not set to an instance of an object.
at PushSharp.Apple.ApnsConnection.createBatch(List`1 toSend)
at PushSharp.Apple.ApnsConnection.d__21.MoveNext()
--- End of inner exception stack trace ---
at PushSharp.Apple.ApnsServiceConnection.d__2.MoveNext()
It seems to happen randomly. I'm using PushSharp in a Windows Service, running multiple (>300) brokers in the same time stored in a singleton object's ConcurrentDictionary, if that helps.
Beside this, sometimes I get the following exception, also in OnNotificationFailed:
System.ArgumentException: Source array was not long enough. Check srcIndex and length, and the array's lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Collections.Generic.Queue1.SetCapacity(Int32 capacity) at System.Collections.Generic.Queue1.Enqueue(T item)
at PushSharp.Apple.ApnsConnection.Send(CompletableApnsNotification notification)
at PushSharp.Apple.ApnsServiceConnection.d__2.MoveNext()
Am I doing something wrong or misunderstanding the concept, or could this happen due to a bug?
Same happens for me.
It reproduces even with 1 broker, just not so often.
Looks like problem is in ApnsConnection class: notifications queue ( Queue<CompletableApnsNotification> notifications ) isn't thread-safe. I think this queue isn't thread-safe because of timerBatchWait timer: timer's callback executes on a new thread, not the broker thread.
If you for example place locks around all places where notifications.Enqueue/notifications.Dequeue is called (lock (notificationBatchQueueLock)), error doesn't seem to reproduce anymore. Should ConcurrentQueue be used instead of Queue here?
@iinke I have a lock statement around the method that it is queening the device tokens. I am also creating batches and Queue device tokens in a timer. So in each 3 seconds I am queering 100 devices. But I am still getting this connection error.
I too have the same issue : Connection Error
I recently updated pushsharp library to 4.0.10, if i put thread.sleep(1000) in for loop notifications will be sent but it's getting late. Because I need to send notifications to thousands of device tokens. Even for 10 device tokens also not sending without thread.sleep(1000).
Please suggest me what's wrong with my code.
Here is my code snippet.
public static void SendNotifications(List currentBrandNotications, long brandId)
{
byte[] appleCert = null;
string p12File = @"aps_production_brand" + brandId.ToString().Trim() + ".p12";
try
{
appleCert = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "P12\\" + p12File));
}
catch (Exception ex)
{
logger.Debug("P12 certificate is not avilable for BrandId: " + brandId);
}
try
{
logger.Debug(" Send PushNotifications To Apple :- ");
if (appleCert != null)
{
// Configuration
var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production, appleCert, currentBrandNotications[0].P12Password);
// Create a new broker
var apnsBroker = new ApnsServiceBroker(config);
var fbs = new FeedbackService(config);
// Wire up events
apnsBroker.OnNotificationFailed += (Notification, aggregateEx) =>
{
//ScheduledNotification ScheduledNotification = new InstantPNScheduler.ScheduledNotification();
aggregateEx.Handle(ex =>
{
// See what kind of exception it was to further diagnose
if (ex is ApnsNotificationException)
{
var notificationException = (ApnsNotificationException)ex;
// Deal with the failed notification
var apnsNotification = notificationException.Notification;
var statusCode = notificationException.ErrorStatusCode;
logger.Debug("Apple Notification Failed: ID=" + apnsNotification.Identifier + " Code=" + statusCode);
}
else
{
// Inner exception might hold more useful information like an ApnsConnectionException
logger.Debug(ex.InnerException.ToString());
}
// Mark it as handled
return true;
});
};
apnsBroker.OnNotificationSucceeded += (Notification) =>
{
logger.Debug("Apple Notification Sent!");
};
// Start the broker
apnsBroker.Start();
foreach (ScheduledNotification notification in currentBrandNotications)
{
try
{
//logger.Debug("iOS Device token=" + notification.DeviceToken); apnsBroker.QueueNotification(new ApnsNotification
{
DeviceToken = notification.DeviceToken,
Payload = JObject.Parse("{\"aps\":{\"alert\":\"" + notification.Message + "\",\"badge\":1,\"sound\":\"sound.caf\",\"BrandId\":\"" + brandId.ToString() + "\",\"notificationType\":\"Basic\",\"DeviceType\":\"" + notification.DeviceType + "\",\"DeviceToken\":\"" + notification.DeviceToken + "\",\"NotificationId\":\"" + notification.NotificationId + "\"}}")
});
}
Thread.Sleep(800);
}
catch (Exception ex)
{
logger.Debug(" SendPushNotificationToApple :- " + ex.Message);
}
}
// Stop the broker, wait for it to finish
// This isn't done after every message, but after you're
// done with the broker
apnsBroker.Stop();
}
}
catch (Exception ex)
{
logger.Debug("Error" + ex.Message);
}
finally
{
//apnsBroker = null;
}
}
@iinke any updates on this? I experienced this when dealing with a larger batch of notifications. I open the broker and send notification on a separate thread. Could this be the issue? Do you suggest I run the service on the main thread? The following are exceptions logged:
Initially one for every notification:
Source array was not long enough. Check srcIndex and length, and the array's lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable) at System.Collections.Generic.Queue1.SetCapacity(Int32 capacity) at System.Collections.Generic.Queue1.Enqueue(T item) at PushSharp.Apple.ApnsConnection.Send(CompletableApnsNotification notification) at PushSharp.Apple.ApnsServiceConnection.<Send>d__2.MoveNext()
followed by a bunch of:
The ApnsNotificationErrorStatusCode: ConnectionError = 254,
@Redth Just checking if there are any updates on this issue. I am experiencing this even with a single broker. Would be great to understand what is going on. Any help would be appreciated. Thanks
Reacted by Matthew, Joan Gerard, onlifesaiprasad, attilabicsko, Bae Junehyeon, luidiva and Petr Zdarsky
It showed the error "Missing Push Token". And because I had a device without a (valid) pushtoken on top of my queue, the APNS server disconnected (all) connection(s) without question.
I am cleaning the expired devices using the FeedbackService class before I
add any to the queue. Is there an extra check I need to make? How do you
check the validity?
Hmm, ok so you had an empty device token for the device. I guess I'll have to enable the log to monitor what the issue is in my case as I cannot see a reason why my token would be empty or null. I only insert them when the user registers for push notifications from the device. So you used the following to enable logging...
I am getting frequently connection error while sending Push to iOS.
"Failed to Connect, check your firewall settings!
at PushSharp.Apple.ApnsConnection.d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PushSharp.Apple.ApnsConnection.d__21.MoveNext()
"
Sometime it send and sometime it gives error.
Hello, I seem to be getting the same problem; I send 100 notifications and nothing is wrong, but when one of the tokens is wrong, Ialso get this exception often:
PushSharp.Apple.ApnsNotificationException: Apns notification error: 'ConnectionError'
Will insert code but keep in mind a lot of variables are useless, just using them to try to catch the problem...
`using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Text;
//using JdSoft.Apple.Apns.Feedback;
using PushSharp.Apple;
//using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
fixed this problem by setting config.internalBatchSize = int.MaxValue;
I think the notification queue is not thread safe and when notifications are divided into batches, adding them to the queue raises this exception
Maybe this problem can occur when the token is in invalid format.
The token must be with Alphanumerics characters only, no white spaces or something else..
It works for me.
Activity
attilabicsko commentedon Apr 1, 2016
Beside this, sometimes I get the following exception, also in OnNotificationFailed:
System.ArgumentException: Source array was not long enough. Check srcIndex and length, and the array's lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Collections.Generic.Queue
1.SetCapacity(Int32 capacity) at System.Collections.Generic.Queue1.Enqueue(T item)at PushSharp.Apple.ApnsConnection.Send(CompletableApnsNotification notification)
at PushSharp.Apple.ApnsServiceConnection.d__2.MoveNext()
Am I doing something wrong or misunderstanding the concept, or could this happen due to a bug?
iinke commentedon Apr 4, 2016
Same happens for me.
It reproduces even with 1 broker, just not so often.
Looks like problem is in
ApnsConnectionclass: notifications queue (Queue<CompletableApnsNotification> notifications) isn't thread-safe. I think this queue isn't thread-safe because oftimerBatchWaittimer: timer's callback executes on a new thread, not the broker thread.If you for example place locks around all places where
notifications.Enqueue/notifications.Dequeueis called (lock (notificationBatchQueueLock)), error doesn't seem to reproduce anymore. Should ConcurrentQueue be used instead of Queue here?mohammadjouhari commentedon Apr 10, 2016
any update on this. I am getting the same issue right here.
mohammadjouhari commentedon Apr 11, 2016
@iinke I have a lock statement around the method that it is queening the device tokens. I am also creating batches and Queue device tokens in a timer. So in each 3 seconds I am queering 100 devices. But I am still getting this connection error.
quick-fix for issue Redth#672
gabrieligbastos commentedon Apr 17, 2016
Didnt get it. Is it a bug or am I also doing something wrong?
mohammadjouhari commentedon Apr 18, 2016
@gabrieligbastos it is a bug. Why this error is coming ?
gabrieligbastos commentedon Apr 18, 2016
I have no clue why? It is just sometimes it just give me back this exception. Seems randomly, Could not find a situation that always happens.
Before update my NugetPackage to the new version, it was not getting this error.
gabrieligbastos commentedon May 3, 2016
Any update to workaround this?
sparity123 commentedon May 24, 2016
I too have the same issue : Connection Error
I recently updated pushsharp library to 4.0.10, if i put thread.sleep(1000) in for loop notifications will be sent but it's getting late. Because I need to send notifications to thousands of device tokens. Even for 10 device tokens also not sending without thread.sleep(1000).
Please suggest me what's wrong with my code.
Here is my code snippet.
public static void SendNotifications(List currentBrandNotications, long brandId)
{
byte[] appleCert = null;
Thanks in advance.
lukaspechar commentedon Jun 30, 2016
@iinke any updates on this? I experienced this when dealing with a larger batch of notifications. I open the broker and send notification on a separate thread. Could this be the issue? Do you suggest I run the service on the main thread? The following are exceptions logged:
Initially one for every notification:
Source array was not long enough. Check srcIndex and length, and the array's lower bounds.at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable) at System.Collections.Generic.Queue1.SetCapacity(Int32 capacity) at System.Collections.Generic.Queue1.Enqueue(T item) at PushSharp.Apple.ApnsConnection.Send(CompletableApnsNotification notification) at PushSharp.Apple.ApnsServiceConnection.<Send>d__2.MoveNext()followed by a bunch of:
The ApnsNotificationErrorStatusCode: ConnectionError = 254,lukaspechar commentedon Jul 27, 2016
@Redth Just checking if there are any updates on this issue. I am experiencing this even with a single broker. Would be great to understand what is going on. Any help would be appreciated. Thanks
dehaaneric commentedon Oct 4, 2016
I had this problem too. I solved it by checking the Push sharp (verbose) logging by setting:
It showed the error "Missing Push Token". And because I had a device without a (valid) pushtoken on top of my queue, the APNS server disconnected (all) connection(s) without question.
lukaspechar commentedon Oct 4, 2016
@tweek82 Thanks for your reply. How did you dequeue the device?
dehaaneric commentedon Oct 4, 2016
I check the push token before I add it to the queue. When token is invalid I directly update the devive in my database by just deleting it.
lukaspechar commentedon Oct 4, 2016
I am cleaning the expired devices using the FeedbackService class before I
add any to the queue. Is there an extra check I need to make? How do you
check the validity?
On 4 October 2016 at 16:22, tweek82 notifications@github.com wrote:
dehaaneric commentedon Oct 4, 2016
Nothing fancy, just check IsNullOrWhitespace before queue'ing. Like this:
lukaspechar commentedon Oct 4, 2016
Hmm, ok so you had an empty device token for the device. I guess I'll have to enable the log to monitor what the issue is in my case as I cannot see a reason why my token would be empty or null. I only insert them when the user registers for push notifications from the device. So you used the following to enable logging...
I'll give it a go. Thanks for your help and swift replies 👍
dehaaneric commentedon Oct 4, 2016
No problem. Please share any of your apns-troubles and solutions. It might help me and others too.
lukaspechar commentedon Oct 5, 2016
Will do, once again thanks for your help @tweek82
bidianqing commentedon May 12, 2017
Apns notification error: 'ConnectionError'
bidianqing commentedon May 12, 2017
IIS Express is Succeeded and IIS is Failed
faisalcse commentedon Jul 11, 2017
I am getting frequently connection error while sending Push to iOS.
"Failed to Connect, check your firewall settings!
at PushSharp.Apple.ApnsConnection.d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PushSharp.Apple.ApnsConnection.d__21.MoveNext()
"
Sometime it send and sometime it gives error.
Please advice on resolution.
Thanks in Advance.
jimenaju commentedon Jul 17, 2017
Hello, I seem to be getting the same problem; I send 100 notifications and nothing is wrong, but when one of the tokens is wrong, Ialso get this exception often:
PushSharp.Apple.ApnsNotificationException: Apns notification error: 'ConnectionError'
Will insert code but keep in mind a lot of variables are useless, just using them to try to catch the problem...
`using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Text;
//using JdSoft.Apple.Apns.Feedback;
using PushSharp.Apple;
//using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace PlaneSeats
{
public class APNS_Server
{ //doubts with Code, refer to:
//https://stackoverflow.com/questions/37045299/issue-with-pushsharp-apple-notification-from-version-4-0-10-0-to-testflight/37133379
//--------------------------------FOR LOOP. STARTS HERE--------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------#763
//--------------------------------FOR LOOP. ENDS HERE--------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
}
}
}
`
Also... if someone could tell me if the order is correct... I find it reaaally hard to understand as I´m new to... everything coding TT_TT
Thanks!
andrei1152 commentedon Aug 17, 2017
fixed this problem by setting config.internalBatchSize = int.MaxValue;
I think the notification queue is not thread safe and when notifications are divided into batches, adding them to the queue raises this exception
andersemil commentedon Oct 13, 2017
Same issue here, I think I might have fixed it by locking notificationBatchQueueLock around the dequeueing, like this:
EduardoKSibata commentedon Jan 10, 2018
Maybe this problem can occur when the token is in invalid format.
The token must be with Alphanumerics characters only, no white spaces or something else..
It works for me.
fatim commentedon Aug 23, 2018
I get the same error running the service in Azure WebApp. Same code works fine on my laptop.
refs #15264 Implemented locking for notifications Queue in ApnsConnec…