Export to GitHub

apns-sharp - issue #34

Notification wont be sent unless thread sleeps after sending it


Posted on Jun 1, 2010 by Quick Panda

What steps will reproduce the problem? 1. Fairly simple to reproduce, just replace the counter value to 1 in the NotificationTest distributed with the source, and you will be able to see this problem.

I tested this on a desktop computer with dsl connection to the internet.

Comment #1

Posted on Jun 1, 2010 by Quick Panda

Hey, nevermind, it was a case of closing the service before the connection was even open. Sorry for the fake issue.

Comment #2

Posted on Jun 2, 2010 by Happy Lion

No worries.

Comment #3

Posted on Jun 18, 2010 by Happy Lion

I have this issue also... calling service.Close(); does not wait for any queued notifications to be sent. This appears to only happen on the FIRST send of the application.

I've had to move service.Close(); to service_NotificationSuccess, although this doesn't really make sense if multiple messages are being sent.

To replicate: change the loop in JdSoft.Apple.Apns.Test.Program to for (int i = 1; i <= 1; i++) Comment the following line: System.Threading.Thread.Sleep(sleepBetweenNotifications);

Comment #4

Posted on Jun 18, 2010 by Happy Lion

To me, it appears a "correct" fix can be added in JdSoft.Apple.Apns.Notifications.NotificationConnection on line 280.

Current code: Thread.Sleep(250);

Fix: while (notifications.Count > 0) { Thread.Sleep(250); }

A helpful comment on that line says // 250 ms should be ample time for the loop to dequeue any remaining notifications

It appears 250ms might be marginally too small an amount, on average my system is taking 500 ms to dequeue the notifications, thought this loop should take care of it.

Comment #5

Posted on Jun 21, 2010 by Happy Lion

I've changed the code to:

int slept = 0; int maxSleep = 10000; //10 seconds

while (notifications.Count > 0 && slept++ <= maxSleep) Thread.Sleep(100);

This should be a safe way to wait until all are dequeued

Comment #6

Posted on Jun 21, 2010 by Happy Lion

Looks like a good check, though I believe maxSleep should be divided by 100 since the thread is sleeping (if the desired max is 10000ms).

int slept = 0; int maxSleep = 10000; //10 seconds

while (notifications.Count > 0 && slept++ <= (maxSleep / 100)) Thread.Sleep(100);

Comment #7

Posted on Jun 21, 2010 by Happy Lion

Ahh yes, thanks for catching that...

Comment #8

Posted on Aug 27, 2010 by Quick Hippo

Thank you, it helps me too. I receive nothing when I try only one notification... after following your instructions it works fine!

Status: Fixed

Labels:
Type-Defect Priority-Medium