PushSharp向iOS发送通知成功但未收到

时间:2017-06-29 03:30:57

标签: c# ios pushsharp

我使用最新的PushSharp(4.0.10)向iOS和Android设备发送通知。大约9个月前,我测试了它,似乎工作正常。我今天尝试了相同的应用程序,设备(iPhone)不再收到通知。设备令牌今天更新,因此它应该有效。 apnsBroker.OnNotificationSucceeded事件被触发但设备永远不会收到通知。 没有例外或任何其他类型的反馈。

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

1 个答案:

答案 0 :(得分:0)

这是一个有效的例子。安装Pushsharp并执行此代码。 确保您应该拥有有效的设备令牌+证书+证书密码。

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