captainclito
captainclito

Reputation: 31

IOS Push Notification doesn't work using PushSharp

Im using PushSHarp on my project via Nuget and VisualStudio (c#) to try to send push notification through APN.

I created all of the file that required using my Apple account(development certificate, .p12 files etc).

Each time i used the right code i stil have the same error( i recreated all of the thing to be sure my token id is not bad):

ID= 1 Code= ConnectionError PushSharp.Common.NotificationException: Invalid DeviceToken à PushSharp.Apple.ApnsNotification.ToBytes() à PushSharp.Apple.ApnsConnection.createBatch(List1 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(List1 toSend) à PushSharp.Apple.ApnsConnection.d__21.MoveNext()

Can you help me in this issue please? i really dont understand what happened.

I am using the below code given in their git wiki page to send the notifications here is the code

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

Upvotes: 1

Views: 1245

Answers (0)

Related Questions