2

我正在尝试通过 PushSharp 向 IOS 设备发送推送通知。对于安卓,它可以工作。对于 IOS,调用StopAllServices()永远挂起,而不调用任何异常处理程序。

问题可能是我获得了一个 .pem 证书文件,而 pushsharp 需要一个 .p12 文件吗?

代码如下:

var br = new PushBroker();
br.OnNotificationSent += br_OnNotificationSent;
br.OnNotificationFailed += br_OnNotificationFailed;
br.OnChannelException += br_OnChannelException;
br.OnServiceException += br_OnServiceException;
br.OnDeviceSubscriptionChanged += br_OnDeviceSubscriptionChanged;
br.OnDeviceSubscriptionExpired += br_OnDeviceSubscriptionExpired;
br.OnChannelCreated += br_OnChannelCreated;
br.OnChannelDestroyed += br_OnChannelDestroyed;

var appleCert = Resource1.ck; // this is a pem file, not a p12 file!!! could this be the problem?
var sandbox = true;
br.RegisterAppleService(new ApplePushChannelSettings(!sandbox, appleCert, "223684"));
// password given to me by ios developer

var deviceIds = new string[] { "09eddcb8b89494adf802a0caf97d5daaa789a53f52d8c544dbdcf39f2c0b619a" };

foreach (var did in deviceIds)
{
    br.QueueNotification(
         new AppleNotification()
               .ForDeviceToken(did)//the recipient device id
               .WithAlert("test: " + DateTime.Now.ToString())//the message
               .WithBadge(1)
               .WithSound("sound.caf"));
}
br.StopAllServices(waitForQueuesToFinish: true); // hangs forever, no callbacks are called

截至昨天,我正在使用通过 Git 获取的 PushSharp,并由我自己使用 Visual Studio 2013 编译。

如果代码位于控制台应用程序和 asp.net 应用程序中,则会发生挂起。

我正在使用沙盒,因为有人告诉我这样做。如果我使用生产服务器,我会收到一个异常,告诉我证书是用于沙盒的。

感谢您提供有关冻结原因的任何提示。

4

4 回答 4

4

我们花了一整天的时间来猜测问题所在! 最后它是错误的 Newtonsoft.Json 版本 我们解决方案中的一些项目依赖于这个库的旧版本,结果我们运气不好在 Web 项目的 /bin 文件夹中得到错误的版本。

于 2015-06-04T17:08:17.787 回答
1

You can wait few seconds for br_OnNotificationFailed or any other event probably. It should contain some error description.

Nevertheless, I've found out PushSharp has strict requirements about certificates usage. PEM should be OK but it is not enough, even if you import it from file - you should have all necessary certificates in Windows certificates store (pem itself and its dependecies):

  1. Import your PEM to Local Machine\Root storage and give read access rights of its private key to the user of your running application

  2. Import from Apple site certificates Apple Worldwide Developer Relations Certification Authority and Apple Root CA into Local Machine\Trusted Root Certification Authorities

  3. Import Entrust Secure CA certificate (for SSL as described in iOS Developer Library) into Local Machine\Trusted Root Certification Authorities

于 2014-07-02T19:06:21.200 回答
1

ASP.NET 应用程序不是使用 PushSharp 的理想场所。如果可能的话,您最好使用 Windows 服务或其他一些基础设施。原因是在 ASP.NET 应用程序中,应用程序池 (AppPool) 可以在您身上重新启动,并且通常不受您的直接控制,这意味着 PushSharp 可能正在发送的所有排队通知可能会丢失,如果PushSharp 没有被优雅地清理。

如果您必须在 ASP.NET 应用程序中运行 PushSharp,最好的方法是在您的 Global.asax 文件中创建一个单例 PushBroker 实例。您应该在 Web 应用程序的整个生命周期内保留这个单例实例,包括在应用程序结束时调用 pushBroker.StopAllServices()(global.asax 中的 Application_End)。

您可以通过持久保存您希望以其他方式发送的通知,并且仅在触发 OnNotificationSent 事件后将它们从该持久存储中删除,来帮助减轻由于不可预见的应用程序池终止或重新启动而导致的消息丢失。这仍然不是完美的(您可能会冒多次通知传递的风险),但对于大多数人来说可能已经足够了。

您不应该在每次发送通知时创建和销毁 PushBroker 实例,因为这会使用不必要的资源,并且如果您使用的是 Apple APNS,它们会要求您在发送通知时尽可能长时间地保持与服务器的连接。您还应该在 Global.asax 的 Application_Ended 事件中调用 pushBroker.StopAllServices()。请记住,PushSharp 有效。

参考:https ://github.com/Redth/PushSharp/issues/240

于 2014-10-14T06:24:58.203 回答
1

最后是证书问题。PushSharp 不接受给我的 .pem。仅当我获得使用本指南创建的 .p12 时

https://code.google.com/p/apns-sharp/wiki/HowToCreatePKCS12Certificate

,问题解决了。

但是,PushSharp 应该引发异常而不是挂起。

于 2014-07-04T07:35:00.193 回答