苹果推送通知与APNS Sharp

3

我使用APNS Sharp库来进行苹果推送通知。我从这里下载了该库。我使用APNS sharp库提供的示例测试程序,没有做任何修改。
直到我在代码的那一行设置断点之前,它根本不会发送任何通知。如果我设置了断点,它就可以正常工作。这是预期的行为吗?还是我做错了什么?而且我也没有收到任何异常。感谢任何帮助。 以下是代码:

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

}

我希望我的问题已经很清楚了…
更新:我遇到了困难,请有能力的人来帮助我。

只是检查一下:你有证书吗?你的证书密码是'yourpassword'吗?只是想知道这是否是从示例中遗留下来的东西。 - ACBurk
谢谢ACBurk。是的,我全都有。如果我设置断点,它就可以正常工作。 - Nnp
2个回答

2
我找到了问题所在。这是APNS SHARP库线程工作流中的错误。
编辑:
我在将所有通知排队后调用此方法。
类似于
service.start();
这是方法:
     public void Send()
    {
        foreach (NotificationConnection conn in this.notificationConnections)
        {
           // Console.Write("Start Sending");
            conn.start(P12File, P12FilePassword);
        }
    }

你的意思是什么?出现了什么错误?我正在使用VB.NET中的一些库代码来推送到APNS。我收到了“通知已排队”的消息,但通知从未被传递到设备...这怎么可能? - Itay Levin
1
当你说NotificationService service = new NotificationService()时,它将启动另一个线程来从队列中读取。但是,在您添加任何通知到队列之前,该线程已经终止,因为没有通知。有意义吗?如果您设置断点,则监听器线程不会完成,因为它正在等待断点。 - Nnp
基本上,我最终创建了另一个名为“Send”的方法在NotificationService中,该方法“启动”线程以发送实际通知。当您创建NotificationService对象时,会调用此“start”方法,这是错误的。如果需要更多解释,请告诉我。我也已经添加了代码。 - Nnp

1

我也遇到了这个问题。查看NotificationConnection.Close()方法,我发现了以下内容:

// 在此处休眠以防止竞争条件 //其中通知可能在工作线程睡眠之后排队, //但是如果我们在那100ms内设置了closing为true, //则该时间段内排队的通知将不会被出队,因为循环将退出由于closing=true; // 250毫秒应足够循环在我们停止接受以上剩余的任何通知后出列 Thread.Sleep(250);

而在提到的循环中,我发现: Thread.Sleep(500);

将关闭方法中的休眠时间设置为1000,这个问题就解决了;) - 答案来源:http://code.google.com/p/apns-sharp/issues/detail?id=41


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接