IOS C#

2016-04-14 15 views
0

'da Pushsharp kullanarak push bildirimi ios'de ios uygulamasında push bildirimi için bir kod geliştirdim ancak mobilde bildirim göndermiyor. Pushsharp kütüphanesi kullanıyorum.IOS C#

public PushNotificationApple() 
     { 
      if (_pushBroker == null) 
      { 
       //Create our push services broker 
       _pushBroker = new PushBroker(); 

       //Wire up the events for all the services that the broker registers 
       _pushBroker.OnNotificationSent += NotificationSent; 
       _pushBroker.OnChannelException += ChannelException; 
       _pushBroker.OnServiceException += ServiceException; 
       _pushBroker.OnNotificationFailed += NotificationFailed; 
       _pushBroker.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired; 
       _pushBroker.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged; 
       _pushBroker.OnChannelCreated += ChannelCreated; 
       _pushBroker.OnChannelDestroyed += ChannelDestroyed; 


       var appleCert = File.ReadAllBytes(System.Web.Hosting.HostingEnvironment.MapPath("~/Certificates" + ConfigSettings.SnaptymAPNSCertificate)); 


       _pushBroker.RegisterAppleService(new ApplePushChannelSettings(false, appleCert,ConfigSettings.SnaptymAPNSPassword)); //Extension method     
      } 
     } 

Benim sendNotification fonksiyonları şöyledir: - -:

public bool SendNotification(GcmNotificationPostDataModel postData) 
     { 

      if (_pushBroker != null) 
      { 
       foreach (var registrationId in postData.RegistrationIds) 
       { 

        _pushBroker.QueueNotification(new AppleNotification() 
               .ForDeviceToken(registrationId) //the recipient device id 
               .WithAlert(postData.Data.Message) //the message 
               .WithBadge(1) 
               .WithSound("sound.caf")); 

       } 
      } 

      return true; 
     } 
+0

Ne tür bir hata alıyorsunuz? İşlemi farklı itici broker olaylarında takip edebilirsiniz. – Patel

+0

istisnasının olup olmadığını kontrol edin Bu durumun sizin için geçerli olmadığından emin olun http://stackoverflow.com/questions/28499395/apn-production-certificate-not-being-recognized-by-pushsharp – Patel

+0

@patel İyi çağrı üretim ortamı, ancak şu anda OP kum havuzuna gönderiyor. Yeni ApplePushChannelSettings (false, ....) 'nin ilk parametresinde görülebilir. Ayrıca, bu durum bir istisna oluşturacaktır. – derpirscher

cevap

0

Ben

PushNotificationApple pushNotification = new PushNotificationApple(); 
pushNotification.SendNotification(postData); 

Benim PushNotificationApple yapıcı kod aşağıdaki gibidir:

Kodum takip gibidir PushSharp 4.0.10 kullanımı ve aşağıdaki kod benim için çalışıyor.

 private void SendMessage() 
     { 
      //IOS 
      var MainApnsData = new JObject(); 
      var ApnsData = new JObject(); 
      var data = new JObject(); 

      MainApnsData.Add("alert", Message.Text.Trim()); 
      MainApnsData.Add("badge", 1); 
      MainApnsData.Add("Sound", "default"); 

      data.Add("aps", MainApnsData); 

      ApnsData.Add("CalledFromNotify", txtboxID.Text.Trim()); 
      data.Add("CustomNotify", ApnsData); 

      //read the .p12 certificate file 
      byte[] bdata = System.IO.File.ReadAllBytes(Server.MapPath("~/App_Data/CertificatesPushNew.p12")); 

//create push sharp APNS configuration 
      var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox,bdata,"YourPassword"); 


      //create a apnService 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 deviceToken1, DateTime timestamp) => 
      { 
       //Remove the deviceToken from your database 
       // timestamp is the time the token was reported as expired 
       Console.WriteLine("Feedback received!"); 
      }; 
      fbs.Check(); 

      // Start the broker 
      apnsBroker.Start(); 

      var deviceToken = "Your device token"; 
      // Queue a notification to send 
      apnsBroker.QueueNotification(new ApnsNotification 
      { 
       DeviceToken = deviceToken, 
       Payload= data 

}); 

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