2017-06-20 64 views
1

我使用PushSharp 4.0.10.0库来发送iOS设备上的通知,但它不起作用。我调试了它,发现有一些ApnsConfiguration连接问题。Pushsharp 4.0.10.0 iOS设备令牌的ApnsConfiguration连接错误

我使用此代码发送notificaiton:

public IHttpActionResult Notify() 
    { 
     HttpResponseMessage response = new HttpResponseMessage(); 
     HttpContent requestContent = Request.Content; 
     string errorMessage = "Some error occured.please try again later"; 
     HttpStatusCode responseCode = HttpStatusCode.Unauthorized; 
     string requestParameter = requestContent.ReadAsStringAsync().Result; 
     string tokan = ""; 
     var r = Request; 
     var header = r.Headers; 
     try 
     { 
      if (requestParameter != null) 
      { 
       PushNotificationModel complaintModel = JsonConvert.DeserializeObject<PushNotificationModel>(requestParameter); 
       JsonConvert.DeserializeObject<PushNotificationModel>(requestParameter); 
       var appleCert = File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/Images/User/xyz.pem")); 
       var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production, 
           appleCert, "xyz"); 

       // Create a new broker 
       var push = new ApnsServiceBroker(config); 

       int DeviceType = 1; 
       string deviceId = Convert.ToString(complaintModel.deviceToken); 
       string message = "New notification!!"; 
       Guid complaintId = complaintModel.ComplaintId; 
       string detail = complaintModel.detail; 
       try 
       { 
        //System.Web.Hosting.HostingEnvironment.MapPath("~/Images/User/") 
        // var appleCert = File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/Images/User/CTPwd.pem")); 
        push.OnNotificationFailed += (notification, aggregateEx) => 
        { 

         aggregateEx.Handle(ex => 
         { 

          // See what kind of exception it was to further diagnose 
          if (ex is ApnsNotificationException) 
          { 

           message = ex.Message; 
          } 
          else 
          { 
           message = "Not an APNSException"; 
          } 

          // Mark it as handled 
          return true; 
         }); 
        }; 
        try 
        { 
         push.OnNotificationSucceeded += (notification) => 
         { 
          message = "New Notification"; 
         }; 
         push.Start(); 
         string appleJsonFormat = "{\"aps\": {\"alert\":" + '"' + message + '"' + ",\"sound\": \"default\"}}"; 
         //string appleJsonFormat = "{\"aps\": {\"alert\": " + "Hello World" + ",\"sound\": \"default\"}}"; 
         push.QueueNotification(new ApnsNotification 
         { 
          DeviceToken = deviceId, 
          Payload = JObject.Parse(appleJsonFormat) 
         }); 

         push.Stop(); 
        } 
        catch(Exception ex) 
        { 

        } 

我已搜查谷歌,但没有发现任何相关的答案。有没有语法问题?

在此先感谢。

回答

1

请使用.P12文件格式进行推送通知快乐编码:)

+0

我找到了解决方案,但感谢您的回答:) –