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

Posted

技术标签:

【中文标题】PushSharp 向 iOS 发送通知成功但未收到【英文标题】:PushSharp sending notifications to iOS successful but not received 【发布时间】:2017-06-29 03:30:57 【问题描述】:

我正在使用最新的 PushSharp (4.0.10) 向 iosandroid 设备发送通知。大约 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();

【问题讨论】:

当您从 APN 获得新令牌时,您是否将令牌更新到您的服务器?否则,您是否在获得令牌后对其进行缓存? 我总是更新令牌以确保它没有过期。 你能把令牌接收的代码过去吗? 看起来问题是由于有人弄乱了应用程序并导致它失去了通知权限。 @jbassking10 你找到任何解决方案了吗?我遇到了完全相同的问题。 【参考方案1】:

这是一个工作示例。安装 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;
     

【讨论】:

以上是关于PushSharp 向 iOS 发送通知成功但未收到的主要内容,如果未能解决你的问题,请参考以下文章

PushSharp 是不是允许向 GCM for iOS 发送通知?

如何使用库 PushSharp 向 iOS 发送推送通知?

使用 Redth PushSharp 发送 iOS 推送通知失败

pushsharp 2 在向苹果 ios 发送通知时触发异常

PushSharp 事件未针对 WP8 触发

如何知道是不是使用 Pushsharp 将推送通知传递到 iOS 应用程序?