IOS推送通知无法使用PushSharp

时间:2018-05-16 15:03:59

标签: c# ios push-notification apple-push-notifications pushsharp

我通过Nuget和VisualStudio(c#)在我的项目中使用PushSHarp尝试通过APN发送推送通知。

我创建了使用Apple帐户所需的所有文件(开发证书,.p12文件等)。

每次我使用正确的代码我都会有同样的错误(我重新创建了所有的东西,以确保我的令牌ID不错):

  

ID = 1 Code = ConnectionError   PushSharp.Common.NotificationException:无效的DeviceTokenà   PushSharp.Apple.ApnsNotification.ToBytes()à   PushSharp.Apple.ApnsConnection.createBatch(List 1 toSend) à PushSharp.Apple.ApnsConnection.<SendBatch>d__21.MoveNext() Message Apns notification error: 'ConnectionError' String PushSharp.Apple.ApnsNotificationException: Apns notification error: 'ConnectionError' ---> PushSharp.Common.NotificationException: Invalid DeviceToken à PushSharp.Apple.ApnsNotification.ToBytes() à PushSharp.Apple.ApnsConnection.createBatch(List 1 toSend)à   PushSharp.Apple.ApnsConnection.d__21.MoveNext()

你可以帮我解决这个问题吗?我真的不明白发生了什么。

我使用git wiki页面中给出的以下代码来发送通知,这里是代码

string TokenId = "f38205ce2137862735bd32e85b581dc85e2dc0abc04a2702e2899bddb9114543";

         PushNotificationApple _notifservice = new PushNotificationApple();

                _notifservice.SendAppleNotification(TokenId, "Test1");

    public void SendAppleNotification(string tokenId, string message)
            {
                byte[] appleCert = null;

                try
                {
                    appleCert = File.ReadAllBytes("F:\\Dev\\ap.p12");
                    System.Diagnostics.Debug.WriteLine("OK LECTURE APPLE CERT");
                }
                catch (Exception ex)
                {
                    var toto = ex.Message;
                    System.Diagnostics.Debug.WriteLine(" Exception lecture"+ ex.Message);
                }

                // Configuration (NOTE: .pfx can also be used here)
                var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox, appleCert, "BrumbySolution99");

                // Create a new broker
                var apnsBroker = new ApnsServiceBroker(config);

                // Wire up events
                apnsBroker.OnNotificationFailed += (notification, aggregateEx) =>
                {

                    aggregateEx.Handle(ex =>
                    {

                        // See what kind of exception it was to further diagnose
                        if (ex is ApnsNotificationException)
                        {

                            System.Diagnostics.Debug.WriteLine("ERRREUR NOTIFICATION");
                            var notificationException = (ApnsNotificationException)ex;

                            // Deal with the failed notification
                            var apnsNotification = notificationException.Notification;
                            var statusCode = notificationException.ErrorStatusCode;

                            System.Diagnostics.Debug.WriteLine("Apple Notification Failed: ID= " + notificationException.Notification.Identifier+" Code= "+ statusCode);
                            System.Diagnostics.Debug.WriteLine("link "+  ex.HelpLink);
                            System.Diagnostics.Debug.WriteLine("Inner "+  ex.InnerException);
                            System.Diagnostics.Debug.WriteLine("Message " + ex.Message);
                            System.Diagnostics.Debug.WriteLine("String " + ex.ToString());

                        }
                        else
                        {
                            // Inner exception might hold more useful information like an ApnsConnectionException           
                            System.Diagnostics.Debug.WriteLine("Apple Notification Failed for some unknown reason : {ex.InnerException}");

                        }

                        // Mark it as handled
                        return true;
                    });
                };

                apnsBroker.OnNotificationSucceeded += (notification) =>
                {
                    System.Diagnostics.Debug.WriteLine("APPLE NOTIFICATION SENT");
                   // Console.WriteLine("Apple Notification Sent!");
                };

                // Start the broker
                apnsBroker.Start();


                var notification_message = "{\"aps\":{\"alert\":{\"title\":\" Title " + message + "\",\"body\":\"Body " + message + "\"},\"badge\":\"1\"}}";
                // Queue a notification to send
                apnsBroker.QueueNotification(new ApnsNotification
                {
                    DeviceToken = tokenId,
                    Payload = JObject.Parse(notification_message)

                });

                // Stop the broker, wait for it to finish   
                // This isn't done after every message, but after you're
                // done with the broker
                apnsBroker.Stop();
            }
        }

0 个答案:

没有答案