2017-06-29 3 views
0

J'utilise le dernier PushSharp (4.0.10) pour envoyer des notifications aux appareils iOS et Android. Il y a environ 9 mois, j'ai testé cela et cela semblait fonctionner correctement. J'ai essayé la même application aujourd'hui et l'appareil (iPhone) ne reçoit plus les notifications. Le jeton d'appareil a été mis à jour aujourd'hui, il devrait donc être valide. L'événement apnsBroker.OnNotificationSucceeded est renvoyé mais l'appareil ne reçoit jamais la notification. Il n'y a aucune exception ou tout autre type de commentaires.PushSharp envoi de notifications à iOS réussi mais pas reçu

var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox, "mycert.p12", "password"); 

     // 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) 
       { 
        var notificationException = (ApnsNotificationException)ex; 

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

        Console.WriteLine($"Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}"); 

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

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

     apnsBroker.OnNotificationSucceeded += (notification) => 
     { 
      Console.WriteLine("Apple Notification Sent!"); 
     }; 

     // Start the broker 
     apnsBroker.Start(); 

     var payload = new Dictionary<string, object>(); 
     var aps = new Dictionary<string, object>(); 
     aps.Add("alert", GetAlert()); 
     aps.Add("badge", 1); 
     aps.Add("sound", "chime.aiff"); 
     payload.Add("aps", aps);    
     payload.Add("data", "info"); 


     apnsBroker.QueueNotification(new ApnsNotification 
     { 
      DeviceToken = textBox1.Text, 
      Payload = JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(payload)) 
     }); 

     apnsBroker.Stop(); 
+0

mettez-vous à jour le jeton vers votre serveur lorsque vous recevez un nouveau jeton d'APN? Sinon, mettez-vous en cache le jeton une fois que vous l'avez obtenu? – Subramanian

+0

Je mets toujours le jeton à jour pour m'assurer qu'il n'a pas expiré. – jbassking10

+0

Pouvez-vous passé le code de jeton recevoir? – Subramanian

Répondre

0

Ceci est un exemple de travail. Installez Pushsharp et exécutez ce code. Assurez-vous d'avoir un jeton de périphérique valide + un certificat + un mot de passe de certificat.

private void SendIOSPushNotification() 
{ 
    try 
    { 
     //Get Certificate 
     var appleCert = System.IO.File.ReadAllBytes(Server.MapPath("~/App_Data/Certificates_new.p12")); 

     // Configuration (NOTE: .pfx can also be used here) 
     var config = new ApnsConfiguration(ApnsConfiguration.ApnsServer## Heading ##Environment.Sandbox, appleCert, "YourCertificatePassword"); 

     // 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) 
       { 
        var notificationException = (ApnsNotificationException)ex; 

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

        Console.WriteLine($"Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}"); 
       } 
       else 
       { 
        // Inner exception might hold more useful information like an ApnsConnectionException   
        Console.WriteLine($"Apple Notification Failed for some unknown reason : {ex.InnerException}"); 
       } 
       // Mark it as handled 
       return true; 
      }); 
     }; 

     apnsBroker.OnNotificationSucceeded += (notification) => 
     { 
      Console.WriteLine("Apple Notification Sent!"); 
     }; 

     var fbs = new FeedbackService(config); 
     fbs.FeedbackReceived += (string deviceToken, DateTime timestamp) => 
     { 
      lblResult.Text = deviceToken +" Expired"; 
      // Remove the deviceToken from your database 
      // timestamp is the time the token was reported as expired 
     }; 

     // Start Proccess 
     apnsBroker.Start(); 

     //Device Token 
     //string _deviceToken = "8b5f6b908ae3d84a3a9778b1f5f40cc73b33fe2 ? 5431e41702cee6e90c599f1e3"; 
     string[] deviceTokens = "8b5f6b908ae3d84a3a9778b1f5f40cc73b33fe25431e41702cee6e90c599f1e3,0592798256a0d2f9e00ea366d5bd3595789fa0f551431f1d707cab76d933c25c".Split(','); 

     foreach (string _deviceToken in deviceTokens) 
     { 
      apnsBroker.QueueNotification(new ApnsNotification 
      { 
       DeviceToken = _deviceToken, 
       Payload = JObject.Parse(("{\"aps\":{\"badge\":1,\"sound\":\"oven.caf\",\"category\":\"NEW_MESSAGE_CATEGORY\",\"alert\":\"" + (txtMessage.Text.Trim() + "\"}}"))) 
      }); 
     } 

     apnsBroker.Stop(); 
     //lblResult.Text = "Messages sent"; 
    } 
    catch (Exception) 
    { 
     throw; 
    } 
}