【问题标题】:PushSharp Apple - The message received was unexpected or badly formattedPushSharp Apple - 收到的消息出乎意料或格式错误
【发布时间】:2014-05-31 16:15:17
【问题描述】:

我使用的是 PushSharp 2.1.2 版。该应用程序是 .NET 4.5.1(尽管我也尝试过针对 .NET 4.5 和 .NET 4)

我正在尝试通过 Apple APNS 沙盒发送推送消息,但未成功。

我正在使用提供的 PHP 脚本成功发送消息 here on Ray Wenderlich's walkthrough 使用相同的证书并发送到与我的 PushSharp 应用程序相同的设备 ID。

我已经通过从密钥链中将完成的证书导出为 p12 进行了测试。导出完成的证书和密钥。导出私钥。也可以通过here使用的方法。当我将证书和密钥结合起来用于 PHP 脚本时,我没有任何问题。

我已将 p12 证书导入到我测试过的机器上 - 似乎没有什么区别。

我尝试在将苹果推送服务注册到推送代理时更改 IsProduction 标志。将其设置为生产时没有错误(即使这是一个沙盒证书),但在这种情况下它显然也无法进入设备。

我的所有消息都不会通过,都会收到如下所示的服务异常:

System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The message received was unexpected or badly formatted
 --- End of inner exception stack trace ---
at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at PushSharp.Apple.FeedbackService.Run(ApplePushChannelSettings settings, CancellationToken cancelToken)
at PushSharp.Apple.ApplePushService.<>c__DisplayClass4.<.ctor>b__1(Object state)

这基本上是我的代码的样子:

var push = new PushBroker();
// register event handlers for channel create/destroy/exception, notificationrequeue, serviceexception, notification sent

var appleCert = File.ReadAllBytes(ConfigurationManager.AppSettings["CertAddress"]);
push.RegisterAppleService(new ApplePushChannelSettings(false, appleCert, ConfigurationManager.AppSettings["CertPassword"]));

var pn = new AppleNotification().ForDeviceToken(item.SendToDeviceIdentifier).WithAlert(item.AlertMessage).WithBadge(item.Badges);

push.QueueNotification(pn);

我调用了通道启动事件,然后调用了服务异常。

一些相关问题提到此错误可能与防火墙问题有关 - 我已经在 2 个能够发送推送通知的不同网络中测试了我的应用程序(其中 1 个当前正在使用 PushSharp 应用程序)。

任何见解将不胜感激。

【问题讨论】:

标签: c# apple-push-notifications pushsharp


【解决方案1】:

使用现已弃用的 APNS-Sharp 库(PushSharp 的祖先)时,我们遇到了同样的问题。我提交了一个针对 APNS-Sharp 的拉取请求,该请求根据我的测试解决了这个问题。

修改是为了改变(在 ApplePushChannel.cs 中)

stream.AuthenticateAsClient(this.appleSettings.Host, this.certificates, System.Security.Authentication.SslProtocols.Ssl3, false);                   

stream.AuthenticateAsClient(this.appleSettings.Host, this.certificates, System.Security.Authentication.SslProtocols.Tls, false);

我没有找到对此的确认,但沙盒 APNS 似乎不再支持 SSL3 协议。与报告此问题的其他人一样,我针对生产 APNS 的通知仍然有效。

你可以在这里找到拉取请求:

https://github.com/Redth/PushSharp/pull/369/files

更新

Apple Developer 网站上有一个关于此主题的帖子:

https://devforums.apple.com/thread/224320?tstart=0

但是,那里的一些人也在这个线程或 github 线程上。所以信息肯定是有偏差的。我在 Apple 的一位联系人说:

虽然目前还没有官方文档,但似乎 APNS 正在转向 TLS 而不是 SSL(仅基于看到这种变化 - 我没有听到任何官方的消息)。

【讨论】:

  • 谢谢!您的拉取请求正是我所做的更改,并且现在可以使用。
  • 我运气不好。我已经编译了当前版本,因为 nuget 包尚未更新,并且我遇到与以前相同的错误...... HRESULT 错误代码是-2146233087。我不知道这是否有帮助...
  • 我尝试点击苹果开发者论坛的链接来解释从 SSL 到 TLS 的重点,它刚刚转到我的会员中心主页。
  • 我也不走运。我从 github 下载了 Moon-APNS github.com/arashnorouzi/Moon-APNS 的源代码,尝试推出我自己的通知应用程序,并在该解决方案的同一行代码中遇到了相同的错误。我试过System.Security.Authentication.SslProtocols.TlsSystem.Security.Authentication.SslProtocols.Ssl3System.Security.Authentication.SslProtocols.Default
  • 我今天遇到了同样的问题,通过上面的修复解决了;但是我觉得这很奇怪。有谁知道这是否已经记录在两者之间?
【解决方案2】:

如果你们中的任何人在 Windows Server 2003 上运行此补丁(我知道,我知道),您将不得不运行此补丁,否则即使您实施了修复程序,您仍然会遇到奇怪的错误。我花了几个小时想知道为什么我的 2008 年的服务器可以工作,而我的 2003 年的服务器却没有。

http://support.microsoft.com/kb/948963

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多