apple push notification with APNS sharp
Asked Answered
S

2

3

i use APNS Sharp library for my apple push notification. i have downloded from Here.i use sample test program provided by APNS sharp library without any modification.
it simply does not send any notification until i put break point at that line of code. if i put break point. i just work fine.is this expected behaviour or i am doing something wrong. and also i am not getting any exception. thanks for any help. here is the code

static void Main(string[] args)
{
    bool sandbox = true;
    string testDeviceToken = "Token";
    string p12File = "apn_developer_identity.p12";
    string p12FilePassword = "yourpassword";
    int sleepBetweenNotifications = 15000;
    string p12Filename = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, p12File);
    NotificationService service = new NotificationService(sandbox, p12Filename, p12FilePassword, 1);
    service.SendRetries = 5; 
    service.ReconnectDelay = 5000; //5 seconds
    service.Error += new NotificationService.OnError(service_Error);
    service.NotificationTooLong += new NotificationService.OnNotificationTooLong(service_NotificationTooLong);
    service.BadDeviceToken += new NotificationService.OnBadDeviceToken(service_BadDeviceToken);
    service.NotificationFailed += new NotificationService.OnNotificationFailed(service_NotificationFailed);
    service.NotificationSuccess += new NotificationService.OnNotificationSuccess(service_NotificationSuccess);
    service.Connecting += new NotificationService.OnConnecting(service_Connecting);
    service.Connected += new NotificationService.OnConnected(service_Connected);
    service.Disconnected += new NotificationService.OnDisconnected(service_Disconnected);
    Notification alertNotification = new Notification(testDeviceToken);
    alertNotification.Payload.Alert.Body = "Testing {0}...";
    alertNotification.Payload.Sound = "default";
    alertNotification.Payload.Badge = i;
    if (service.QueueNotification(alertNotification))
      Console.WriteLine("Notification Queued!");
    else
      Console.WriteLine("Notification Failed to be Queued!");
    Console.WriteLine("Cleaning Up...");

    service.Close();// if i dont put a break point in here, it simply does not send any notification

    service.Dispose();

}

i hope my question is clear...
Update: i am stuck here.please any one can help me.

Sanguinaria answered 1/7, 2010 at 22:14 Comment(2)
just checking: You do have your cert, right? is your cert password 'yourpassword'? Just wondering if that's something that was left over from the example.Ratib
Thanks ACBurk. yes i have all. it just work fine if i put break point.Sanguinaria
S
2

i found out the problem. it was error in APNS SHARP library thread workflow.

EDIT:
i am calling this method after queueing all the notification.
like
service.start();
and here is method

     public void Send()
    {
        foreach (NotificationConnection conn in this.notificationConnections)
        {
           // Console.Write("Start Sending");
            conn.start(P12File, P12FilePassword);
        }
    }
Sanguinaria answered 11/7, 2010 at 2:21 Comment(3)
what do you mean? what error? im using some library code in vb.net to do this push to apns. and im getting "notification queued" but the notification never beeing delivered to the device...how is it possible?Dunlin
when you say NotificationService service = new NotificationService(), its starts another thred which reads from queue. but before you add any notification to queue that thred already terminated as there was no notification. make sense> if you put break point then listenr thread wont finish because it waiting on break point.Sanguinaria
basically i end up creating another method called "Send" in NotificationService which "starts" thread which sends out actual notification. this "start" method was called when you create NotificationService object. which was wrong. let me know if you need more explanation. i have also added codeSanguinaria
L
1

I am seeing this as well. Looking at the NotificationConnection.Close() method i found this:

// Sleep here to prevent a race condition // in which a notification could be queued while the worker thread // is sleeping after its loop, but if we set closing true within that 100 ms, // the queued notifications during that time would not get dequeued as the loop // would exit due to closing = true; // 250 ms should be ample time for the loop to dequeue any remaining notifications // after we stopped accepting above Thread.Sleep(250);

And in the loop mentioned i found: Thread.Sleep(500);

Setting the sleep time in the close method to 1000 fixed it for me ;) - answer from :http://code.google.com/p/apns-sharp/issues/detail?id=41

Lumen answered 5/4, 2011 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.